Hi,
Suppose we have a mongodb collection with 2 columns: id, c
1,2
2,6
3,1
Now I would like to select first few rows where sum of column c is greater or equal than C
It looks impossible to such query in mongodb for me. I’d appreciate if anybody have any idea.
What is the use case that you are trying to solve?
My understanding is that your requirement implies that each document is not a separate entity, but depends upon all other documents with a lower id
. Is this correct?
If this is correct, then I am not aware if there is any built-in feature in MongoDB that lets you create this kind of relationship between rows like you described.
Having said that, if applicable, you could potentially add an additional field (e.g. sum
) that pre-calculates the cumulative sum of the column c
of the current document and all previous documents. Using your example:
> db.test.insert({_id: 1, c: 2, sum: 2})
> db.test.insert({_id: 2, c: 6, sum: 8})
> db.test.insert({_id: 3, c: 1, sum: 9})
and you could satisfy one of your example queries like (in the mongo
shell):
> var maxid = db.test.find({sum: 8}).next()._id // find the maximum _id having the required sum
> db.test.find({_id: {$lte: maxid}})
{"_id": 1, "c": 2, "sum": 2}
{"_id": 2, "c": 6, "sum": 8}
or in one line:
> db.test.find({ _id: { $lte: db.test.find({sum:8}).next()._id } })
{"_id": 1, "c": 2, "sum": 2}
{"_id": 2, "c": 6, "sum": 8}
Please note that for this method to be performant, documents should only be added to the end of the collection (i.e. having a higher _id
compared to the last inserted document). Otherwise, the sum
field must be recalculated for the whole collection.
Best regards,
Kevin