MongoDB对特定嵌套属性的投影?
对于特定嵌套属性的投影,请在MongoDB中使用Aggregate()。首先让我们创建一个包含文档的集合-
> db.demo379.insertOne(... {
... "details1" : {
... "details2" : {
... "details3" : {
... "10" : "John",
... "50" : "Chris",
... "40" : "David",
... "30":"Mike"
... }
... }
... }
... }
... );
{
"acknowledged" : true,
"insertedId" : ObjectId("5e5a94f82ae06a1609a00b10")
}
在find()方法的帮助下显示集合中的所有文档-
> db.demo379.find().pretty();
这将产生以下输出-
{"_id" : ObjectId("5e5a94f82ae06a1609a00b10"),
"details1" : {
"details2" : {
"details3" : {
"10" : "John",
"30" : "Mike",
"40" : "David",
"50" : "Chris"
}
}
}
}
以下是对特定嵌套属性的投影查询-
> db.demo379.aggregate([... { "$addFields": {
... "details1.details2.details3": {
... "$arrayToObject": {
... "$filter": {
... "input": { "$objectToArray": "$details1.details2.details3" },
... "as": "out",
... "cond": {
... "$and": [
... { "$gte": ["$$out.k", "35"] },
... { "$lte": ["$$out.k", "60"] },
... ]
... }
... }
... }
... }
... } }
... ])
这将产生以下输出-
{ "_id" : ObjectId("5e5a94f82ae06a1609a00b10"), "details1" : { "details2" : { "details3" : { "40" : "David", "50" : "Chris" } } } }
以上是 MongoDB对特定嵌套属性的投影? 的全部内容, 来源链接: utcz.com/z/317101.html