下面是我的模式设计
const Schema1 = mongoose.Schema({
_id: false,
id: { type: mongoose.Schema.Types.ObjectId,
ref: 'UserinfoSchema'
},
name: { type: String , default: null },
from: { type: Date, default: Date.now }
})
const ProfileSchema = mongoose.Schema({
profilename:{
type: String,
required: true
},
list:{
acceptedList:[ Schema1 ],
requestedList:[ Schema1 ],
pendingList:[ Schema1 ]
}
})
我想建立一个查询,该查询在所有嵌套的数组(即acceptedList,requestedList,pendingList)中进行查询,并找出id是否存在,如果id在任何列表中都不存在,则更新请求的列表。
任何一种技术都是有帮助的。性能是关键。
您可以使用$or
运算符来做到这一点:
db.Collection.update({
$or: {
"list.acceptedList": {$nin: [your_id]},
"list.requestedList": {$nin: [your_id]},
"list.pendingList": {$nin: [your_id]}
}
}, {
$addToSet: {
"list.requestedList": your_id
}
});
$nin
带有[your_id]的运算符检查您的ID是否不在3个数组中,因此必须使用[],因为它仅允许使用数组。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句