搜尋

首頁  >  問答  >  主體

MongoDB找出最大值為什麼得到的是null

資料庫大概是這個樣子:

{
    "_id" : ObjectId("57d248bd43be573cb0530d00"),
    "url" : "www.baidu.com",
    "shorterUrlNum" : 1
}

/* 2 */
{
    "_id" : ObjectId("57d248d843be573cb0530d01"),
    "url" : "www.baidu.com",
    "shorterUrlNum" : 1
}

/* 3 */
{
    "_id" : ObjectId("57d2490d01ddf93cb4723f8b"),
    "url" : "www.baidu.com",
    "shorterUrlNum" : 1
}

/* 4 */
{
    "_id" : ObjectId("57d24ad4a8305e3cc9851d6d"),
    "url" : "www.baidu.com",
    "shorterUrlNum" : 1
}

使用指令

db.getCollection('sites').aggregate([
                {
                    $group: {
                        _id: 'max',
                        max: {'$max': '$shorterUrl'}
                    }
                }])

為什麼得到的是null這個最大值?

{
    "_id" : "max",
    "max" : null
}
我想大声告诉你我想大声告诉你2836 天前587

全部回覆(1)我來回復

  • 怪我咯

    怪我咯2017-05-02 09:24:17

    你的 document 裡根本沒有 $shorterUrl 這個字段啊。

    回覆
    0
  • 取消回覆