Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
在 Mongo 中,在更改记录时复制记录的最佳方法是什么?
例如,假设我有一个用户对象,我想在更改某些属性时复制它。所以(逻辑上):
db.users.find({_id:"Fred"}).forEach( function(r) { db.users.insert(r.copy("_id"="Barney","age":25)) } );
当然没有“复制”功能,但这就是我正在寻找的:一种复制文档的方法,同时更改一些属性(当然是键_id)。
您不必复制任何内容,例如这将正常工作:
db.users. find( { _id: "Fred" } ). forEach( function (r) { r._id = "Barney"; r.age = 25; db.so.insert( r ); } );
我不太明白你为什么要这样做。为新的“Barney”文档再次插入包含所有原始字段的新文档会更有意义。