Now I want to Copy Only Column c & e from Database 1 to Database 2.
It's Only possible through mongo shell
method like cursor.forEach()
& db.getSiblingDB()
. you can copy the one database collection some selected documents to the another database collection.
My Question is How I can Copy the selected Columns on The entire Table
form DB1 And paste it to DB2 with only Selected Columns.
Let's start from beginning , suppose that you have two database db1
& db2
.
use db1
switched to db db1
I am going to create collection
in db1
having name orders
> db.createCollection("order")
{ "ok" : 1 }
Again i am going to insert some documents
in orders collection
in db1
.
> db.orders.insertMany([
... { "_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"
... },
... {
... "_id" : ObjectId("5a44c1479adf6e5fc5cea528"),
... "cust_id" : "A123",
... "amount" : 300,
... "status" : "D"
... }])
{
"acknowledged" : true,
"insertedIds" : [
ObjectId("5a44c1479adf6e5fc5cea525"),
ObjectId("5a44c1479adf6e5fc5cea526"),
ObjectId("5a44c1479adf6e5fc5cea527"),
ObjectId("5a44c1479adf6e5fc5cea528")
]
}
In the above collection orders
there is 4 documents , But i only want to copy some field records ( Suppose that here i want to copy "cust_id" : "B212" respective records to db2
).
Again i am going to create second database db2
.
> use db1
switched to db db2
Again i am going to create the collection orders1
in db2
.
> db.createCollection("order1")
{ "ok" : 1 }
Note : Here for running the below mention query you have to switch to db1
. because we are going to copy the collection records from db1
to db2
.
> use db1
switched to db db1
Then execute the below mention query
db.orders.find({"cust_id": "B212"}).forEach(function(d){db.getSiblingDB('db2')['orders1'].insert(d);});
For confirmation of copy of that records field has successfully done or not . You have to switch to db2
. And find out the collection order1
records in db2
.
> use db2
switched to db db2
> show collections
orders1
> db.orders1.find()
{ "_id" : ObjectId("5a44c1479adf6e5fc5cea527"), "cust_id" : "B212", "amount" : 200, "status" : "A" }
>
Or through below command to find the records in proper format
> db.orders1.find().pretty()
{
"_id" : ObjectId("5a44c1479adf6e5fc5cea527"),
"cust_id" : "B212",
"amount" : 200,
"status" : "A"
}
>
As above i have already said that only i want to copy the "cust_id" : "B212"
records from db1->orders
to db2->orders1
.
Note: Here the two databases would need to share the same mongod for this to work.
For your further ref Here and Here