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

浏览:75日期:2023-06-22

问题描述

数据库大概是这个样子:

{ '_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}

问题解答

回答1:

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

相关文章: