在数组对象中按日期排序mongo db集合

时间:2018-02-13 16:49:15

标签: arrays mongodb sorting find

我正在尝试查询集合并按此排序: 首先我获得所有匹配字符串数组的文档,然后我必须通过评级属性对它们进行排序,然后当我有两个具有相同速率的文档时,我必须在最后一个评论的日期排序它们。

我会试着更清楚,

这是集合中文档的json示例:

{
"_id" : ObjectId("5703decca27819000000"),
"__v" : 0,
"tokens" : null,
"comments" : [ 
    {
        "owner" : "name.surname",
        "created" : ISODate("2016-05-02T18:44:38.000Z"),
        "comment" : "RA visto da posta"
    }, 
    {
        "owner" : "name.surname",
        "created" : ISODate("2016-05-02T18:44:38.000Z"),
        "comment" : "visto "
    }
],
"attachments" : [],
"status" : {
    "tag" : "tp.st.resource.edited",
    "owner" : "name.surname",
    "date" : ISODate("2016-05-03T07:25:55.000Z"),
    "visibility" : [ 
        "recruiter", 
        "hunter"
    ],
    "extra" : null,
    "rating" : 4
},
"order" : 70020160503.0,
"zone" : "",
"letter" : "",
"created" : ISODate("2016-04-05T15:50:36.000Z"),
"phones" : [ 
    {
        "number" : "555555555",
        "type" : "main"
    }
],
"email" : "email@email.com",
"profileType" : "Tecnico/Sistemista/Windows",
"skills" : [ 
    {
        "id" : 39,
        "value" : 2,
        "name" : "SQL"
    }, 
    {
        "id" : 79,
        "value" : 2,
        "name" : "Oracle"
    }, 
    {
        "id" : 80,
        "value" : 3,
        "name" : "Access"
    }, 
    {
        "id" : 89,
        "value" : 2,
        "name" : "SQLite"
    }, 
    {
        "id" : 127,
        "value" : 3,
        "name" : "Windows"
    }
],
"webSites" : [],
"birth" : {
    "date" : ISODate("1962-06-19T22:00:00.000Z"),
    "place" : null
},
"surname" : "John",
"name" : "Doe"

}

所以到目前为止我的查询是:

db.getCollection('resources')。find({'skills.name':{$ all:[“Java”,“Python”]}})。sort({'status.rating': - 1} )

现在我拥有所有具有“Java”和“Python”技能的“资源”,按status.rating排序。现在我需要按照最后评论的日期对它们进行排序。

是谁愿意帮忙?

0 个答案:

没有答案