Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7805929
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T02:22:09+00:00 2026-06-02T02:22:09+00:00

I am using Nodejs , mongodb database. We can use $nin like this Model.find({

  • 0

I am using Nodejs, mongodb database. We can use $nin like this

Model.find({ uname : { $nin : ["sachin","saurav"] } }....

above words for normals elements like uname and others. But for object ids(_id), ..

Model.find({_id : {$nin : ["6534767457dfgbyb23","wtvwt3wy5etvdh"] } } ...

above line not giving error, it is showing correctly..

var ObjectID = require('mongodb').ObjectID;
var a = new ObjectID("sdfsdznfsdz");
var b=new ObjectID("sdfjwneufhq2rfwefsd");
Model.find({_id : { $nin : [a,b] } }...

above also not giving error…

The problem is, I cant write manually like a,b,c,d…

I have to store all those a,b,c,d… in some variable in some correct format, and have to do like this

Model.find({_id : {$nin : variable } }

or

Model.find({_id : {$nin : [variable] } }

I tried this

var string = a+","+b //this didnt work, error : invalid object id
var string = "nfw34qfhrs9"+","+"u89tgngnfdb"  //this also same error
var string = "\"jn4tr43r\""  +  "," + "\"ansfuw37fe\""  //this also same error 

What should I do? the thing is, I should get all items except those items with those _ids.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T02:22:11+00:00Added an answer on June 2, 2026 at 2:22 am

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Recently I start using MongoDB with Mongoose on Nodejs. When I use Model.find method
I am using mongoose/nodejs to get data as json from mongodb. For using mongoose
I'm using Mongoose, MongoDB, and Node.js. I would like to define a schema where
I just installed mongodb from Synaptic (Using ubuntu 11.10). I'm following this tutorial: http://howtonode.org/express-mongodb
I'm currently trying to make a register form using mongoDB and nodeJS - I've
I'm working on developing a node.js app using mongoDB as a database. I'm working
I'm using nodejs 0.4.7 to make the request, this is my code: var post_data
I'm currently building a webserver in nodeJS that'll be using mongoDB. One of the
I just wrote my first nodejs program using the node-mongodb-native driver. I used the
Is there any way to use dynamic keys with the node-mongodb-native driver? By this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.