0

I've got a very basic Mongoose aggregate query

Transaction.aggregate().match({
    timestamp : {
        $gt : time_lower_bound,
        $lt : time_upper_bound
    }
}).group({
    _id : '$currencyFrom',
    count : {
        $sum : 1
    }
}).sort({
    count : -1
})

I wanted to make the _id dynamic. But i tried

var field = job.data.field;
Transaction.aggregate().match({
    timestamp : {
        $gt : time_lower_bound,
        $lt : time_upper_bound
    }
}).group({
    _id : '$' . field,
    count : {
        $sum : 1
    }
}).sort({
    count : -1
})

and it doesn't return the correct data, it returns

[ { _id: null, count: 473 } ]

so i'm missing something obvious here.

0

1 Answer 1

2

Javascript uses plus signs for concatenation, not periods.

Assuming field is a string like currencyFrom, replace

_id : '$' . field,

with

_id : '$' + field,
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.