I have a collection that I would like to request as follows:
- return all documents
- up to 2 comments (e.g. snippet, 0, 1 or 2 comments)
- All comments must have submissions> 10
It seems that I need to create a function to evaluate each document separately, but it is not clear how this is done, especially considering that I want to cut and return up to n elements that meet these criteria.
Example circuit:
{ title: "One", comments: [ { title: "comment1", views: 9 }, { title: "comment2", views: 10 }, { title: "comment3", views: 11 }, { title: "comment4", views: 12 }, ] }
I want to do something like:
db.collection.find({"comments.views": {$gt: 10}}, {comments:{$slice: 2}})
But this returns any document with a comment s> 10 views, and then slices 2 comments ... I want to return those comments that have> 10 elements. I can not do this on the client And use $ slice without losing some comments, so I need to do this on the DB. Thoughts?
mongodb
killermonkeys
source share