$ nin与$ expr [英] $nin with the $expr
本文介绍了$ nin与$ expr的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个查询来查找用户CreatedBy
是否在SharedWith
中.我想反查询,以检查CreatedBy
是否不在SharedWith
中.
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不支持直接对$and
进行查询的$nin
或$not
.
MongoDB does not support a direct $nin
or $not
for $and
query.
任何想法如何实现这一目标.
Any idea how to achieve 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
是查询运算符,不是聚合运算符,也是 $expr
仅支持aggregation
运算符,而不支持query
运算符.因此,您可能应该使用 $not
$in
使用 $expr
表达式
以这种方式
$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屋!
查看全文