Global Warrior
Global Warrior

Reputation: 5130

Aggregation query in Mongo

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]

Upvotes: 3

Views: 555

Answers (1)

William Z
William Z

Reputation: 11129

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!

Upvotes: 7

Related Questions