gpt4 book ai didi

javascript - 如何使用条件参数查询 mongodb 中的多个文档

转载 作者:行者123 更新时间:2023-12-03 01:06:37 24 4
gpt4 key购买 nike

我有包含 groupIdcreatedTS 字段的消息文档。对于查询,我有带有 groupIdlastVisit 的对象数组。我想查询 lastVisit

之后每个 groupId 的所有消息

我尝试使用$in和groupIds,但它没有使用lastVisit过滤createdTS

成员架构

const GroupMemberSchema = new mongoose.Schema({
userId: { type: String, required: true },
groupId: { type: String, required: true },
addTS: { type: Date, default: Date.now },
lastVisit: { type: Date, default: Date.now }
});

消息架构

const GroupMessageSchema = new mongoose.Schema({
id: { type: String, required: true },
groupId: { type: String, required: true },
content: { type: String, required: true },
createdTS: { type: Date, default: Date.now },
});

查询

GroupMessage.find({groupId: {$in: groupIds}})

最佳答案

如果我对问题的理解正确,那么您需要获取与每个 groupId 匹配且同时大于适当的 lastVisit 的记录。如果将其转换为 MongoDB 查询,它将是这样的:

{
"$or": [
{
"$and": [
{ "groupId": _groupId[i] },
{ "createdTS": { "$gt": _lastVisit[i] } }
]
},
...
]
}

其中_groupId[i]_lastVisit[i]是组列表和lastVisit时间戳的数组元素。

关于javascript - 如何使用条件参数查询 mongodb 中的多个文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52368946/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com