tor*_*ard 5 mongodb mongodb-query
我试图从我的预订总量中排除某些字段。
{ “用户”:0, “付款类型”:0, “_id”:0 }
没有这个它工作得很好,但是有了它我得到了错误:
assert: command failed: {
"ok" : 0,
"errmsg" : "A pipeline stage specification object must contain exactly one field.",
"code" : 40323,
"codeName" : "Location40323"
} : aggregate failed
Run Code Online (Sandbox Code Playgroud)
这是可以做到的,还是我只能用 find() 来做到这一点?
db.getCollection('booking').aggregate([{
$match: {
checkin : {$lte: (1512145439)},
checkout: {$gte: (1483203600)},
}
},
{
"user":0,
"paymentType":0,
"_id":0
}
, {
$lookup: {
from: "users",
localField: "user",
foreignField: "_id",
as: "users"
}
}, {
$unwind: "$user"
}, {
$addFields: {
"country": "$users.country"
}
}, {
$project: {
"users": 0
}
}, {
$project: {
"booking": 0
}
}])
Run Code Online (Sandbox Code Playgroud)
如果我这样做,它将排除以下字段:
db.getCollection('booking').find({},
{
"user":0,
"paymentType":0,
"_id":0
})
Run Code Online (Sandbox Code Playgroud)
您必须在聚合管道的阶段内使用字段排除/包含$project。
附带说明,$addFields当您需要添加新字段/覆盖字段时使用,$project当您想要限制字段时使用。
从文档中,
$addFields 阶段相当于 $project 阶段,它显式指定输入文档中的所有现有字段并添加新字段。
| 归档时间: |
|
| 查看次数: |
11471 次 |
| 最近记录: |