以下是我的数据库,只有相关字段:
[
{
_id: ObjectId('641b5665894f755f6dc422a2'),
directMsgs: {
person1: [
{
authorId: ObjectId('6454bb4e33b90133ba752e96'),
isRead: false
},
{
authorId: ObjectId('641b5665894f755f6dc422a2'),
isRead: true
}
],
person2: [
{
authorId: ObjectId('641b5665894f755f6dc422a2'),
isRead: true
},
{
authorId: ObjectId('642b5765834f755f6dc422b3'),
isRead: true
}
]
}
}
]
下面是投影后的样子:
[
{
_id: ObjectId('641b5665894f755f6dc422a2'),
directMsgs: [
{
k: 'person1',
v: [
{
authorId: ObjectId('6454bb4e33b90133ba752e96'),
isRead: false
},
{
authorId: ObjectId('641b5665894f755f6dc422a2'),
isRead: true
}
],
},
{
{
k: 'person2',
v: [
{
authorId: ObjectId('641b5665894f755f6dc422a2'),
isRead: true
},
{
authorId: ObjectId('642b5765834f755f6dc422b3'),
isRead: true
}
]
}
}
]
}
]
我希望能够检查一个用户是否有任何未读消息,其中消息的authorId
不等于用户的_id
。以下是我尝试过的:
const userId = '641b5665891f755f6dd422a2'
const uid = new ObjectId(userId)
const data = await User.aggregate(
[
{
$project: {
directMsgs: { $objectToArray: '$directMsgs'}
}
},
{
$match: {
_id: uid,
'directMsgs.v.authorId': { $ne: uid },
'directMsgs.v.isRead': false
}
}
]
)
匹配似乎没有这行也能正常工作:'directMsgs.v.authorId': { $ne: uid }
。如果我将其更改为'directMsgs.v.authorId': uid
,它将与您所期望的匹配。$ne
操作员似乎有问题。你们会在这里干什么?
1条答案
按热度按时间piok6c0g1#
来自MongoDB
$match
文档$match接受指定查询条件的文档。查询语法与读取操作查询语法相同;即$match不接受原始聚合表达式。相反,使用$expr查询表达式将聚合表达式包含在$match中。
当您尝试在
$match
阶段使用运算符实现过滤时,查询不起作用:您应该使用
$expr
运算符。$match
载物台$and
-过滤匹配多个条件的文档。1.1.按
_id
匹配文档1.2.
$ne
-比较 1的结果。2.1 不是空数组。1.2.1
$filter
-过滤数组中的文档。input
-$reduce
-将嵌套数组directMsgs.v
展平为一级数组。cond
-通过authorId
不等于uid
和isRead
等于false
匹配文档。Demo @ Mongo Playground