Json MongoDB:如何通过键值获取嵌套对象的数据?
我正在学习MongoDB,并尝试使用MongoDB客户端通过特定的键值检索对象 我有以下数据:Json MongoDB:如何通过键值获取嵌套对象的数据?,json,mongodb,Json,Mongodb,我正在学习MongoDB,并尝试使用MongoDB客户端通过特定的键值检索对象 我有以下数据: { "type": "products", "products": { "Intel® Core™ i9-9980XE Extreme Edition": { "description": null, "price": 2457, "catalog_id": "1" },
{
"type": "products",
"products": {
"Intel® Core™ i9-9980XE Extreme Edition": {
"description": null,
"price": 2457,
"catalog_id": "1"
},
"Intel® Core™ i9-9980HK": {
"description": null,
"price": 1548,
"catalog_id": "1"
},
"AMD Ryzen Threadripper 2990WX": {
"description": null,
"price": 500,
"catalog_id": "2"
},
"Baikalel Ectronics BE-M1000": {
"description": null,
"price": 128,
"catalog_id": "3"
},
"GeForce RTX 2080 Ti": {
"description": null,
"price": 2048,
"catalog_id": "5"
}
}
}
我已经了解了如何访问嵌套对象中的数据:
db.shop.findOne( { type : "products" }).products["GeForce RTX 2080 Ti"].price
但是我有点困惑如何通过“catalog\u id”过滤所有hested对象:“1”
当我使用
db.shop.find( { type : "products" }, {"catalog_id": "1"})
MongoDB客户端只显示主对象的id。db.shop.find({“products.catalog\u id:“1”})
类似的问题您在此处共享的数据看起来只是一个对象,有两个键类型和产品。因此,您的查询匹配此对象并正确返回对象。之后需要对其进行解析。Mongo查询将只提供记录
{
**"_id": ObjectId("58c7da2adaa8d031ea699fff")**
"type": "products",
"products": {
"Intel® Core™ i9-9980XE Extreme Edition": {
"description": null,
"price": 2457,
"catalog_id": "1"
},
"Intel® Core™ i9-9980HK": {
"description": null,
"price": 1548,
"catalog_id": "1"
},
"AMD Ryzen Threadripper 2990WX": {
"description": null,
"price": 500,
"catalog_id": "2"
},
"Baikalel Ectronics BE-M1000": {
"description": null,
"price": 128,
"catalog_id": "3"
},
"GeForce RTX 2080 Ti": {
"description": null,
"price": 2048,
"catalog_id": "5"
}
}
}