0

I'm trying to use aggregate to get some specific results by a ObjectId field and a Date field. When i use only ObjectId to match the results, it worked correctly, but after added Date conditions, it just returns nothing. Here is my aggregate codes:

FlowerAssignment.aggregate({
            $match: {
                class: new MongoTypes.ObjectId(current_class),
                time: {
                    $lte: moment().startOf('month').toDate(),
                    $gte: moment().endOf('month').toDate()
                }
            }
        })
        .project({
            student: 1,
            name: '$studentInfo.name',
            avatar: '$studentInfo.avatar',
            flowerInfo: 1
        }).group({
            _id: '$student',
            name: {
                $first: '$name'
            },
            avatar: {
                $first: '$avatar'
            },
            flowers: {
                $sum: 1
            }
        }).exec(function (err, results) {});

What's wrong with the match conditions...I've really got no ideas....

2
  • Have you tried debugging the pipeline by removing the class: new MongoTypes.ObjectId(current_class) condition in the query and just try the query with the time range only? Can you also show a sample document with some data and the expected output? Commented Jun 5, 2015 at 8:22
  • 1
    Thanks. I just followed the answer by JohnnyHK, checked my time range, and solved it. I made an unbelievable mistake with my jammed mind. Commented Jun 5, 2015 at 12:51

1 Answer 1

1

Your $match object contains mutually exclusive terms as, unless it's an array, the time field of a document can't simultaneously be both before the start of the month and after the end of the month.

I'm assuming you've got those backwards and it should be:

$match: {
    class: new MongoTypes.ObjectId(current_class),
    time: {
        $gte: moment().startOf('month').toDate(),
        $lte: moment().endOf('month').toDate()
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.