我正在寻找mongodb查询来从文档内的文档中检索结果。我有这样的集合:
{
"_id" : ObjectId("59e6ea30ef03f811538bagd4"),
"_class" : "xyz",
"name" : "Test1",
"attributes" : [
{
"attributeName" : "Country",
"includedAttributes" : [
{
"_id" : ObjectId("59ddffe7d1f2f34eb9d32dcf"),
"attributeValue" : "United States",
"attributeName" : "Country"
}
]
}
]
},
{
"_id" : ObjectId("59e6ea30ef03f811538bxed4"),
"_class" : "xyz",
"name" : "Test2",
"attributes" : [
{
"attributeName" : "Country",
"includedAttributes" : [
{
"_id" : ObjectId("59ddffe7d1f2g34eb9d32dcf"),
"attributeValue" : "Buffalo",
"attributeName" : "City"
}
]
}
]
},
{
"_id" : ObjectId("59e6ea30ef03f811538baef4"),
"_class" : "xyz",
"name" : "Test3",
"attributes" : [
{
"attributeName" : "City",
"includedAttributes" : [
{
"_id" : ObjectId("59ddffe7h1f2e34eb9d32dcf"),
"attributeValue" : "Chicago",
"attributeName" : "City"
}
]
}
]
}
我希望查询此条件inludedAttributes {attributeName:Country and attributeValue:United States}或inludedAttributes {attributeName:City and attributeValue:Buffalo}。
我尝试了以下查询,但它给了我零结果:
db.getCollection('collection1').find({
"$or" : [ { "attributes" : { "$elemMatch" : { "includedAttributes" : { "$elemMatch" : { "attributeName" : "Country"}} , "$and" : [ { "attributeValue" : "United States"}]}}},
{ "attributes" : { "$elemMatch" : { "includedAttributes" : { "$elemMatch" : { "attributeName" : "City"}} , "$and" : [ { "attributeValue" : "Buffalo"}]}}} ]})