我有一个大约400gb的mongodb.文档包含各种字段,但这里的关键是一个IDarray.

所以json文件可能是这样的

{
 "name":"bob"
 "dob":"1/1/2011"
 "key":
      [  
       "1020123123",
       "1234123222",
       "5021297723"
      ]
}

这里的焦点变量是"键".5000万个文档中总共有大约100亿个密钥(因此每个文档大约有200个密钥).密钥可以重复,大约有1500万个唯一密钥.

我想做的是返回10000把最常用的 keys .我以为聚合可以做到这一点,但我很难让它运行.这是我的代码:

db.users.aggregate( 
 [ 
  { $unwind : "$key" }, 
  { $group : { _id : "$key", number : { $sum : 1 } } },
  { $sort : { number : -1 } }, 
  { $limit : 10000 }
 ] 
);

知道我做错了什么吗?

推荐答案

试试这个:

db.users.aggregate( 
 [ 
  { $unwind : "$key" }, 
  { $group : { _id : "$key", number : { $sum : 1 } } },
  { $sort : { number : -1 } }, 
  { $limit : 10000 },
  { $out:"result"},
 ], {
  allowDiskUse:true,
  cursor:{}
 }
);

然后在db.result.find()之前找到结果.

Mongodb相关问答推荐

$mod只支持数字类型,不支持MongoDb中的array和int

MongoDB$unionWith,如何 Select 特定文档

MongoDB:如何获取多个$indexOfArray值?

将子元素的数组值提取到 mongodb 中的单个数组中?

避免在 MongoDB 聚合框架中使用 ISODate() 以便管道可以是纯 JSON

在mongoose 中按键查找嵌套对象

没有mongoose 的 Express 和 MongoDB

映射数组导致 mongodb 聚合

将 MongoDB 转移到另一台服务器?

如何在 MongoDB 中存储时间?作为字符串?给出任意年/月/日?

将数据插入 MongoDB - 没有错误,没有插入

将 mongodb 聚合框架结果导出到新集合

Ruby 按键值分组哈希

使用 MongoDB 的 map/reduce 来分组两个字段

MongoDB MapReduce - 发出一个键/一个值不调用reduce

MongoDB 查询:字段不存在或具有特定值

MongoDB $elemMatch $in

MongoDB:聚合框架: $match between fields

show dbs 给出Not Authorized to execute command错误

MongoDB打印两点之间的距离