MongoDB copy a field to another collection with a foreign key

岁酱吖の 提交于 2019-12-07 03:59:07

问题


I want to copy over the color from user collection to the Car collection. I'm using a foreign key which is userID.

> db.test1.User.find()
{ "_id" : ObjectId("515f7db83f71d6bcb1c41a48"), "age" : 33, "Color" : "blue" }
{ "_id" : ObjectId("515f7dc03f71d6bcb1c41a49"), "age" : 52, "Color" : "red" }
{ "_id" : ObjectId("515f7dc43f71d6bcb1c41a4a"), "age" : 43, "Color" : "yellow" }


> db.test2.Car.find()
{ "_id" : ObjectId("515f84883f71d6bcb1c41a54"), "speed" : 291, "userID" : ObjectId("515f7db83f71d6bcb1c41a48") }
{ "_id" : ObjectId("515f84883f71d6bcb1c41a55"), "speed" : 202, "userID" : ObjectId("515f7db83f71d6bcb1c41a49") }
{ "_id" : ObjectId("515f84883f71d6bcb1c41a56"), "speed" : 193, "userID" : ObjectId("515f7db83f71d6bcb1c41a4a") }

Here is my query

db.test1.User.find().forEach( 
function(x)
{
  db.test2.Car.update( { userID: x._id }, { $set: { color: x.color} } ) 
} );

I want this result:

> db.test2.Car.find()
{ "_id" : ObjectId("515f84883f71d6bcb1c41a54"), "speed" : 291, "userID" : ObjectId("515f7db83f71d6bcb1c41a48"), "color" : "blue" }
{ "_id" : ObjectId("515f84883f71d6bcb1c41a55"), "speed" : 202, "userID" : ObjectId("515f7db83f71d6bcb1c41a49"), "color" : "red" }
{ "_id" : ObjectId("515f84883f71d6bcb1c41a56"), "speed" : 193, "userID" : ObjectId("515f7db83f71d6bcb1c41a4a"), "color" : "yellow" }

Thanks for your help !


回答1:


There are several issues with your test set up:

  • Case of field names does not match (you are referencing color instead of Color when copying)
  • Only one of the example foreign keys matches in the target collection: ObjectId('515f7db83f71d6bcb1c41a48')
  • Your update will only affect the first matching document for the "foreign key". This would be fine for a 1:1 relationship, but not a 1:many

A corrected example taking the above into account (aside from the non-matching keys):

db.test1.User.find().forEach( 
    function(x) {
        db.test2.Car.update(
            // query 
            { userID: x._id },

            // update 
            { $set: { color: x.Color} },

            // options:
            { "multi" : true } // Update all matching documents
        );
    }
);

Which results in setting {color:blue} for the only foreign key that actually matches in the sample documents:

db.test2.Car.find()
{
    "_id" : ObjectId("515f84883f71d6bcb1c41a55"),
    "speed" : 202,
    "userID" : ObjectId("515f7db83f71d6bcb1c41a49")
}
{
    "_id" : ObjectId("515f84883f71d6bcb1c41a56"),
    "speed" : 193,
    "userID" : ObjectId("515f7db83f71d6bcb1c41a4a")
}
{
    "_id" : ObjectId("515f84883f71d6bcb1c41a54"),
    "color" : "blue",
    "speed" : 291,
    "userID" : ObjectId("515f7db83f71d6bcb1c41a48")
}



回答2:


Here are my two cents: you can drop "function(x)". I prefer the following syntax:

db.source_collection_name.find().forEach(
  doc=>
      db.target_collection_name.update(
        {"_id": doc._id},
        {$set: {"field_to_update": doc.field}}
      )
)

In your case, something like this should work:

db.test1.User.find().forEach(
  doc=> 
    db.test2.Car.update( 
      { "userID": doc._id }, 
      { $set: { "color": doc.Color} } 
    ) 
);


来源:https://stackoverflow.com/questions/15846376/mongodb-copy-a-field-to-another-collection-with-a-foreign-key

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!