Cannot use commands write mode error, Degrading to compatibility mode - mongodb

I just play with mongo shell and came across with Cannot use commands write mode, degrading to compatibility mode.
I connected to remote mongo server (mongolab) and tried to insert new record to collection by my simple script:
// script.js
db = connect(host + ":" + port +"/" + dbName);
db.auth(username, password);
db.test2.insert({ item: "card", qty: 15 });
I run script by mongo script.js and got:
MongoDB shell version: 2.6.3
connecting to: test
connecting to: my.mongolab.com:port/DBname
Cannot use commands write mode, degrading to compatibility mode
What is wrong? Additionally when I executed similar query after connected via mongo my.mongolab.com:port/DBname -u <dbuser> -p <dbpassword> everything is OK.

You are using a shell that's newer than the server it's communicating with.
You can check the server version from the shell via db.version() - to check shell version you use version()
Starting with 2.6 the mongod server started using new write commands which are different than the previously use insert/update/remove op code (this is all described in the MongoDB Wire Protocol).
This is a harmless "informational" warning. It's a good idea to use the same version shell as the server to avoid wondering about such things though.

Related

mongosh: how to connect with uuidRepresentation=javaLegacy?

I am connecting to my local host mongodb via moongo shell with the following command and getting the following error.
Command
mongosh "mongodb://username:password#localhost:27017/authDb?uuidRepresentation=javaLegacy"
Error
Connecting to: mongodb://<credentials>#localhost:27017/authDb?uuidRepresentation=javaLegacy&directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.2.3
MongoParseError: option uuidrepresentation is not supported
My requirement is to execute mongo queries that involves Java Legacy UUIDs and the expect the query results to be represented like that.
I can achieve this via tools like Robo3T though.
But I am a specific case where I must do it via mongo shell.
I cannot find any support for this elsewhere.

Using a BASH script to edit a Mongo database when there is no Mongo client installed

I have an application running on a Raspberry Pi and it's storing data in a Mongo database, but the Mongo client is not also installed on the Pi. I'm using Alpine Linux for my OS.
I want to search for entries in the database using a single parameter in that entry (i.e. a filename) and then completely delete the entry from the database.
How can I accomplish this with a BASH script when the Mongo client is not installed on the Pi?
Please let me know.
Thanks!
The simple solution is install the mongo shell, or use one of the drivers such as pymongo.
If you're really good with bash, you might be able to use nc, telnet, or expect to connect to the mongod port, and speak Mongo Wire Protocol, which is not nearly as fun as it sounds.

How do I set the default connection URL for mongo CLI?

