MongoDB, c# driver, ElemMatch an array within an array
-
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
-
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
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 -
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, 2013Well 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
-
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
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