使用PyMongo,如何查找/搜索嵌套数组json对象与给定字符串匹配的文档。在
给定MongoDB集合中的以下2个产品JSON文档。。在
[{
"_id" : ObjectId("5be1a1b2aa21bb3ceac339b0"),
"id" : "1",
"prod_attr" : [
{
"name" : "Branded X 1 Sneaker"
},
{
"hierarchy" : {
"dept" : "10",
"class" : "101",
"subclass" : "1011"
}
}
]
},
{
"_id" : ObjectId("7be1a1b2aa21bb3ceac339xx"),
"id" : "2",
"prod_attr" : [
{
"name" : "Branded Y 2 Sneaker"
},
{
"hierarchy" : {
"dept" : "10",
"class" : "101",
"subclass" : "2022"
}
}
]
}
]
我想 1返回prod中的所有文档_att.hierarchy.subclass=“2022年” 2返回prod中的所有文档_属性名称包含“运动鞋”
我很感激JSON可以有不同的结构,不幸的是,这不是我能控制的范围。在
1。返回prod中的所有文档_属性层次结构子类=“2022年”
根据MongoDB的Query an Array of Embedded Documents文档,可以使用点表示法将数组字段(
prod_attr
)的名称与一个点(.
)和嵌套文档中的字段名(hierarchy.subclass
)连接起来:2。返回prod中的所有文档_属性名称包含“运动鞋”
如前所述,可以使用点表示法查询数组中嵌套元素的字段。 要执行“包含”查询,必须使用^{} 运算符:
^{pr2}$另一个选择是使用MongoDB聚合框架:
$unwind
运算符为prod_attr
数组中的每个对象创建一个新对象,因此只有嵌套文档,没有数组(有关详细信息,请查看documentation)。下一步是
$match
运算符,它实际对嵌套对象执行查询。在这是一个简单的例子,但是使用Aggregators Operators你有很大的灵活性。在
相关问题 更多 >
编程相关推荐