Quantcast
Channel: Spring Community Forums - NoSQL
Viewing all articles
Browse latest Browse all 128

MongoDb compare two fields (Aggregation)

$
0
0
Hi everyone,

I am trying to compare two fields from the same document.
In MongoDb you can deal with that using two methods:

1)
db.T.find( { $where: function() { return this.Grade1 > this.Grade2 } } );

so you can use that method in Spring Data using a BasicQuery
However it is not very elegant and with big collections it can become slow to execute.

2) Using aggregation framework.
with something like that:

db.test.aggregate([
{$project: {
// All your other fields here
cmp_value: {$cmp: ['$a', '$b']}
}},
{$match: {cmp_value: {$gt: 0}}}
])

I already use Aggregation in Spring Data for other query (count, sum, ...)
but I am unable to make this one...

I think it misses the "$cmp" in Spring Data or maybe I did not well searched...

Does anyone have a solution ?

Thanks,
Tom

Viewing all articles
Browse latest Browse all 128

Trending Articles