question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Model.find, $and operator doesn't work with _id

See original GitHub issue

$and operator doesn’t work with _id

When

 Model.find({_id: someId}, function () {...});

All works well, but if you do this

 Model.find({$and: [{_id: someId}]}, function () {...});

Document will not be found.

someId - is string, like: 4e9599df12049f440f001404

Issue Analytics

  • State:closed
  • Created 12 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
zxcabscommented, Nov 11, 2011

Okey, here another example:

here is schema:

new Schema({
    name: { type:String, default:'foo' }
});

I want check, is document with id containes name == ‘foo’.

for this, i do

TestModel.find({$and: [{_id: id}, {name: 'foo'}]}, function (err, doc){
    //
});

If the id is of type String, the search result is empty. For this to work, you need to do this:

var ObjectID = mongoose.Types.ObjectId;

TestModel.find({$and: [{_id: new ObjectID(id)}, {name: 'foo'}]}, function (err, doc){
    //
});

But, if i don’t use $and

TestModel.find({ _id: id, name: 'foo' }, function (err, doc){
    //
});

all works great.

1reaction
ekryskicommented, Nov 10, 2011

Your query doesn’t really make sense. What are you anding with? The query should have a second query param to and with. Like this:

Model.find( { $and: [ { _id: someId }, { _id: anotherId } ] }, function(...

or

Model.find( { $and: [ { a: 1 }, { a: { $gt: 5 } } ] } )

You should take a look http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-%24and

Read more comments on GitHub >

github_iconTop Results From Across the Web

Mongoose's find method with $or condition does not work ...
Recently I start using MongoDB with Mongoose on Nodejs. When I use Model. find method with $or condition and _id field, Mongoose does...
Read more >
Optional chaining (?.) - JavaScript - MDN Web Docs - Mozilla
The optional chaining (?.) operator accesses an object's property or calls a function. If the object accessed or function called is ...
Read more >
Model Querying - Basics - Sequelize
The operators Op.and , Op.or and Op.not can be used to create arbitrarily complex nested logical comparisons. Examples with Op.and and Op.or ​....
Read more >
Mongoose v6.8.2: Queries
Executing; Queries are Not Promises; References to other documents ... const Person = mongoose.model('Person', yourSchema); // find each person with a last ...
Read more >
$nin — MongoDB Manual
the field value is not in the specified array or. the field does not exist. If the field holds an array, then the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found