MongoDB shell脚本按投影顺序选择字段

时间:2016-08-03 10:00:51

标签: mongodb shell mongodb-query

我正在尝试提出一个查询,该查询可以按照与我的投影查询相同的顺序返回文档的字段。

我的MongoDB 外壳查询

db.Tickets.find({ "ProjectID" : 70 } }, { 
    "_id" : 0, 
    "TicketProjectID" : 1, 
    "ProjectName" : 1, 
    "TicketID" : 1, 
    "Name" : 1, 
    "RequestorName" : 1, 
    "Category" : 1, 
    "Type" : 1, 
    "AssignedTo" : 1, 
    "Priority" : 1, 
    "Status" : 1, 
    "Contributors" : 1, 
    "DueDate" : 1, 
    "MonthCreated" : 1, 
    "CreatedDate" : 1 }).sort({ "CreatedDate" : -1 })

结果:

{
    "TicketProjectID" : 999,
    "ProjectName" : "Apps",
    "TicketID" : xxxx,
    "Name" : "Namee",
    "Status" : "Ready for QA",
    "Priority" : "3 - Normal",
    "Type" : "Bug",
    "RequestorName" : "Req Name",
    "AssignedTo" : "HaBo",
    "CreatedDate" : ISODate("2016-02-10T07:08:59.317Z"),
    "DueDate" : ISODate("2016-02-20T08:00:00.000Z"),
    "Category" : "Some Cat",
    "Contributors" : null,
    "MonthCreated" : "2016 - February"
}

如何以预测的相同顺序获得结果。

预期结果:

 {
        "TicketProjectID" : 999,
        "ProjectName" : "Apps",
        "TicketID" : xxxx,
        "Name" : "Namee",
        "RequestorName" : "Req Name",
        "Category" : "Some Cat",
        "Type" : "Bug",
        "AssignedTo" : "HaBo",
        "Priority" : "3 - Normal",
        "Status" : "Ready for QA",
        "Contributors" : null,
        "DueDate" : ISODate("2016-02-20T08:00:00.000Z"),
        "MonthCreated" : "2016 - February",
        "CreatedDate" : ISODate("2016-02-10T07:08:59.317Z")
    }

另外在我的查询中,如何设置日期字段的格式以返回人类可读的格式化日期

"DueDate" : ISODate("2016-02-20T08:00:00.000Z") as
"DueDate" : "2016-02-20 08:00"

0 个答案:

没有答案