Let's say I have a model called User
. I have an array with object Ids.
I want to get all User records that "intersect" with the array of Ids that I have.
User.find({ records with IDS IN [3225, 623423, 6645345] }, function....
Mongoose | findById() Function The findById() function is used to find a single document by its _id field. The _id field is cast based on the Schema before sending the command.
The value of the $in operator is an array that contains few values. The document will be matched where the value of the breed field matches any one of the values inside the array.
From the documentation: Mongoose assigns each of your schemas an id virtual getter by default which returns the documents _id field cast to a string, or in the case of ObjectIds, its hexString.
find returns an array always.
Here is a mongoosey way to use the $in operator.
User.find() .where('fb.id') .in([3225, 623423, 6645345]) .exec(function (err, records) { //make magic happen });
I find the dot notation quite handy for querying into sub documents.
http://mongoosejs.com/docs/queries.html
You need to use the $in operator >
https://docs.mongodb.com/manual/reference/operator/query/in/#op._S_in
For example:
Users.find( { "fb" : { id: { $in : arrayOfIds } } }, callback );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With