$nin 与 $expr [英] $nin with the $expr
问题描述
我有一个查询来查找用户 CreatedBy
是否在 SharedWith
中.我想反转查询以检查 CreatedBy
是否不在 SharedWith
中.
MongoDB 不支持用于 $and
查询的直接 $nin
或 $not
.
知道如何实现这一点.
用户文档看起来像这样,
集合 = [{由...制作":{"_id": "User001","电子邮件": "user@eg.com","Name": "User001"},跟...一起分享": [{"_id": "User001","电子邮件": "user@eg.com","Name": "User001"},{"_id": "User002","电子邮件": "user@eg.com","Name": "User002"},{"_id": "User003","电子邮件": "user@eg.com","Name": "User003"},]}]
$nin
是查询运算符,而不是聚合运算符,也是 $expr
仅支持 aggregation
运算符而不支持 query
那些.因此,您可能应该使用 $not
$in代码>
与 $expr
表达式以这种方式
<代码>{$匹配":{"$and": [{$或":[{多用户": {$exists":错误}},{$expr":{"$not": { "$in": ["$CreatedBy", "$Multi_User"] }}}]}]}}
I have a query to find if a user CreatedBy
is in a SharedWith
. I want to inverse the query to check if the CreatedBy
is not in SharedWith
.
[
{
"$match": {
"$and": [
{
"$and": [
{
"SharedWith": {
"$exists": true
}
},
{
"$expr": {
"$in": [
"$CreatedBy",
"$Multi_User"
]
}
}
]
}
]
}
}
]
MongoDB does not support a direct $nin
or $not
for $and
query.
Any idea how to achieve this.
The user document looks like this,
Collection = [
{"CreatedBy":
{"_id": "User001",
"Email": "user@eg.com",
"Name": "User001"},
"SharedWith": [
{"_id": "User001",
"Email": "user@eg.com",
"Name": "User001"},
{"_id": "User002",
"Email": "user@eg.com",
"Name": "User002"},
{"_id": "User003",
"Email": "user@eg.com",
"Name": "User003"},
]}
]
$nin
is an query operator not an aggregation operator and also $expr
only supports the aggregation
operators not the query
ones. So, You should probably use $not
$in
with the $expr
expression
in this manner
{
"$match": {
"$and": [
{
"$or": [
{
"Multi_User": {
"$exists": False
}
},
{
"$expr": {
"$not": { "$in": ["$CreatedBy", "$Multi_User"] }
}
}
]
}
]
}
}
这篇关于$nin 与 $expr的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!