48

Document:

{
    "_id" : ObjectId("560c24b853b558856ef193a3"),
    "name" : "Karl Morrison",
    "pic" : "",
    "language" : ObjectId("560c24b853b558856ef193a2"),
    "cell" : 1,
    "local" : {
        "email" : "[email protected]",
        "password" : "12345"
    },
    "sessions" : [
        {
            "id" : ObjectId("560c24b853b558856ef193a5")
        }
    ]
}

This works:

yield new Promise(function (resolve, reject) {
                users.col.aggregate([
                        {
                            $match: {
                                'name': 'Karl Morrison'
                            }
                        }
                    ],
                    function (err, res) {
                        console.log('err ' + err);
                        console.log('res ' + JSON.stringify(res)); // <-- echos the object retrieved
                        if (err === null)
                            resolve(res);
                        reject(err);
                    });
            });

This does not work:

yield new Promise(function (resolve, reject) {
                users.col.aggregate([
                        {
                            $match: {
                                '_id': '560c24b853b558856ef193a3' // <-- _id of the user
                            }
                        }
                    ],
                    function (err, res) {
                        console.log('err ' + err);
                        console.log('res ' + JSON.stringify(res));
                        if (err === null)
                            resolve(res);
                        reject(err);
                    });
            });

The .col access the native mongodb object (using co-monk otherwise). So I'm doing it manually. This however isn't working. I suspect I am not casting the id hexstring to an ObjectId. No matter what I try nothing works.

3
  • 1
    The _id needs to be an ObjectId. Monk cannot cast the type for you from a string when used with .aggregate(), so you need to cast the type yourself. Sep 30, 2015 at 23:53
  • 3
    Possible duplicate of MongoDB - Aggregate $match on ObjectId. Basic same reason which is "cast it yourself", as both frameworks use similar code for auto-casting, but none of it applies to .aggregate(). Sep 30, 2015 at 23:57
  • @BlakesSeven Indeed it is, thank you, been looking for ages (didn't find it due to mongoose probably)
    – basickarl
    Sep 30, 2015 at 23:57

4 Answers 4

113
const ObjectId = mongoose.Types.ObjectId;
const User = mongoose.model('User')

User.aggregate([
  {
    $match: { _id: ObjectId('560c24b853b558856ef193a3') }
  }
])
0
17

Try this

const User = require('User')
const mongoose = require("mongoose");


User.aggregate([
  {
    $match: { _id: new mongoose.Types.ObjectId('560c24b853b558856ef193a3') }
  }
])
1
  • Why 'new' keyword here? Is there not a better way to do? Sep 11 at 13:01
0

You could try with $oid for older Mongos versions.

{ $match: { '_id' : {"$oid": '560c24b853b558856ef193a3' } } }

Greetings.

-5
const User = mongoose.model('User')
User.aggregate([
  {
    $match: { _id: new mongoose.Types.ObjectId('id') }
  }
])
1
  • 1
    Isn't this the opposite of the correct way of doing this? May 14, 2019 at 22:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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