ユーザー コレクションの色を Car コレクションにコピーしたいと考えています。ユーザーIDである外部キーを使用しています。
> 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") }
これが私のクエリです
db.test1.User.find().forEach(
function(x)
{
db.test2.Car.update( { userID: x._id }, { $set: { color: x.color} } )
} );
この結果が欲しい:
> 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" }
ご協力いただきありがとうございます !