【发布时间】:2021-11-06 00:55:54
【问题描述】:
我正在尝试获取一组用户的聊天记录。 聊天模式的定义如下:
const ChatSchema = new Schema<IChatSchema>(
{
messages: [
{
type: Schema.Types.ObjectId,
ref: "MessageSchema",
},
],
participants: [
{
type:Schema.Types.ObjectId,
ref: "UserSchema",
}
]
},
{
timestamps: true,
}
);
我有两个用户名“A”和“B”,我想查询这两个用户的常用聊天记录。知道怎么做吗?
用户架构
const UserSchema = new Schema<IUserSchema>(
{
username: {
type: String,
required: true,
unique:true,
},
},
{
timestamps: true,
}
);
我尝试了这种方法,但没有奏效。
let chat = await Chats.find({
participants: { $elemMatch: { username: usernames } },
})
我也试过了
let chat = await Chats.find({
"participants.username": { $all: usernames },
})
【问题讨论】:
标签: node.js mongodb mongoose mongodb-query