Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. MongoDB, c# driver, ElemMatch an array within an array

MongoDB, c# driver, ElemMatch an array within an array

Scheduled Pinned Locked Moved C#
mongodbcsharpcomdata-structureshelp
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jkirkerx
    wrote on last edited by
    #1

    Perhaps this is poor planing on my part, a bad document model but I thought it was the right way to go. I'm trying to find "US" and "AK" in a collection of Mongo Documents that represent ship rates. I have a collection of Ship Rate documents that look like this.

    {
    "Carrier" : "USPS",
    "US Mail First Class Package",
    "Countries" : [
    {
    "Name" : "United States",
    "Abbr" : "US",
    "States" : [
    "AL",
    "AK",
    "More states ..."
    ],
    "Name" : "Canada",
    "Abbr" : "CA",
    "States" : [
    "BC",
    "ON"
    ]
    }
    }

    With this code in my repository, I can get all the documents with "US" in countries, but I want to go one step further and qualify the state as well, in case I don't want to ship to Hawaii.

    var results = new List();
    var filter1 = Builders.Filter
    .ElemMatch(c => c.Countries, c => c.Abbr == countryCode);
    var shipRates = await _context.WebsiteShipServices.Find(filter1).ToListAsync();

    foreach (var shipRate in shipRates)
    {
    results.Add(new QuotedShipRates()
    {
    Name = shipRate.Name,
    Code = shipRate.Code,
    Rate = 5.99M,
    GuaranteedDate = DateTime.Now.AddDays(5)
    });
    }

    I just can't figure out how to get deeper here, to go to the array within the array. I wanted to make this AsQueryable at first, but couldn't figure out how to write an expression for it. I'm aware Canada has provinces, and Japan has prefectures, etc. I don't know what else to call states at the moment. LOL :)

    If it ain't broke don't fix it Discover my world at jkirkerx.com

    R J 2 Replies Last reply
    0
    • J jkirkerx

      Perhaps this is poor planing on my part, a bad document model but I thought it was the right way to go. I'm trying to find "US" and "AK" in a collection of Mongo Documents that represent ship rates. I have a collection of Ship Rate documents that look like this.

      {
      "Carrier" : "USPS",
      "US Mail First Class Package",
      "Countries" : [
      {
      "Name" : "United States",
      "Abbr" : "US",
      "States" : [
      "AL",
      "AK",
      "More states ..."
      ],
      "Name" : "Canada",
      "Abbr" : "CA",
      "States" : [
      "BC",
      "ON"
      ]
      }
      }

      With this code in my repository, I can get all the documents with "US" in countries, but I want to go one step further and qualify the state as well, in case I don't want to ship to Hawaii.

      var results = new List();
      var filter1 = Builders.Filter
      .ElemMatch(c => c.Countries, c => c.Abbr == countryCode);
      var shipRates = await _context.WebsiteShipServices.Find(filter1).ToListAsync();

      foreach (var shipRate in shipRates)
      {
      results.Add(new QuotedShipRates()
      {
      Name = shipRate.Name,
      Code = shipRate.Code,
      Rate = 5.99M,
      GuaranteedDate = DateTime.Now.AddDays(5)
      });
      }

      I just can't figure out how to get deeper here, to go to the array within the array. I wanted to make this AsQueryable at first, but couldn't figure out how to write an expression for it. I'm aware Canada has provinces, and Japan has prefectures, etc. I don't know what else to call states at the moment. LOL :)

      If it ain't broke don't fix it Discover my world at jkirkerx.com

      R Offline
      R Offline
      realJSOP
      wrote on last edited by
      #2

      That's exactly how to do it. It's an array within an array (or more preferable IMHO, a list within a list). Define your classes, and then use newtonsoft.json to deserialize into your objects.

      ".45 ACP - because shooting twice is just silly" - JSOP, 2010
      -----
      You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
      -----
      When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013

      J 1 Reply Last reply
      0
      • R realJSOP

        That's exactly how to do it. It's an array within an array (or more preferable IMHO, a list within a list). Define your classes, and then use newtonsoft.json to deserialize into your objects.

        ".45 ACP - because shooting twice is just silly" - JSOP, 2010
        -----
        You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
        -----
        When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013

        J Offline
        J Offline
        jkirkerx
        wrote on last edited by
        #3

        Well that was uplifting! At least I designed my model right. :)

        If it ain't broke don't fix it Discover my world at jkirkerx.com

        1 Reply Last reply
        0
        • J jkirkerx

          Perhaps this is poor planing on my part, a bad document model but I thought it was the right way to go. I'm trying to find "US" and "AK" in a collection of Mongo Documents that represent ship rates. I have a collection of Ship Rate documents that look like this.

          {
          "Carrier" : "USPS",
          "US Mail First Class Package",
          "Countries" : [
          {
          "Name" : "United States",
          "Abbr" : "US",
          "States" : [
          "AL",
          "AK",
          "More states ..."
          ],
          "Name" : "Canada",
          "Abbr" : "CA",
          "States" : [
          "BC",
          "ON"
          ]
          }
          }

          With this code in my repository, I can get all the documents with "US" in countries, but I want to go one step further and qualify the state as well, in case I don't want to ship to Hawaii.

          var results = new List();
          var filter1 = Builders.Filter
          .ElemMatch(c => c.Countries, c => c.Abbr == countryCode);
          var shipRates = await _context.WebsiteShipServices.Find(filter1).ToListAsync();

          foreach (var shipRate in shipRates)
          {
          results.Add(new QuotedShipRates()
          {
          Name = shipRate.Name,
          Code = shipRate.Code,
          Rate = 5.99M,
          GuaranteedDate = DateTime.Now.AddDays(5)
          });
          }

          I just can't figure out how to get deeper here, to go to the array within the array. I wanted to make this AsQueryable at first, but couldn't figure out how to write an expression for it. I'm aware Canada has provinces, and Japan has prefectures, etc. I don't know what else to call states at the moment. LOL :)

          If it ain't broke don't fix it Discover my world at jkirkerx.com

          J Offline
          J Offline
          jkirkerx
          wrote on last edited by
          #4

          This actually worked after modifying the document and taking out Hawaii. I didn't think of it when I posted the first time, but it makes sense now. I just appended && c.states.Contains(stateCode) to the filter. I was hoping my model of states would work as like a continuous string of values.

          var results = new List();
          var filter1 = Builders.Filter
          .ElemMatch(c => c.Countries, c => c.Abbr == countryCode && c.States.Contains(stateCode));
          var shipRates = await _context.WebsiteShipServices.Find(filter1).ToListAsync();

          foreach (var shipRate in shipRates)
          {
          results.Add(new QuotedShipRates()
          {
          Name = shipRate.Name,
          Code = shipRate.Code,
          Alias = shipRate.Alias,
          Rate = 5.99M,
          GuaranteedDate = DateTime.Now.AddDays(shipRate.Days_Max),
          Selected = false
          });
          }

          If it ain't broke don't fix it Discover my world at jkirkerx.com

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups