How to copy part of collection from remote server to local database in MongoDB
I'm new to MongoDB, so I'm probably just missing something simple. The remote server has a named MongoDB database DatabaseABC
that has a collection called Logger
. This collection is very extensive. I figured the best way to figure out how to request it without waiting a few minutes between requests is to copy the latest documents data to the collection locally.
From what I've read, I have to use cloneCollection and give it a request to filter it. I cannot get it to work. The documentation says:
You have to connect directly to the mongod instance.
but I don't understand what that means. How do I connect to a local database using mongod
? mongod
there seems to be a way to start the database, I can do it, but I don't understand how to work with it { cloneCollection: "DatabaseABC.Logger", from: "mongoDevEtc.domain.net:27017", query: { TheTimestamp: "2015-05-13" } }
.
I need baby steps. Let's say I have a local database called test
. I got a new Microsoft Windows command prompt listed in the bin directory where mongod.exe exists. What commands am I entering to move all the logs written on May 13, 2015 from mongoDevEtc.domain.net:27017.DatabaseABC.Logger
to my local collection to 127.0.0.1:21000.test.Logger
(note, the log collection doesn't exist locally yet)?
source to share
First of all mongod
, this is the MongoDB server. It understands a bunch of different commands , but you need to use a client to issue those commands. The standard client for MongoDB Mongolian shell is calledmongo
. You can invoke it directly from the command line and start issuing some commands.
Now for your specific needs, the command allows you to copy from one collection to a DB on a remote server to another collection to another DB on your local server (i.e.: the one your client is connected to). From the Mongo Shell, you can execute this command (like any other "raw" command) with . Something like that: cloneCollection
db.runCommand
> db.runCommand(
{ cloneCollection: "DatabaseABC.Logger",
from: "mongoDevEtc.domain.net:27017",
query: { TheTimestamp: "2015-05-13" }
}
)
Note that your remote db has the same name as your local db, you can use the Mongo Shell database method db.cloneCollection
:
> db.cloneCollection("mongoDevEtc.domain.net:27017",
"Logger",
{ TheTimestamp: "2015-05-13" })¶
As you can see below, it db.cloneCollection
is a simple wrapper around the database command cloneCollection
:
> db.cloneCollection
function (from, collection, query) {
assert( isString(from) && from.length );
assert( isString(collection) && collection.length );
collection = this._name + "." + collection;
query = query || {};
return this._dbCommand( { cloneCollection:collection, from:from, query:query } );
}
source to share
To copy the database from computer to your computer, follow these steps:
- Open cmd
-
Run this code to copy:
mongodump --db Your_database_name /h IP_of_remote_machine
Example:
mongodump --db myDb /h 192.168.0.100
-
Run to restore database from dump / myNpsCorporate in mongodb:
mongorestore --Your_database_name dump/Your_database_name
Example:
mongorestore --myDb dump/myDb
-
finishing
source to share