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:
- Created 12 years ago
- Comments:6 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Okey, here another example:
here is schema:
I want check, is document with id containes name == ‘foo’.
for this, i do
If the id is of type String, the search result is empty. For this to work, you need to do this:
But, if i don’t use $and
all works great.
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