使用命令将文档中的值复制到mongodb中的另一个文档中



如何在同一集合中复制文档的键的值?

示例:从以下文档到

{
"_id" : "werty4567tfdxc6ytf",
"thumbnail" : "somelargebase64encodedstring",
"other1" : "value"
}
{
"_id" : "sdf4567g67h8njomioh"
// thumbnail in the above document need to go here
"other2": "value" // these values shouldnt be replaced by above doc values
}

使用$merge。

MongoDB Enterprise > db.t.insert({ "_id" : "werty4567tfdxc6ytf", "thumbnail" : "somelargebase64encodedstring" })
WriteResult({ "nInserted" : 1 })
MongoDB Enterprise > db.t.aggregate([{$set:{_id:'sdf4567g67h8njomioh'}}])
{ "_id" : "sdf4567g67h8njomioh", "thumbnail" : "somelargebase64encodedstring" }
MongoDB Enterprise > db.t.aggregate([{$set:{_id:'sdf4567g67h8njomioh'}},{$merge:'t'}])
MongoDB Enterprise > db.t.find()
{ "_id" : "werty4567tfdxc6ytf", "thumbnail" : "somelargebase64encodedstring" }
{ "_id" : "sdf4567g67h8njomioh", "thumbnail" : "somelargebase64encodedstring" }

最新更新