i am trying to do some queries to my db... but i am new in mongodb and i dont know what operator i have to use, or maybe i just do not know how to do it.
i have a collection like this.
const userSchema= new Schema({
user: { type: String },
object: { type: String }
})
then i receive in my server a query like this
{
users: ['John', 'Michael', 'Peter'],
objects: ['Object1','Object2','Object3','Object4', 'Object5']
}
so.. in my controller i do this to find...
userModel.find({ user: { $in: req.body.users}, object: { $in: req.body.objects})
.then((users)=>{
res.json(users)
})
Ok, this is working... but, in the case that one of the arrays Users or Objects is empty, it doesn't find nothing... so, how i can handle it? there is any operator that can find all if the array is empty or something?
for example.. if the query comes like this.
{
users: ['John', 'Michael', 'Peter'],
objects: []
}
i would like to find the users in my Users array even if i dont receive any object.
Any help? thank you in advance