Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare mongoose array elements with array

beginner at nodejs and mongoose.

I have a schema like this

schema = new Schema({
    username: String,
    items: [Number]
});

Now, i want to make a call something like

var myItemsArray = [10,5,23,534];
user.find({items: myItemsArray}, function(err, user){});

Basically, i want the call to return the user if it has any of the above items.

Thanks

like image 810
Adrian Coman Avatar asked Dec 24 '22 13:12

Adrian Coman


1 Answers

You need to make use of the $in operator

user.find({items: {$in: myItemsArray}}, function(err, user){
    console.log(err, user);
});
like image 105
ZeMoon Avatar answered Jan 06 '23 12:01

ZeMoon