筛选集合,但按内部属性排序

考虑到我有如下所示的文档集合,并且希望获得如下返回的信息:</ p>

从一个广告系列中选择所有 campaignID = 12 </ code>的条目,并按 entries.questionscorrect </ code>的降序排列(最多10个)。</ p >

我在很多查询上都做过刺,但是我似乎陷入了这样一个事实,即我在一个级别上进行选择,但想在一个较低级别(即属性)上进行订购。</ p>

这是我到目前为止的内容:</ p>

db.getCollection('main')。find({“ id”:4},{“ entries “:1})。sort({” questionscorrect“:-1})。limit(2)</ code> </ p>

如何用Go(Mgo)语法或 请直接输入MongoDB查询吗?</ p>

我一直在获取返回的信息,但按</ p>

  {
“ _id”:ObjectId( “ 57f4a590a4be269aa54a0505”),
“ campaignID”:12,
“ name”:“ name-here”,
“ description”:“ description-here”,
“ entr ies“:[{
” id“:1,
”昵称“:” conorh“,
” name“:” conor h ***“,
” email“:” ch@gmail.com“ ,
“ agreeTerms”:true,
“ optInMarketing”:否,
“ questionscnswered”:10,
“ questionscorrect”:3
},{
“ id”:2,
“ nickname” :“ bobs”,
“ name”:“ bob smyth”,
“ email”:“ bs12121@gmail.com”,
“ agreeTerms”:true,
“ optInMarketing”:false,
“ questionscnswered “:10,
”问题正确“:6
},{
” id“:3,
”昵称“:” jd12“,
”名称“:” jane doe“,
”电子邮件“ :“ janedough@gmail.com”,
“ agreeTerms”:true,
“ optInMarketing”:false,
“ questionscnswered”:10,
“ questionscorrect”:1
}]
}
< / code> </ pre>
</ div>

展开原文

原文

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
    }]
}

duanmu2015
duanmu2015 目前尚不清楚预期的结果是什么。你能详细说明一下吗?
3 年多之前 回复

1个回答



尝试以下操作:</ p>

  db.getCollection('main')。aggregate(  [
{$ match:{“ campaignID”:12.0}},
{$ unwind:“ $ entries”},
{$ sort:{“ entries.questionscorrect”:-1}},
{$ 组:{_ id:“ $ _ id”,
“条目”:{$ push:“ $ entries”}}}
])
</ code> </ pre>
</ div>

展开原文

原文

Try below:

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

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问