My approach would be to get the document instance, and create a new one from the instance fields. I am sure there is a better way to do it.
相关问题
- MongoDB can not create unique sparse index (duplic
- Spring Data MongoDB - lazy access to some fields
- Golang mongodb aggregation
- How to convert from Timestamp to Mongo ObjectID
- MongoDB Indexing: Multiple single-field vs single
相关文章
- mongodb有没有什么办法禁止读取数据的时候进行缓存
- mongodb-aggregate聚合查询分组后如何获得多字段
- mongodb error: how do I make sure that your journa
- How to track MongoDB requests from a console appli
- MongoError: cannot infer query fields to set, path
- Pymongo $in Query Not Working
- django.core.exceptions.ImproperlyConfigured: '
- How to represent an array with mixed types
For simply clone use this :
Can you clarify what you mean by "copy/clone"? Are you going trying to create a duplicate document in the database? Or are you just trying to have two
var
s in your program that have duplicate data?If you just do:
In this case,
x
andy
will be two "copies" of the same document within your program.Alternatively, if you wanted to insert a new copy of the doc into the database (though with a different
_id
I assume), that would look like this:Or if you're doing this from the outset, aka you created some document
d1
, you can just callsave
twice without setting the_id
:There will now be two documents with differing
_id
's and all other fields identical in your database.Does this clarify things a bit?
The following code to clone documents in Amelia's response above does not work:
The following code to clone documents: