Mongo Copy Database Collect Exist Error - mongodb

I am trying to copy a database from a remote host over to the current database. I am using this command:
rs0:PRIMARY> db.copyDatabase("olddb", "newdb", "xx.xx.xx.137", "user1", "abc123");
But I am getting this error:
{
"done" : true,
"ok" : 0,
"errmsg" : "failed to create collection \"newdb.email_batches\": collection already exists"
}
Except the collect does not exist. What could I be doing wrong in this command?

The problem was different database versions. The DB I was on was Mongo 3.0 and the database I was trying to transfer from was 3.2
What is very strange about that is the command copyDatabase as functionaliy for transferring data from 2.6 to 3.2, yet for some reason does not work with 3.0.

Related

How to write log file for individual collection in mongodb database?

I have a peculiar question that I haven't been able to find an answer to yet. I have a database and a collection in it. Let's call them "test database" and "test collection". There are a range of users from root to a user with read-only access to the "test database". I need to output a special log file for operations that are carried out in a "test database".
I have already found "mongo.log" in the path "/var/log/mongodb/". But all possible requests from all "databases" are loaded into this file at once, which is not convenient. Is it possible somehow to create a separate file for writing logs only from the "test database"?
I also found out earlier that in order to keep track of all requests, I need to set the following settings:
$ mongo
MongoDB shell version: 2.4.9
connecting to: test
> use myDb
switched to db myDb
> db.getProfilingLevel()
0
> db.setProfilingLevel(2)
{ "was" : 0, "slowms" : 1, "ok" : 1 }
> db.getProfilingLevel()
2

Mongo shell not creating indexes on running databases

We dumped the definition of indexes using mongo-index-exporter and we try to apply them at each launch of our application, so to achieve a reproducible environment, by executing
mongo --verbose "mongodb://$MONGO_DB_USER:$MONGO_DB_PASSWORD#$MONGO_DB_HOST/$MONGO_DB_NAME?authSource=$MONGO_AUTH_SOURCE&authMode=scram-sha1" indexes.js
on a file like so:
print('Using db ' + db)
setVerboseShell(true)
print('Creating indexes on mycoll1')
db.mycoll1.createIndex({"_id":1}, {"name":"_id_", "background": true});
db.mycoll1.createIndex({"createdAt":1,"field1.field2.field3":1}, {"name":"coll1_field123_idx"});
print('Creating indexes on mycoll2')
db.mycoll2.createIndex({"_id":1}, {"name":"_id_", "background": true});
db.mycoll2.createIndex({"createdAt":1,"field1.field2.field3":1}, {"name":"coll1_field123_idx"});
To create indexes on a replicaSet with five nodes, we are performing the
following command:
mongo --verbose "mongodb://$MONGO_DB_USER:$MONGO_DB_PASSWORD#$MONGO_DB_HOST/$MONGO_DB_NAME?authSource=$MONGO_AUTH_SOURCE&authMode=scram-sha1" indexes.js
this works fine for us in QA, where MONGO_DB_HOST is a single node and we are using a fresh new empty database, and doesn't work in production where additionally the database is already existing (and the collection have contents).
Additional relevant information:
background mode doesn't make any difference
The verbose mode doesn't work, i.e. although we launch mongo client with -v and we do a setVerbose(true) nothing is logged on the console
Our mongo shell is 3.6.11 and our mongo server is 3.2.9
Copy pastying the commands inside a mongo shell executed on the server works terminates immediately and results in the following output
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 11,
"numIndexesAfter" : 11,
"note" : "all indexes already exist",
"ok" : 1
}

How to clone/copy a database in Azure Cosmos DB

How do I clone a database in Azure Cosmos DB to a mongod running on localhost:27001?
I've tried the following, but I can't get it working:
db.cloneDatabase("mycosmosdb:mypassword#username.documents.azure.com:10255/MyDatabase?ssl=true&replicaSet=globaldb")
This returns the following when running the command from my local machine where mongod is running :
{
"clonedColls" : [ ],
"ok" : 0,
"errmsg" : "connect failed to replica set mycosmosdb:mypassword#username.documents.azure.com:10255/Mydatabase?ssl=true&replicaSet=globaldb:27017",
"code" : 6,
"codeName" : "HostUnreachable"
}
Trying this variant also fails:
db.copyDatabase("NameOfAzureDB", "NameOfLocalDB", "username.documents.azure.com:10255", "username", "password")
{
"ok" : 0,
"errmsg" : "couldn't connect to server username.documents.azure.com:27017, connection attempt failed"
}
cloneDatabase is not supported on Cosmos DB as clone command that it wraps around is not flexible enough to allow SSL connection and the name of the replica set. It only accepts the hostname and port number and nothing else, so the connection string used in the question is not supported (and there's no such moniker as "mycosmosdb" either).

Got unauthorized error when renaming mongodb collection

I am using mongohq sandbox plan. In the command prompt,
db["oldCollectionName"].renameCollection("newCollectionName", true)
works fine without using admin database.
However, I got "unauthorized" exception when I do this in Java:
oldCollection.rename(newCollectionName);
Since I am using mongohq sandbox plan, I don't have access to admin database. Is there a way to rename this collection without creating a new collection, copying over all the documents, and dropping the old collection?
In Java and using Jongo you can do the following:
MongoCollection col = new Jongo(DbConfigurer.getDB()).getCollection("CODE");
col.getDBCollection().rename("CODE45", true);
I've just tested and it works.
Now using the 'runCommand' (same than using db.command) in the following example:
DB db = ....getDB();
Jongo jongo = new Jongo(db);
jongo.runCommand("{ renameCollection : 'OLD_NAME', to: 'NEW_NAME', dropTarget: true}");
I obtain the same error you have.
I read from some documentation you have to connect first to admin database to process some of the command not allowed, so i did the same but with "admin" db, and i obtain the following error stack:
{ "serverUsed" : "localhost/127.0.0.1:27017" ,
"errmsg" : "exception: invalid collection name: NEW_NAME" , "code" : 15967 , "ok" : 0.0}
Strange to have such different behaviors...

Can't add member into MongoDB replica-set

I am using the MongoDB 2.4.3, and following the wizard:
http://docs.mongodb.org/manual/tutorial/deploy-replica-set/
But when adding the other members into replica-set, get the following error:
root#vm3:~# mongo
MongoDB shell version: 2.4.3
connecting to: test
rs1:PRIMARY> rs.add("vm1")
{
"errmsg" : "exception: set name does not match the set name host vm1:27017 expects",
"code" : 13145,
"ok" : 0
}
rs1:PRIMARY> rs.add("vm4")
{
"errmsg" : "exception: set name does not match the set name host vm4:27017 expects",
"code" : 13145,
"ok" : 0
}
vm1, vm3 and vm4 know each other because I configured their /etc/hosts files correctly.
Any idea? I don't understand what does this error message mean!
After restarting all vms, it works now.
root#vm3:~# mongo
MongoDB shell version: 2.4.3
connecting to: test
rs1:PRIMARY> rs.add("vm4")
{ "ok" : 1 }
rs1:PRIMARY> rs.add("vm1")
{ "ok" : 1 }
In my case, just restart virtual machines, every thing is fine.
If you are re-installing a MongoDB instance, the replSet may be living in your data file on the drive. I had the same problem setting up a new replica set. The problem was from changing the replica set name after bringing up instances with an older replSet name. I deleted the data files, ran my install scripts again and it worked just fine.