regex - Select from mongoDB using Mongoose Where ObjectId NOT EQUAL to a particular ID -
please how go retrieving records mongodb using mongoose model id doesn't match given id. model setup properly. not entirely new node.js, mongodb, mongoose.
i tried using $ne:
var id = .... user.find({id: {$ne: id}}, function(error, users) { console.log(users); callback(error, count); }); i tried using regexp:
var regex = new regexp('^((?!' + id + ').)*$', "i"); user.find({id: regex}, function(error, users) { console.log(users); callback(error, users); });
as johnnyhk , aaron dufour have commented, need use _id instead of id.
further, mongo expecting object , not string when query _id.
you can require native mongodb drivers objectid , use in code this:
var objectid = require('mongodb').objectid; _id = new objectid('stringid'); user.find({'_id': {$ne: _id}}, function(error, users) { console.log(users); callback(error, count); });
Comments
Post a Comment