Mongodb 过滤字段而不输入空值
收藏:Mongodb 过滤字段而不输入空值,mongodb,aggregation-framework,Mongodb,Aggregation Framework,收藏: { "_id" : ObjectId("5338ec2a5b5b71242a1c911c"), "people" : [ { "name" : "Jhon" }, { "age" : "30" }, { "weight" : "80" } ]} 查询:db.tmp.aggregate({$project:{“people.name”:1}}) 本期: { "result" : [
{
"_id" : ObjectId("5338ec2a5b5b71242a1c911c"),
"people" : [
{
"name" : "Jhon"
},
{
"age" : "30"
},
{
"weight" : "80"
}
]}
查询:db.tmp.aggregate({$project:{“people.name”:1}})
本期:
{
"result" : [
{
"_id" : ObjectId("5338ec2a5b5b71242a1c911c"),
"people" : [
{
"name" : "Jhon"
},
{},
{}
]
}
],
"ok" : 1}
如何在没有{}的情况下仅显示“name”字段为空?在中处理数组时应该使用
db.tmp.aggregate([
{“$REWIND”:“$people”},
{“$match”:{“people.name”:{“$exists”:true}},
{“$project”:{“people.name”:1}
])
如果要将其作为阵列,请分组:
db.tmp.aggregate([
{“$REWIND”:“$people”},
{“$match”:{“people.name”:{“$exists”:true}},
{“$group”:{
“\u id”:“$\u id”,
“people”:{“$push”:{“name”:“$people.name”}
}}
])