Filter the collection but sort by internal property


Jim:

Considering that I have a collection of documents like the following, and I would like to get the information returned like this:

Select all items in a campaign campaignID = 12, entries.questionscorrectin descending order (capped at 10).

I've done jabs on a lot of queries, but I seem to be stuck with the fact that I'm selecting on one level, but want to order on a lower level (i.e. attributes).

Here is what I have so far:

db.getCollection('main').find({"id":4}, {"entries": 1}).sort({"questionscorrect": -1}).limit(2)

How can I write this code in Go (Mgo) syntax or direct MongoDB query?

I keep getting the information returned but sorted by quantity

{
    "_id": ObjectId("57f4a590a4be269aa54a0505"),
    "campaignID": 12,
    "name": "name-here",
    "description": "description-here",
    "entries": [{
        "id": 1,
        "nickname": "conorh",
        "name": "conor h***",
        "email": "[email protected]",
        "agreeTerms": true,
        "optInMarketing": false,
        "questionscnswered": 10,
        "questionscorrect": 3
    }, {
        "id": 2,
        "nickname": "bobs",
        "name": "bob smyth",
        "email": "[email protected]",
        "agreeTerms": true,
        "optInMarketing": false,
        "questionscnswered": 10,
        "questionscorrect": 6
    }, {
        "id": 3,
        "nickname": "jd12",
        "name": "jane doe",
        "email": "[email protected]",
        "agreeTerms": true,
        "optInMarketing": false,
        "questionscnswered": 10,
        "questionscorrect": 1
    }]
}
Sachin:

Please try the following methods:

db.getCollection('main').aggregate([
{$match: {"campaignID" : 12.0}},
{$unwind:"$entries"},
{$sort: {"entries.questionscorrect": -1}},
{$group:{_id:"$_id", 
    "entries":{$push:"$entries"}}}
])

Related


Filter the collection but sort by internal property

Jim: Considering that I have a collection of documents like the following, and I would like to get the information returned like this: Select all items in a campaign campaignID = 12, entries.questionscorrectin descending order (capped at 10). I've done jabs on

Filter the collection but sort by internal property

Jim: Considering that I have a collection of documents like the following, and I would like to get the information returned like this: Select all items in a campaign campaignID = 12, entries.questionscorrectin descending order (capped at 10). I've done jabs on

Filter the collection but sort by internal property

Jim: Considering that I have a collection of documents like the following, and I would like to get the information returned like this: Select all items in a campaign campaignID = 12, entries.questionscorrectin descending order (capped at 10). I've done jabs on

Filter the collection but sort by internal property

Jim: Considering that I have a collection of documents like the following, and I would like to get the information returned like this: Select all items in a campaign campaignID = 12, entries.questionscorrectin descending order (capped at 10). I've done jabs on

Filter the collection but sort by internal property

Jim: Considering that I have a collection of documents like the following, and I would like to get the information returned like this: Select all items in a campaign campaignID = 12, entries.questionscorrectin descending order (capped at 10). I've done jabs on

Filter the Collection Navigation property

Matteo I want to filter it based on the "Translations" collection navigation property of the "Translation Sets" entity. E.g If the "Language ID" of a "translation" is 5 ( Italian ), the "TranslationSet" containing this "translation" should be removed from the

Filter the Collection Navigation property

Matteo I want to filter it based on the "Translations" collection navigation property of the "Translation Sets" entity. E.g If the "Language ID" of "Translation" is 5 ( Italian ), the "TranslationSet" containing this "Translation" should be removed from the re

Filter the Collection Navigation property

Matteo I want to filter it based on the "Translations" collection navigation property of the "Translation Sets" entity. E.g If the "Language ID" of "Translation" is 5 ( Italian ), the "TranslationSet" containing this "Translation" should be removed from the re

Filter the Collection Navigation property

Matteo I want to filter it based on the "Translations" collection navigation property of the "Translation Sets" entity. E.g If the "Language ID" of a "translation" is 5 ( Italian ), the "TranslationSet" containing this "translation" should be removed from the

Filter the Collection Navigation property

Matteo I want to filter it based on the "Translations" collection navigation property of the "Translation Sets" entity. E.g If the "Language ID" of a "translation" is 5 ( Italian ), the "TranslationSet" containing this "translation" should be removed from the

Filter/sort collection by object field?

username I'm not sure why this doesn't work. I'm not sure if this is a printing issue or a problem with the method itself. I'm making a program that takes songs and either filters or sorts them based on a given user input. The user should be able to enter mult

Sort dynamic LINQ by collection property

username I am using System.Linq.Dynamic and have the following simplified structure (model): parent -> child (a collection of subtypes) child has propertySortingOrder I want to retrieve a list of "Parents" including "Children" and "Sort by Sort Order". I can d

How to sort by navigation property (collection)

Monem How to sort by navigation property field (entity framework) if the navigation property is a collection I want to sort the employee list by date and the date in the navigation property (collection) steve pie If Trans represents a collection like NameValue

Sort dynamic LINQ by collection property

username I am using System.Linq.Dynamic and have the following simplified structure (model): Parent -> Child (Collection of type Child) child has propertySortingOrder I want to retrieve a list of "Parents" including "Children" and "Sort by Sort Order". I can d

Sort dynamic LINQ by collection property

username I am using System.Linq.Dynamic and have the following simplified structure (model): Parent -> Child (Collection of type Child) child has propertySortingOrder I want to retrieve a list of "Parents" including "Children" and "Sort by Sort Order". I can d

Filter by collection or sub-property

mitch2k: My User model has a "pre-registered" attribute public function prevregistration() { return $this->hasMany(Prevregistration::class, 'prevregistration_userid'); } My prevregistraton model has a 'prev' attribute public function prev() { retur

Filter by collection or sub-property

mitch2k: My User model has a "pre-registered" attribute public function prevregistration() { return $this->hasMany(Prevregistration::class, 'prevregistration_userid'); } My prevregistraton model has a 'prev' attribute public function prev() { retur

Filter by collection or sub-property

mitch2k: My User model has a "pre-registered" attribute public function prevregistration() { return $this->hasMany(Prevregistration::class, 'prevregistration_userid'); } My prevregistraton model has a 'prev' attribute public function prev() { retur