简体   繁体   English

基于MongoDB中另一个集合中的数据更新集合

[英]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在其全部或入门usersupdated前一天比如多。

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.

相关问题 MongoDB:根据另一个集合更新一个集合 - MongoDB: update a collection based on another collection 如何根据MongoDB中的另一个集合更新集合? - How to update a collection based on another collection in MongoDB? 基于另一个集合更新集合的字段 - MongoDB - Update field of a collection based on another collection - MongoDB 根据MongoDB中另一个集合中的数据查询一个集合中的数据 - Query data in one collection based on data in another collection in MongoDB 如何根据存储在另一个集合中的数据在 MongoDB 集合中搜索文本? - How to search for text in a MongoDB collection based on data stored in another collection? 根据另一个搜索MongoDB集合 - Searching a MongoDB collection based on another MongoDB更新集合的数据 - MongoDB update collection's data 如何基于MongoDB中的另一个集合嵌入字段值更新嵌入集合字段的数组? - How to update array of embedded collection field based on another collection embedded field value in MongoDB? 当某些数据从另一个集合中删除但两个集合都在同一集群下时更新 MongoDB 集合中的数据 - Update data in a MongoDB collection when certain data's gets deleted from another collection but both collection under same cluster 根据Mongodb中集合中的dob更新年龄 - Update age based on dob in a collection in Mongodb
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM