I’m trying to query a collection in mongo that’s structured like this…
{
"type":"apple"
"quality": {
"1": {
"owner":"Dan",
"age":28
},
"2": {
"owner":"Joe",
"age":21
}
"3": {
"owner":"Bob",
"age":29
}
}
}
I initially tried findOne({"quality.owner":"Dan"}) but that failed. Then I realized it needed to be done like findOne({"quality.1.owner":"Dan"}) … not what I want.
Is there anyway to put a wildcard in place of the 1 or to perform the query the way I’m intending? I don’t know what all the variables will be in the quality objects so I don’t think I can do a “sub-object” match either =/
EDIT:
Unfortunately Re-formatting this data is out of the question for the project as the data is being retrieved by a 3rd party API and those index numbers represent “placement” (1st place, 2nd place…)
With your current schema you can’t query like you want(
findOne({"quality.owner":"Dan"})). With embedded documents you can search only with dot notation.But you can redesign your current schema to be like this :
And then search using dot notation: