Got unauthorized error when renaming mongodb collection - mongodb

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...

Related

Installing Mongodb in Kubernetes using Helm

I installed mongodb using Helm in my Kubernetes cluster.
https://github.com/kubernetes/charts/tree/master/stable/mongodb
It works fine and lets me connect to the db. But I cannot do any operation once I log into the mongo server. Here is the error I get when I try to see the collections.
> show collections;
2018-04-19T18:03:59.818+0000 E QUERY [js] Error: listCollections failed: {
"ok" : 0,
"errmsg" : "not authorized on test to execute command { listCollections: 1.0, filter: {}, $db: \"test\" }",
"code" : 13,
"codeName" : "Unauthorized"
} :
_getErrorWithCode#src/mongo/shell/utils.js:25:13
DB.prototype._getCollectionInfosCommand#src/mongo/shell/db.js:942:1
DB.prototype.getCollectionInfos#src/mongo/shell/db.js:954:19
DB.prototype.getCollectionNames#src/mongo/shell/db.js:965:16
shellHelper.show#src/mongo/shell/utils.js:836:9
shellHelper#src/mongo/shell/utils.js:733:15
#(shellhelp2):1:1
I am logging in as the root user using
mongo -p password
I don't know why even the root user has no authorization to do anything.
I found the issue. By default, MongoDB uses the admin DB to authenticate but in the helm chart, the authentication DB is the same as the DB that you create with it. So If I create a DB called test, the authentication DB will also be test

Mongo java driver authentication not working properly

I am trying to access mongodb from java using mongo java driver 3.4. I wrote below code to access mongodb and even though I entered wrong credentials while connecting I am able to access my database.
MongoCredential credential = MongoCredential.createScramSha1Credential("rupesh_user", "rupesh_db",
"admin123".toCharArray());
MongoClient mongoClient= new MongoClient( new ServerAddress( "localhost", 27017 ), Arrays.asList(credential));
When I am trying to access mongo database from terminal in ubuntu. Its asking for authentication.
> use rupesh_db
switched to db rupesh_db
> show collections
2017-08-03T13:07:52.970+0530 E QUERY [thread1] Error: listCollections failed: {
"ok" : 0,
"errmsg" : "not authorized on rupesh_db to execute command { listCollections: 1.0, filter: {} }",
"code" : 13,
"codeName" : "Unauthorized"
} :
From Console you will have to authorize
> use rupesh_db
switched to db rupesh_db
> db.auth("rupesh_user","admin123")
> show collections
More details

Mongo Copy Database Collect Exist Error

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.

How to get rid of "not authorized on parse to execute command { listCollections: 1.0 }"

I have my AWS EC2 instance given by my client.
On Which There Is an existing database.
But When I SSH to the server using given credentials and run the following command
show collections
I am getting this error
2016-08-30T12:22:52.216+0000 E QUERY Error: listCollections failed: {
"ok" : 0,
"errmsg" : "not authorized on parse to execute command { listCollections: 1.0 }",
"code" : 13
How to get my user authenticated perform such operations.Or how to get existing Admin credentials
Just turn off the security from mongodb.conf
noauth = true
#auth = true
so you should be able to logged in as admin now. (use show users to get the list of existing users in base)

What is the BSON doc (query) for the cmd `show dbs` in MongoDB?

According to Getting Started with MongoDB, we can use show dbs to get the list of existing databases.
But this is a kind of command running in the mongo shell.
My question is that how to write a mongodb query (bson) for the list of databases, and where this query should be sent to?
It's not a query, but you can run the listDatabases command against the admin database to get a list of databases in BSON:
> use admin
switched to db admin
> db.runCommand({listDatabases : 1})
{
"databases" : [
{
"name" : "local",
"sizeOnDisk" : 83886080,
"empty" : false
},
],
"totalSize" : 83886080,
"ok" : 1
}
Most programming language engines for MongoDB can also run commands in addition to queries given the right syntax. For example, in Java:
DB db = mongo.getDB("admin");
DBObject cmd = new BasicDBObject("listDatabases", 1);
CommandResult result = db.command(cmd);
Interestingly, if you're really insistent on it being a query, you can query the virtual collection $cmd on the admin database to run commands by submitting queries:
> use admin
switched to db admin
> db.$cmd.findOne({"listDatabases":1})