在具有特定条件的嵌套级别收集不同的字段名称
Collect distinct field names at nested level with specific condition
我有问题陈述,其中我需要 "config.first.second" child 级别的所有字段名称,其中包含字段至少一次为真。
这是我的 mongo collection objects.
[ {
"_id" : ObjectId("560e97f4a78eb445cd2d75e5"),
"config" : {
"first" : {
"second" : {
"field1" : {
"include":"true"
},
"field3" : {
"include":"true"
},
"field9" : {
"include":"false"
},
"field6" : {
"include":"false"
}
}
}
},
"date_created" : "Fri Oct 02 14:43:00 UTC 2015",
"last_updated" : "Mon Apr 11 15:26:37 UTC 2016",
"id" : ObjectId("560e97f4a78eb445cd2d75e5")
},
{
"_id" : ObjectId("56154465a78e41c04692af20"),
"config" : {
"first" : {
"second" : {
"field1" : {
"include":"true"
},
"field3" : {
"include":"false"
},
"field7" : {
"include":"true"
}
}
}
},
"date_created" : "Wed Oct 07 16:12:21 UTC 2015",
"last_updated" : "Mon Apr 11 15:18:58 UTC 2016",
"id" : ObjectId("56154465a78e41c04692af20")
}
]
使用以上 mongo collection 。查询必须return结果
["field1","field3","field7"]
您可以 运行 使用 mapReduce:
db.collection.mapReduce(
function() {
Object.keys(this.config.first.second)
.filter( k => this.config.first.second[k].include === "true" )
.forEach(k => emit(k,1) );
},
function() { },
{
"out": { "inline": 1 },
}
)['results'].map( d => d._id )
如果你有 MongoDB 3.4 那么你可以使用 .aggregate()
:
db.collection.aggregate([
{ "$project": {
"field": {
"$filter": {
"input": { "$objectToArray": "$config.first.second" },
"as": "f",
"cond": { "$eq": [ "$$f.v.include", "true" ] }
}
}
}},
{ "$unwind": "$field" },
{ "$group": { "_id": "$field.k" } }
]).toArray().map(d => d._id)
Returns:
[
"field1",
"field3",
"field7"
]
我有问题陈述,其中我需要 "config.first.second" child 级别的所有字段名称,其中包含字段至少一次为真。 这是我的 mongo collection objects.
[ {
"_id" : ObjectId("560e97f4a78eb445cd2d75e5"),
"config" : {
"first" : {
"second" : {
"field1" : {
"include":"true"
},
"field3" : {
"include":"true"
},
"field9" : {
"include":"false"
},
"field6" : {
"include":"false"
}
}
}
},
"date_created" : "Fri Oct 02 14:43:00 UTC 2015",
"last_updated" : "Mon Apr 11 15:26:37 UTC 2016",
"id" : ObjectId("560e97f4a78eb445cd2d75e5")
},
{
"_id" : ObjectId("56154465a78e41c04692af20"),
"config" : {
"first" : {
"second" : {
"field1" : {
"include":"true"
},
"field3" : {
"include":"false"
},
"field7" : {
"include":"true"
}
}
}
},
"date_created" : "Wed Oct 07 16:12:21 UTC 2015",
"last_updated" : "Mon Apr 11 15:18:58 UTC 2016",
"id" : ObjectId("56154465a78e41c04692af20")
}
]
使用以上 mongo collection 。查询必须return结果
["field1","field3","field7"]
您可以 运行 使用 mapReduce:
db.collection.mapReduce(
function() {
Object.keys(this.config.first.second)
.filter( k => this.config.first.second[k].include === "true" )
.forEach(k => emit(k,1) );
},
function() { },
{
"out": { "inline": 1 },
}
)['results'].map( d => d._id )
如果你有 MongoDB 3.4 那么你可以使用 .aggregate()
:
db.collection.aggregate([
{ "$project": {
"field": {
"$filter": {
"input": { "$objectToArray": "$config.first.second" },
"as": "f",
"cond": { "$eq": [ "$$f.v.include", "true" ] }
}
}
}},
{ "$unwind": "$field" },
{ "$group": { "_id": "$field.k" } }
]).toArray().map(d => d._id)
Returns:
[
"field1",
"field3",
"field7"
]