Onosa
Onosa

Reputation: 1273

How to copy part of a collection from a remote server to a local database in MongoDB

I am new to MongoDB, so I'm probably just missing something simple. A remote server has a MongoDB database called DatabaseABC which has a collection called Logger. This collection is humongous. I figured the best way to understand how to query against it without having to wait several minutes between queries was to copy the last day's worth of documents in the collection locally.

From what I've read, I should be able to use cloneCollection and give it a query to filter it down. I can't get it to work. The documentation says,

You must 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 seems like a way to start a database, I can do that, but I don't get how to run { cloneCollection: "DatabaseABC.Logger", from: "mongoDevEtc.domain.net:27017", query: { TheTimestamp: "2015-05-13" } } with it.

I need baby steps. Assume I have a local database called test. I have a fresh Microsoft Windows command prompt open pointed at the bin directory where mongod.exe exists. What commands do I enter in order to move all the logs written on May 13, 2015 from mongoDevEtc.domain.net:27017.DatabaseABC.Logger to my local collection at 127.0.0.1:21000.test.Logger (note, the logger collection doesn't exist locally yet)?

Upvotes: 4

Views: 3106

Answers (2)

Namatullah Shahbazi
Namatullah Shahbazi

Reputation: 238

To copy a database from a machine to your machine, fallow this steps:

  1. Open the cmd
  2. Run this code to copy:

    mongodump  --db Your_database_name /h IP_of_remote_machine
    

    ex:

    mongodump  --db myDb /h 192.168.0.100
    
  3. Run to restore the database from dump/myNpsCorporate to mongodb:

    mongorestore  --Your_database_name dump/Your_database_name
    

    ex:

    mongorestore  --myDb dump/myDb
    
  4. finish

Upvotes: 0

Sylvain Leroux
Sylvain Leroux

Reputation: 52040

First of all mongod 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 is the Mongo Shell called mongo. You can invoke it directly from the command line and start issuing some commands.

Now, for your specific needs: the cloneCollection command allows you to copy from one collection in a DB on a distant server to an other collection, on a different DB on you local server (i.e.: the one to which your client is connected). From the Mongo Shell, you may issue this command (like any other "raw" command) using db.runCommand. Something like that:

> db.runCommand(
               { cloneCollection: "DatabaseABC.Logger",
                 from: "mongoDevEtc.domain.net:27017",
                 query: { TheTimestamp: "2015-05-13" }
               }
)

Please note that is your distant db has the same name as your local db you might use the Mongo Shell database method db.cloneCollection instead:

> db.cloneCollection("mongoDevEtc.domain.net:27017",
                     "Logger",
                     { TheTimestamp: "2015-05-13" })¶

As you can see below, db.cloneCollection is a simple wrapper around the cloneCollection database command:

> 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 } );
}

Upvotes: 5

Related Questions