160

Recently I start using MongoDB with Mongoose on Nodejs.

When I use Model.find method with $or condition and _id field, Mongoose does not work properly.

This does not work:

User.find({
  $or: [
    { '_id': param },
    { 'name': param },
    { 'nickname': param }
  ]
}, function(err, docs) {
   if(!err) res.send(docs);
});

By the way, if I remove the '_id' part, this DOES work!

User.find({
  $or: [
    { 'name': param },
    { 'nickname': param }
  ]
}, function(err, docs) {
   if(!err) res.send(docs);
});

And in MongoDB shell, both work properly.

3 Answers 3

278

I solved it through googling:

var ObjectId = require('mongoose').Types.ObjectId;
var objId = new ObjectId( (param.length < 12) ? "123456789012" : param );
// You should make string 'param' as ObjectId type. To avoid exception, 
// the 'param' must consist of more than 12 characters.

User.find( { $or:[ {'_id':objId}, {'name':param}, {'nickname':param} ]}, 
  function(err,docs){
    if(!err) res.send(docs);
});
6
  • 4
    can you describe why this solution works with words? thanks Nov 22, 2015 at 23:21
  • This looks like a solution to a rather specific issue. You may have to keep searching.
    – Kesarion
    Apr 15, 2016 at 15:17
  • Could you provide your reference? Why in this case param has to consist more than 12 characters? Is this specific to your problem or the requirement of ObjectId()? What if my param doesn't have 12 characters? Thanks!
    – yusong
    Dec 2, 2016 at 20:13
  • 8
    you can also check ObjectId as follow: const mongoose = require('mongoose'); mongoose.Types.ObjectId.isValid(objectidtocheck)
    – Orhan
    Jun 18, 2017 at 2:21
  • @yusong it's due to mongoose will throw an error if it's not a valid ObjectId. A cleaner way to do it mentioned above me. Jun 19, 2017 at 23:22
90

I implore everyone to use Mongoose's query builder language and promises instead of callbacks:

User.find().or([{ name: param }, { nickname: param }])
    .then(users => { /*logic here*/ })
    .catch(error => { /*error logic here*/ })

Read more about Mongoose Queries.

3
  • Love it! Thanks for the heads up!
    – zeckdude
    Apr 29, 2020 at 7:31
  • Why is chaining methods like or() onto find() better than using operators like { $or: [] } in a regular JSON filter? Is this way faster? Is one just syntactic sugar for another?
    – Benji
    Dec 20, 2022 at 13:04
  • Yes, the methods are syntactic sugar. It's a matter of preference. For me they sometimes read better. Other times, they make things less readable. I would say to stick with whatever makes more sense to you, but stick to one or the other. Or if you're working at a company, it would be best to go with whatever the norm is at your company and stay consistent.
    – Govind Rai
    Dec 21, 2022 at 6:34
6

You can also add a mix of $or and and to give your functions more flexibility, options and robustness, like so:

var ObjectId = require("mongoose").Types.ObjectId;
var idParam = new ObjectId(param.length < 12 ? "123456789012" : param);
const {nameParam, nicknameParam, ageParam} = req.params || req.body || req.query

User.find({
        $or: [{
                _id: objId
            },
            {
                name: nameParam
            },
            {
                nickname: nicknameParam
            }
        ],
        $and: [{
            age: ageParam
        }]
    },
    function (err, docs) {
        if (!err) res.send(docs);
    }
);

So this means that your find() will look for all users where (_id = idParam OR name = nameParam OR nickname = nicknameParam) AND (age = ageParam)

I hope this helps someone out there. Cheers!!!

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.