Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 454 Vote(s) - 3.52 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Duplicate a document in MongoDB using a new _id

#1
Ok, I suppose that this is a silly question and probably has a simple answer.

How can I duplicate a document in MongoDB, changing the _id of the new one?

Imaging that you have the original document:

> var orig = db.MyCollection.findOne({_id: 'hi'})

And now I want another document in the collection with _id 'bye'.
Reply

#2
Just change the id and re-insert.

> db.coll.insert({_id: 'hi', val: 1})
> var orig = db.coll.findOne({_id: 'hi'})
> orig._id = 'bye'
bye
> db.coll.insert(orig)
> db.coll.find()
{ "_id" : "hi", "val" : 1 }
{ "_id" : "bye", "val" : 1 }
Reply

#3
You can give a new ObjectId to the copy Document. In mongo shell

var copy = db.collection.findOne();
for (var i = 0; i< 30; i++){
copy._id = new ObjectId();
db.collection.insert(copy);
}
Reply

#4
A little improvement to the @689 response

var copy = db.collection.findOne({},{_id:0});
for (var i = 0; i< 30; i++){
db.collection.insert(copy);
}
Reply

#5
You can use below code :

**Run using single line :**

db.collectionName.find({"_id" : ObjectId("5a4e47e0a21698d455000009")}).forEach(function(doc){var newDoc = doc; delete newDoc._id; db.collectionName.insert(newDoc); })

**a structural format for understanding :**

db.collectionName.find({"_id" : ObjectId("5a4e47e0a21698d455000009")}).forEach(function(doc){
var newDoc = doc;
delete newDoc._id;
db.collectionName.insert(newDoc);
})
Reply

#6
In mongo shell: **It's OK**

db.products.find().forEach( function(doc){db.products.insert(
{
"price":doc.price,
"name":doc.name,
"typeName":doc.typeName,
"image":doc.image
}
)} );
Reply

#7

single line
<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

(function(doc){delete doc._id; db.collection.insert(doc)})(db.collection.findOne({ _id: ObjectId("60c9a684c7d51907c35ad463")}))

<!-- end snippet -->

structured
```
(function(doc) {
delete doc._id;
db.collection.insert(doc);
})(db.collection.findOne({ _id: ObjectId('60c9a684c7d51907c35ad463') }));

// (function(doc){...})(doc)
```
Reply

#8
Many of the previous answers are simple and correct, but they are not efficient if your goal is to copy **all the documents in the collection** with new `_id`s. Here is a three-line solution that was more than 10x faster for me:

First copy the collection efficiently:
```
db.collection.aggregate([{ $out: "collection_copy" }]);
```

Now pass documents from the copy back into the original, but remove the `_id` field so a new `_id` is generated:
```
db.collection_copy.aggregate([{$project: {_id: 0}}, { $merge: "collection" }]);
```

Finally, clean up the copy:
```
db.collection_copy.drop();
```
Reply

#9
With a pipeline

db.collection.insertMany(
db.collection.aggregate([
{
$match: { _id: ObjectId("") }
},

{ $unset: [ "_id"] }]).toArray());
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through