MongoDB查找最大值为什么得到的是null

查看:133
本文介绍了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
}

解决方案

你的 document 里根本没有 $shorterUrl 这个字段啊。

这篇关于MongoDB查找最大值为什么得到的是null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