Below is my Schema design
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 ]
}
})
I want to build a query, which queries in all of the nested arrays(i.e acceptedList, requestedList, pendingList) and find out whether id is present or not and update the requestedList if id is not present in any of the list.
Any of the technique is helpful. Performance is key thing.
You can use the $or
operator to do that :
db.Collection.update({
$or: {
"list.acceptedList": {$nin: [your_id]},
"list.requestedList": {$nin: [your_id]},
"list.pendingList": {$nin: [your_id]}
}
}, {
$addToSet: {
"list.requestedList": your_id
}
});
The $nin
operator with [your_id] check if your id is not in the 3 arrays, you must use [] because it allows only arrays.