I cannot find anywhere to set the default connection URL for the mongo CLI. I want to be able to specify a particular username/password/hostname/database, which can all be specified as a URL. However, I want to just be able to type mongo instead of mongo "mongodb://…" in the shell.
I also don’t want the password to show up in /proc/«PID»/cmdline, but having it in a dotfile in my home directory is perfectly fine with me.
It doesn’t appear that ~/.mongorc.js allows specifying the default connection string. But I would expect such an option to be available because the mysql CLI supports ~/.my.cnf which allows you to specify username/password/hostname/database. So, where is this for mongodb?
EDIT: The solution has to work even if the mongodb at localhost is fully secured (no insecure local test database should be required—I thought this went without saying).
You can run mongo shell commands in ~/.mongorc.js. So you can just run the connect method in your ~/.mongorc.js:
db = connect("mongodb://username:password#host:27017/myDatabase")
When you run mongo it will first connect to localhost, then connect to the database passed to the connect method.
A MongoDB server must be running on localhost, otherwise the mongo shell will error and exit before trying to connect to the other DB.
Run mongo --nodb to get around needing to connect on localhost before connecting to whatever is defined in ~/.mongorc.js.
To directly launch mongo against a particular hostname/database/username/password without prompting for a password, without passing the password on the CLI, and without requiring an insecure database at mongodb://localhost/test, you can use this technique. Write a script file which does the connection for you and then launch mongo with the appropriate arguments. For example:
~/.mongo-do-auth.js
// https://docs.mongodb.com/manual/reference/program/mongo/#cmdoption-nodb
// https://docs.mongodb.com/manual/tutorial/write-scripts-for-the-mongo-shell/#mongo-shell-new-connections
//
// Run this with:
//
// mongo --nodb --shell ~/.mongo-do-auth.js
db = connect('localhost/admin', 'root', 'asdf123');
And then, simply launch it from the CLI to get an interactive session:
sam ~ # mongo --nodb --shell ~/.mongo-do-auth.js
MongoDB shell version: 2.6.12
type "help" for help
connecting to: localhost/admin
> quit()
Or, to run a script noninteractively, simply remove --shell and append your script. Consider a script:
s2.js:
print('connected to database ' + db);
To run:
sam ~ # mongo --nodb ~/.mongo-do-auth.js ~/s2.js
MongoDB shell version: 2.6.12
loading file: /root/.mongo-do-auth.js
connecting to: localhost/admin
loading file: /root/s2.js
connected to database admin
However, this requires a lot of work and invoking mongo with arguments. It appears that this is a limitation of mongo because no one has suggested any alternative which works as smoothly as the mysql CLI client’s ~/.my.cnf. This means that all shell scripts which directly invoke mongo will need to allow the the user to pass through --nodb and a path to a connection script instead of being able to rely on implicit per-user configuration.

ReferenceError: require is not defined in MongoDB shell

I try to connect MongoDB from Mongo client on windows command (Window 8.1). When I use require() in javascript, I have error as below. Does any one has same issue? Did I miss any require related npm installation? How can't MongoDB shell find require function?
C:\tutorial\nodeMongoAngular-master\lesson2>mongo
MongoDB shell version: 3.0.1
connecting to: test
var MongoClient = require('mongodb').MongoClient;
2015-04-30T14:33:25.812-0400 E QUERY ReferenceError: require is not defined
at (shell):1:19
You are confusing the mongo administrative shell with the Node.js driver. While both environments happen to use JavaScript, the mongo shell has more limited I/O support and is not intended to be used as a driver for application development.
If you want to write Node.js applications using the MongoDB driver (as per your example code), you need to use the node interpreter. The Node.js driver documentation includes a Quickstart tutorial with examples that should help you get started.
#Scott Lee: if you're still looking for an answer, try running with command "node yourscript.js". Make sure mongod is running. 'mongo script.js' will work for pure mongo scripts without nodejs code.

how can I connect to a remote mongo server from Mac OS terminal

I would like to drop into the mongo shell in the terminal on my MacBook. However, I'm interested in connecting to a Mongo instance that is running in the cloud (compose.io instance via Heroku addon). I have the name, password, host, port, and database name from the MongoDB URI:
mongodb://username:password#somewhere.mongolayer.com:10011/my_database
I have installed mongodb on my MacBook using Homebrew not because I want Mongo running on my Mac, but just to get access to the mongo shell program in order to connect to this remote database.
However, I can't find the right command to get me the full shell access I would like. Using instructions found here http://docs.mongodb.org/manual/reference/program/mongo/ (search for "remote") I am able to get what looks like a connection, but without giving my username or password I am not fully connected. Running db.auth(username, password) returns 1 (as opposed to "auth fails" when I provide incorrect username and password), but I continue to get an "unauthorized" error message when issuing the show dbs command.
You are probably connecting fine but don't have sufficient privileges to run show dbs.
You don't need to run the db.auth if you pass the auth in the command line:
mongo somewhere.mongolayer.com:10011/my_database -u username -p password
Once you connect are you able to see collections?
> show collections
If so all is well and you just don't have admin privileges to the database and can't run the show dbs
With Mongo 3.2 and higher just use your connection string as is:
mongo mongodb://username:password#somewhere.mongolayer.com:10011/my_database
Another way to do this is:
mongo mongodb://mongoDbIPorDomain:port