17

Relatively Simple Scenario:

I have this Voucher object which has a user property (of type ObjectId). I want to get the sum of all voucher values for a single user. Here is my current strategy, which returns an empty array:

Voucher.aggregate [
    { $match : { user : new ObjectId(user_id), expires : { $gt : new Date() } } }
    { $group : { _id : null, sum : { $sum : '$value' } } }
], (err, result)->

    console.log err
    console.log result

Removing the match for the user id, and leaving the expires field will return results. So the question becomes what is wrong with the match on user?

5
  • I can't reproduce this; $match works for me with an ObjectId in a plain mongo shell. I don't know what the dc in your code block is. Is that a Mongoose thing?
    – Doug Paul
    May 1, 2013 at 4:05
  • 1
    To be specific, here's what I did in the mongo shell: In my test database, I did db.tmp.save({foo: ObjectId()}). Then after finding its ID with a findOne, I did db.users.aggregate({$match:{foo:ObjectId("5180956a55064fce4d42f02d")}}) and the document was returned in the results. Can you try this, and then try reducing your question to a minimum example of the issue? I'm using Mongo 2.4.3, by he way.
    – Doug Paul
    May 1, 2013 at 4:16
  • Hey Doug, thanks for the help. The dc is a context object with some helpers (like Object Id). So please ignore that. I reduced the aggregate to { $match : { user : new ObjectId(user_id) } } and am still receiving a result of []. May 1, 2013 at 4:44
  • Running the same aggregation in mongo shell is working. May 1, 2013 at 4:47
  • Thanks for the help Doug, ultimately led me to find the mistake. May 1, 2013 at 4:55

4 Answers 4

63

Turns out the casting of the ObjectId seemed to be the issue. It was being cast using the Schema type Object Id mongoose.Schema.Types.ObjectId when it needed to be just a pure ObjectId mongoose.Types.ObjectId.

2
  • 1
    +1. I just got bit by this. Thanks for the answer! Aug 15, 2014 at 23:22
  • thanks a bunch.. at least there's still hair left in my head :( you saved me xD
    – jrran90
    Jul 16, 2018 at 8:48
21

When using the find method or findById the query can be only:

var query = {
    $or: [
        {'_sender':req.body._id},
        {'_recipient':req.body._id}
    ]
};

When using aggregate the query need a cast

var mongoose = require('mongoose');
var query = {
    $or: [
        {'_sender':new mongoose.Types.ObjectId(decoded._id)},
        {'_recipient':new mongoose.Types.ObjectId(decoded._id)}
    ]
};
0
12
var aggregate  = [
{
                $match: {
                    'lenderId': new mongoose.Types.ObjectId(req.user),
                    '_disbursed': true
                }
            },
            {
                $unwind:'$emi'
            },
            {
                $match: {
                    'emi._settled': false,
                }
            }
];
0
2

use toString() method.

{ $match : { user : user_id.toString() , expires : { $gt : new Date() } } }

Your Answer

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

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