我在Query for documents where array size is greater than 1上找到了相关的内容,它适用于find
,但不适用于aggregation
查询...
我有以下用户集合......
{
"_id" : ObjectId("5ac8b91482c2345af70d4650"),
"firstName" : "Clerk",
"lastName" : "Kent",
"email" : "clerk@ltc.com",
"endorsedBy" : [],
"followings" : [],
"followers" : [],
"isAdminEndorsed" : false,
}
{
"_id" : ObjectId("5ac8ba3582c2345af70d4658"),
"firstName" : "Bruce",
"lastName" : "Wayne",
"email" : "bruce@linkites.com",
"endorsedBy" : [],
"followings" : [
ObjectId("5acc97b817c27b258bc9aa1e")
],
"followers" : [],
"isAdminEndorsed" : false
}
{
"_id" : ObjectId("5acc97b817c27b258bc9aa1e"),
"firstName" : "admin",
"lastName" : "ltc",
"email" : "superadmin@ltc.com",
"endorsedBy" : [],
"followings" : [],
"followers" : [
ObjectId("5ac8ba3582c2345af70d4658")
],
"isAdminEndorsed" : true
}
{
"_id" : ObjectId("5ad6ed4b7730923118b399a7"),
"firstName" : "Batman",
"lastName" : "tge",
"email" : "ashishlal95@gmail.com",
"endorsedBy" : [
ObjectId("5ad6ed4b7730923118b399a7"),
ObjectId("5ad6ed4b7730923118b399a7"),
ObjectId("5ad6ed4b7730923118b399a7"),
ObjectId("5ad6ed4b7730923118b399a7"),
ObjectId("5ad6ed4b7730923118b399a7"),
ObjectId("5ad6ed4b7730923118b399a7"),
ObjectId("5ad6ed4b7730923118b399a7"),
ObjectId("5ad6ed4b7730923118b399a7"),
ObjectId("5ad6ed4b7730923118b399a7"),
ObjectId("5ad6ed4b7730923118b399a7"),
ObjectId("5ad6ed4b7730923118b399a7")
],
"followings" : [],
"followers" : [],
"isAdminEndorsed" : false
}
我想要用户列表
1)没有登录用户。
2)背书,长度应小于10。
3)isAdminEndorsed应该是假的。
4)firstName应匹配正则表达式'cle'。
我尝试过以下查询但不起作用......我在这里找不到任何东西......
const user = await User.aggregate([{
$match: {
_id: {
$ne: mongoose.Types.ObjectId(req.user.id)
},
$and: [{ endorsedBy: { $lt: 10 } }, { isAdminEndorsed: false }]
}
},{
$project: {
isFollow: { $in: [mongoose.Types.ObjectId(req.user.id),
'$followers'] },
}
}])
我的输出应该是
{
"_id" : ObjectId("5ac8b91482c2345af70d4650"),
"firstName" : "Clerk",
"lastName" : "Kent"
}
答案 0 :(得分:1)
您应该检查数组中absence
的{{1}}。
在适当的猫鼬改变后尝试此查询:
如果您不想进行不区分大小写的搜索,请务必进行适当的更改,此处不需要10th element
运算符。
$and
输出:
db.sample.aggregate([
{$match: {
_id: {$ne: ObjectId("5ac8ba3582c2345af70d4658")},
isAdminEndorsed: false,
"endorsedBy.9":{$exists:false},
firstName:{$regex:"cle", $options:"i"}
}},
{$project: {
isFollow: { $in: [ObjectId("5ac8b91482c2345af70d4650"),
'$followers'] },
}
}
])