How to sort by the value of number1 plus number2 in the mongodb -


i prepare objects in mongodb.

{number1:123,number2:321} {number1:222,number2:4532} 

for example, these have in database. now, want them value of number1 + number2.

how ?

one option no 1 has said here use aggregation framework. better using $where since faster , can sort on return , more such:

db.c.aggregate([     {'$project': {sumfield: {$add: ['$number1', '$number2']}}},     {'$sort': {sumfield: 1}} ]); 

like so. better using inbuilt js, plus index , covered query make faster since aggregation, unlike $where can use index.


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -