如何设置字符串字段不等于空字符串MongoDB聚合管道 [英] How to set that a string field is not equal to empty string MongoDB aggregation pipeline
本文介绍了如何设置字符串字段不等于空字符串MongoDB聚合管道的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何设置某个字段不能为空字符串。
当我将friendsArray.lastMessage
字符串字段设置为空时,会得到我想要的结果,即:
matches = db.user_relations.aggregate(
[
{"$match": {"userId": id}},
{"$unwind": {"path": "$friendsArray"}},
{"$sort": {"friendsArray.lastTimestamp": -1}},
{"$match": {"friendsArray.lastMessage": ""}},
{"$limit": 6},
{"$replaceRoot": {"newRoot": "$friendsArray"}},
{
"$lookup": {
"from": "users",
"localField": "userId",
"foreignField": "_id",
"as": "joined__"
}
},
{"$unwind": {"path": "$joined__"}},
{"$replaceRoot": {"newRoot": {"$mergeObjects": ["$joined__","$$ROOT"]}}},
{"$project": {"joined__": 0}},
{"$match": {"$expr": {"$eq": ["$isProfileBlocked", False]}}},
{
'$addFields': {'userId': {'$toString': '$userId'}}
},
{"$unset": valuesToUnset}
]
)
但是当我更改此设置时:{"$match": {"friendsArray.lastMessage": ""}},
为此:{"$match": {"friendsArray.lastMessage": {"$not" : [{"$eq": ""}]}}}
它给了我一些奇怪的结果?它混合数据,我真的不知道它在做什么?
我试着放了$ne
和$nin
参数,找遍了整个互联网,还是找不到满意的答案?
有人知道我做错了什么吗?
推荐答案
您在$eq
运算符上使用的sinTax不正确。检查docs。
请注意,查找中的$eq
与聚合中的$eq
不同。
正确的做法是如何在该行上使用它:
{"$match": {"$expr": {"$eq": ["$isProfileBlocked", False]}}},
我还举例说明了您需要什么here。
这篇关于如何设置字符串字段不等于空字符串MongoDB聚合管道的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文