【发布时间】:2021-12-12 23:19:43
【问题描述】:
如何设置某个字段不能为空字符串。
当我将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 参数和搜索整个互联网,但我找不到满意的答案?
有人知道我做错了什么吗?
【问题讨论】:
-
可能给出样本数据和预期输出,找到导致问题的阶段,我们可以测试一下
标签: mongodb mongodb-query aggregation-framework pymongo