Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Aggregation query in Mongo

Tags:

mongodb

Is there any way in which I can tell mongo to return documents till some length is reached. For e.g I have collection of audio file and in the collection as

audio : {
        name: string;
        length: long;
        releaseDate: long
}

Can I query as return all audio in a list such that, it is sorted by release date and
200>= sum(length) >= 100 [sum of length of audio file in the list]

like image 439
Global Warrior Avatar asked Jun 19 '26 14:06

Global Warrior


1 Answers

The easiest way to do this in Mongo is to have your client-side program manually iterate through a cursor, and stop when the condition is completed. Here's an example using the mongo shell:

cursor = db.audio.find().sort( { releaseDate: -1 } )

var len = 0
while ( (len < 100) && cursor.hasNext() ) {
    record = cursor.next()
    len += record.length
    printjson(record)
}

I hope this helps!

like image 64
William Z Avatar answered Jun 24 '26 19:06

William Z



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!