这是我的 Mongoose 对话对象模型:
const conversationSchema = mongoose.Schema({
participants: [
{
id_profile: { type: mongoose.Schema.Types.ObjectId, ref: 'User', required: true }
}
]
});
如何找到一个对话对象,该对象包含数组“参与者”中 ID = 123456 和 ID = 654321 的两个对象?数组“participants”中对象的顺序可以不同。
conversation = {
participants: [
{ id_profile: '123456' },
{ id_profile: '654321' }
]
}
conversation = {
participants: [
{ id_profile: '654321' },
{ id_profile: '123456' }
]
}
如何使用 Mongoose 创建查询?
最佳答案
您可以使用 $all
进行此查询,它会查找字段与所有一组值匹配的文档:
Conversation.find({'participants.id_profile': {$all: ['123456', '654321']}})
关于node.js - 使用 Mongoose 库创建查询(查找数组中的对象),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59412926/