0

I am trying to run mongodb copy collection on same server.
Requirement is we delete database as per our archival policy but we need to keep one collection from this data as a backup in separate db on same server.
I run copyCollection but it takes lock on whole mongo instance and block all other operations.
Collection size is 10 GB so it takes considerable time while copying and system is unavailable for duration.
Please suggest a way to achieve this without locking whole instance.

  • why using "db.collection.copyTo()" it's Deprecated since version 3.0. – Md Haidar Ali Khan Feb 14 '18 at 6:28
  • Ya i checked that so i am looking for another solution now. – viren Feb 14 '18 at 6:45
  • Yes you can do that through "mongoexport" and "mongoimport" shell command. If you are doing the query on the same mongod server. – Md Haidar Ali Khan Feb 14 '18 at 7:22
  • I have updated the answer as per your requirement. – Md Haidar Ali Khan Feb 14 '18 at 8:04
2

I am trying to run mongodb copy collection on same server. Requirement is we delete database as per our archival policy but we need to keep one collection from this data as a backup in separate db on same server.

Yes, it's possible through mongoexport from your source db and mongoimport the collection to the target database on the same mongod server.

Let's start from beginning.

Suppose that i have two database StackExchange and StackOverflow in same mongod.

> show dbs
StackExchange     0.000GB
StackOverflow     0.000GB

Suppose that in StackExchange there is collection orders having 4 documents.

> show collections
orders
>

Let's check there is documents records in Orders collection exist in StackExchange or not through mongo shell.

 > db.orders.find().pretty()
{
        "_id" : ObjectId("5a44c1479adf6e5fc5cea526"),
        "cust_id" : "A123",
        "amount" : 250,
        "status" : "A"
}
{
        "_id" : ObjectId("5a44c1479adf6e5fc5cea525"),
        "cust_id" : "A123",
        "amount" : 500,
        "status" : "A"
}
{
        "_id" : ObjectId("5a44c1479adf6e5fc5cea528"),
        "cust_id" : "A123",
        "amount" : 300,
        "status" : "D"
}
{
        "_id" : ObjectId("5a44c1479adf6e5fc5cea527"),
        "cust_id" : "B212",
        "amount" : 200,
        "status" : "A"
}
>

Here, as we can see that there is 4 document records in orders collection.

Suppose that i want to copy the orders collection of StackExchange database to the target database StackOverflow with targetorders documents name.

>mongoexport -d StackExchange -c orders | mongoimport -d StackOverflow -c targetorders
2018-02-14T10:52:16.630+0300    connected to: localhost
2018-02-14T10:52:16.631+0300    exported 4 records
2018-02-14T10:52:16.693+0300    connected to: localhost
2018-02-14T10:52:17.047+0300    imported 4 documents

To verify the targetorders collection documents in StackOverflow database.

> use StackOverflow
switched to db StackOverflow
> show collections
targetorders
test
> db.targetorders.find().pretty()
{
        "_id" : ObjectId("5a44c1479adf6e5fc5cea528"),
        "cust_id" : "A123",
        "amount" : 300,
        "status" : "D"
}
{
        "_id" : ObjectId("5a44c1479adf6e5fc5cea525"),
        "cust_id" : "A123",
        "amount" : 500,
        "status" : "A"
}
{
        "_id" : ObjectId("5a44c1479adf6e5fc5cea526"),
        "cust_id" : "A123",
        "amount" : 250,
        "status" : "A"
}
{
        "_id" : ObjectId("5a44c1479adf6e5fc5cea527"),
        "cust_id" : "B212",
        "amount" : 200,
        "status" : "A"
}
>

So, here we can see all the 4 documents of orders collection from the StackExchange has successfully copied in StackOverflow.

Very Import Note: Run mongoexport & mongoimport from the system command line, not the mongo shell.

Hope this will help out to you.

For further your ref mongoexport and mongoimport

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.