gpt4 book ai didi

MongoDB 从聚合中排除字段,这可能吗?

转载 作者:行者123 更新时间:2023-12-05 05:21:15 27 4
gpt4 key购买 nike

我试图从我的预订汇总中排除一些字段。

{ “用户”: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

这是可能的,还是我只能用 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
}
}])

如果我这样做,它将排除字段:

db.getCollection('booking').find({},        
{
"user":0,
"paymentType":0,
"_id":0
})

最佳答案

您必须在聚合管道的 $project 阶段使用字段排除/包含。

旁注,当您需要添加新字段/覆盖字段时使用 $addFields,当您想要限制字段时使用 $project

来自文档,

The $addFields stage is equivalent to a $project stage that explicitly specifies all existing fields in the input documents and adds the new fields.

关于MongoDB 从聚合中排除字段,这可能吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43265628/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com