Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@lastid
Last active December 23, 2015 11:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lastid/6630739 to your computer and use it in GitHub Desktop.
Save lastid/6630739 to your computer and use it in GitHub Desktop.
How to get the best 2 comment or reply with only 1 request to mongodb Expected result: [ {_id:'c1', text: 'Comment 1', votes: 3, type: 'c'}, {_id: 'r1', text: 'Reply 1', votes: 2, type: 'r'} ]
db.playground.insert(
{
_id: 'c1',
text: 'Comment 1',
replies: [
{_id: 'r1', text: 'Reply 1'}
],
votes: [
{ type : 'c', _id: 'c1', v: 3},
{ type : 'r', _id: 'r1', v: 2}
]
}
)
db.playground.insert(
{
_id: 'c2',
text: 'Comment 2',
votes: [
{ type : "c", _id: 'c2', v: 1},
]
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment