[英]Update collection based on data in another collection in MongoDB
I have two MongoDB collections. 我有两个MongoDB集合。
questions
: questions
:
{
"_id" : "8735574",
"title" : "...",
"owner" : {
"user_id" : 950690
},
}
{
"_id" : "8736808",
"title" : "...",
"owner" : {
"user_id" : 657258
},
}
and users
: 和
users
:
{
"_id" : 950690,
"updated" : SomeDate,
...
}
{
"_id" : 657258,
"updated" : SomeDate,
...
}
The entries in users
have to be regularily created or updated based on questions
. 必须根据
questions
定期创建或更新users
的条目。 So I would like to get all the user_id
s from questions
that either do not have an entry in users
at all or their entry in users
was updated
more than eg one day ago. 所以,我想获得的所有
user_id
从s questions
,要么没有在入门users
在其全部或入门users
在updated
前一天比如多。
To achieve this, I could read all user_id
s from questions
and then manually drop all users from the result that do not have to be updated. 为此,我可以从
questions
读取所有user_id
,然后从结果中手动删除所有不必更新的用户。 But this seems to be a bad solution as it reads a lot of unneccessary data. 但这似乎是一个糟糕的解决方案,因为它读取了大量不必要的数据。 Is there a way to solve this differently?
有没有办法以不同的方式解决这个问题? Some kind of collection join would be great but I know that this does not (really) exist in MongoDB.
某种类型的集合连接会很棒,但我知道这在MongoDB中并不存在。 Any suggestions?
有什么建议么?
PS: Nesting these collections into a single collection is no solution as users
has to be referenced from elsewhere as well. PS:将这些集合嵌套到单个集合中并不是解决方案,因为
users
也必须从其他地方引用。
Unfortunately there is no good way of doing this and since you don't have access to the indexes to able to do this client side without reading out all the data and manually manipulating it, it is the only way. 不幸的是,没有好办法做到这一点,因为你无法访问索引以便能够在不读出所有数据并手动操作它的情况下执行此客户端,这是唯一的方法。
The join from users to questions could be done by querying the users collection and then doing an $in
on the questions collection but that's really the only optimisation that can be made. 从用户到问题的连接可以通过查询用户集合然后在问题集合上进行
$in
来完成,但这确实是唯一可以进行的优化。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.