Nodejs,mongodb - using $nin or $in for object ids or _id Nodejs,mongodb - using $nin or $in for object ids or _id mongodb mongodb

Nodejs,mongodb - using $nin or $in for object ids or _id


The way $nin works in mongo is that it takes an array of values to check.

The code:var string = a+","+b Doesnt make this a valid array. as you're creating a string with the value sdfsdznfsdz, u89tgngnfdb

So $nin is treating the array as that value, not the way you're trying to implement.

Solution to do what you want is to create an array with those values.

So something like:

var ids = new Array( new ObjectId("1124edcef241"), new ObjectId("56943decfeca845") );// Dont put [ ] as a parameter. Just send a variable thats an array     Model.find( {_id : { $nin : ids } } ); ...

Just incase you're a tad hazy regarding arrays, I suggest having a read of this:

http://www.w3schools.com/js/js_obj_array.asp


Not able to comment yet, but just wanted to add here that new ObjectID() wasn't working for me, but new ObjectId() (lowercase d) did work. Hope this helps someone.