Considering that I have a collection of documents like below and I want to get information returned as follows:
Select all the entries from a campaign with campaignID = 12
, sort by entries.questionscorrect
in descending order with a limit of 10.
I have made a stab at a number of queries but I seem to be getting stuck on the fact that I am selecting on one level but want to order on a lower level i.e. property.
Here's what I have so far:
db.getCollection('main').find({"id":4}, {"entries": 1}).sort({"questionscorrect": -1}).limit(2)
How can I write this in either Go (Mgo) syntax or straight MongoDB query please?
I keep getting returned information but sorted on the number of
{
"_id": ObjectId("57f4a590a4be269aa54a0505"),
"campaignID": 12,
"name": "name-here",
"description": "description-here",
"entries": [{
"id": 1,
"nickname": "conorh",
"name": "conor h***",
"email": "ch@gmail.com",
"agreeTerms": true,
"optInMarketing": false,
"questionscnswered": 10,
"questionscorrect": 3
}, {
"id": 2,
"nickname": "bobs",
"name": "bob smyth",
"email": "bs12121@gmail.com",
"agreeTerms": true,
"optInMarketing": false,
"questionscnswered": 10,
"questionscorrect": 6
}, {
"id": 3,
"nickname": "jd12",
"name": "jane doe",
"email": "janedough@gmail.com",
"agreeTerms": true,
"optInMarketing": false,
"questionscnswered": 10,
"questionscorrect": 1
}]
}