MongoDb按其他集合中的计数对集合进行排序 [英] MongoDb sort Collection by count in other Collection

查看:94
本文介绍了MongoDb按其他集合中的计数对集合进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个集合:collection Acollection B.集合B"A_id"将具有集合A的_id(有点像一种关系).

I have two collections: collection A and collection B. Collection B "A_id" will have the _id from Collection A (kinda like a relationship).

集合A

{
  _id,
  name
}

集合B

{
 _id,
 A_id,
 other_id
}

我需要按照到B集合中A_id的文件重复量对集合A中的文件进行排序(如果可以的话,按计数排序).约束是我们不想在集合A中有一个数组,因为集合B中的文档会增长很多,并且会不断更新.

I need to sort the documents from Collection A by the amount of repetitions of documents to an A_id in Collection B (sort by count if you may). The constraint is that we don't want to have an array in collection A because the documents in B will grow a lot and will be updated constantly.

任何想法如何执行这种排序?

Any idea how to perform this kind of sorting?

非常感谢

推荐答案

有很多方法可以完成您要问的事情.您必须首先了解有关数据的一些知识. B文件将有多少个文件?

There are many ways to do what you're asking. You have to know some things about the data first. How many documents will a B document have?

  • 这似乎是您希望Collection A的item字段的文档字段为B的情况.这有16 MB的限制,但是在大多数情况下都可以正常工作.

  • It looks like a case where you'd want Collection A to have an items field with documents that are B. There is a 16 MB limit for that, but for most cases works just fine.

对A进行计数,并插入B条记录. (即,每次插入B时,您都会$inc在A上输入该字段).然后,您需要在A的count字段上做一个索引,然后执行sort({count: -1})

Keep a count on A with numbers of B records inserted. (Ie every time you insert a B you'd $inc the field on A). You'd then do an index on the count field for A and do sort({count: -1})

这篇关于MongoDb按其他集合中的计数对集合进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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