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); }); 

see answer.


Comments

Popular posts from this blog

node.js - Using Node without global install -

How to access a php class file from PHPFox framework into javascript code written in simple HTML file? -

java - Null response to php query in android, even though php works properly -