What mongodb command I should run to know server info? - mongodb

For example, I want to know the database directory that is used by mongodb to run what it is?
The documentation said that the default data is in /data/db however, there is no such directory
I wonder if there is a mongodb command to get that simple info. I look through the web and could not find it.

You can see the Configuration Parameters used with:
> db.serverCmdLineOpts()
For example:
{
"argv" : [
"mongod",
"--dbpath",
"/usr/local/db"
],
"parsed" : {
"dbpath" : "/usr/local/db"
},
"ok" : 1
}
Any parameters not specifically listed will be using their default values.

Just create a folder called data anywhere in your system.
Then let Mongo know where the this folder is by updating the path.
in windows you would run this on the command line.
C:\mongodb\bin\mongod.exe --dbpath c:\test\mongodb\data
This is where your mongo stores your data.

Related

How to execute mongo commands through shell scripts? (mine does not work)

I am trying to use scripts js in the mongo shell. I am reading a book on the topic. I have tried several options, such as opening the cmd at a specific place at my computer.
I found here the question "How to execute mongo commands through shell scripts?", but nothing seems to work for me. My mongo is working properly and I can enter mongo simply typing mongo at cmd. However, it cannot find the js files.
I am using Windows; anything has an idea what may be happening under the hood?
An example after:
mongo demo.js
2020-02-20T11:03:33.098-0300 E - [main] file [demo.js] doesn't exist
2020-02-20T11:03:33.098-0300 F - [main] failed to load: demo.js
2020-02-20T11:03:33.098-0300 E - [main] exiting with code -3
Create your my_script.js file with this one command:
db.testColl.insertOne( { a: "hello" } )
Place the script file in your current directory.
1. Run JS Script from OS Command-line:
From OS prompt do this:
> mongo localhost/testDB my_script.js
Once the above command is run, you will see the output as follows (similar, depending upon your MongoDB version and the OS (Windows, in this case)):
MongoDB shell version v4.2.3
connecting to: mongodb://localhost:27017/testdb?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("456b350f-668f-4389-9901-7c456e2c50fe") }
MongoDB server version: 4.2.3
Now, from the Mongo Shell (i.e., do mongo, and then from the mongo prompt):
mongo > use testDB
mongo > show collections
You will see the testColl listed.
mongo > db.testColl.find()
{ "_id" : ObjectId("5e4ea0d05816162b300b0346"), "a" : "hello" }
This is the document created in the testDB database and the collection testColl as per the command in the my_script.js.
2. Run JS Script from Mongo Shell:
Also, you can run the my_script.jsfrom within the Mongo Shell.
mongo > load("my_script.js")
true
mongo > db.test.find()
{ "_id" : ObjectId("5e4ea0d05816162b300b0346"), "a" : "hello" }
{ "_id" : ObjectId("5e4ea10f276cde8fc5fedec5"), "a" : "hello" }
See there are two documents with different _id field values.
NOTE: I think you can run only some commands from the .js file.
3. Another Example:
Create a JS file named script2.js with the following content:
db.test.find().forEach(printjson)
Note the printjson shell method prints a document to the shell output.
mongo > load("script2.js")
{ "_id" : ObjectId("5e4ea0d05816162b300b0346"), "a" : "hello" }
{ "_id" : ObjectId("5e4ea10f276cde8fc5fedec5"), "a" : "hello" }
References:
load()
Print document value in
MongoShell

How to find mongodb data and log files location through command?

How to find mongodb data and log files location through command?
like below SQL server command.
SELECT * FROM sys.database_files
The easiest way is probably with the getCmdLineOpts command:
db.getSiblingDB("admin").runCommand({getCmdLineOpts:1})
This Mongo Shell command will first switch to the admin database then execute the getCmdLineOpts command. An alternative is the shell wrapper:
db.serverCmdLineOpts()
These will return the parsed command line options, which should contain both the data directory being used and the log path.
{
"argv" : [
"C:\\****\\3.4.10\\bin\\mongod.exe",
"--dbpath",
"C:\\****\\data",
"--port",
"27017",
"--logpath",
"C:\\****\\data\\mongod.log",
"--bind_ip",
"0.0.0.0"
],
"parsed" : {
"net" : {
"bindIp" : "0.0.0.0",
"port" : 27017
},
"storage" : {
"dbPath" : "C:\\****\\data"
},
"systemLog" : {
"destination" : "file",
"path" : "C:\\****\\data\\mongod.log"
}
},
"ok" : 1
}
Note: I obfuscated my paths, they do not normally contain ****.
You can see it provides both the raw values as well as the parsed values. If both command line options and config file options are specified on the command line, this will show the effective values being used by the process. Keep in mind there are several extra options that can effect where data is stored but this should get you on your way pretty quickly.
If you would like to know this information without using the Mongo Shell you will have to either grep the config file or look at the command line options of the running process, or both.
You can view logs in mongoCLI also
to list all logs
> show logs
global
startupWarnings
show log content
> show log global
2018-01-30T09:14:10.305+0530 I CONTROL [initandlisten] MongoDB starting : pid=778 port=27017 dbpath=/var/lib/mongodb 64-bit host=ubuntu
2018-01-30T09:14:10.305+0530 I CONTROL [initandlisten] db version v3.6.1
2018-01-30T09:14:10.305+0530 I CONTROL [initandlisten] git version: 025d4f4fe61efd1fb6f0005be20cb45a004093d1
data path will be printed on global log line 1, in my machine its dbpath=/var/lib/mongodb

How to execute the mongo cloneCollection command to copy a collection to a remote server

I'd like to copy a collection from one database to an instance on another server. From other stackoverflow questions, I understand the correct way to do that is with this command:
{ cloneCollection: "<collection>", from: "<hostname>", query: { <query> } }
via http://docs.mongodb.org/manual/reference/command/cloneCollection/
However, I don't understand where do I enter this command? It isn't accepted as...
$ mongod { cloneCollection: "remote", from: "ec2-whatever-amazon.com"}
How do I copy a remote collection at db.remote.collname to db.local.collname using this cloneCollection syntax via command line?
MongoDB database commands are run using db.runCommand() from the mongo shell. Refer to http://docs.mongodb.org/manual/tutorial/use-database-commands/.
Try something like this (using another database command for simplicity):
$ mongo
> db.runCommand({ isMaster: 1})
{
"ismaster" : true,
"maxBsonObjectSize" : 16777216,
"localTime" : ISODate("2014-02-18T22:30:04.417Z"),
"ok" : 1
}
>

mongo --shell file.js and "use" statement

can't find solution for simple question:
I have file text.js
use somedb
db.somecollection.findOne()
When I run this file in cmd with redirection command from file:
"mongo < text.js"
it's work properly
But when I try this way
"mongo text.js" or "mongo --shell test.js"
I got this error message
MongoDB shell version: 2.2.0
connecting to: test
type "help" for help
Wed Dec 05 16:05:21 SyntaxError: missing ; before statement pathToFile\test.js.js:1
failed to load: pathToFile\test.js.js
It's fail on "use somedb". If I remove this line, it's run without error, but console is clear.
is there any idea, what is this and how to fix?
I'm tying to find sollution for this, to create build tool for Sublime Text 2.
default build file was
{
"cmd": ["mongo","$file"]
}
but in this case I get the error above
PS. right after posting this question I find sollution for SublimeText2:
{
"selector": "source.js",
"shell":true,
"cmd": ["mongo < ${file}"]
}
PSS. right after posting this question I find sollution for SublimeText3:
{
"selector": "source.js",
"shell":true,
"cmd": ["mongo","<", "$file"]
}
this build tool work properly
use dbname is a helper function in the interactive shell which does not work when you are using mongo shell with a JS script file like you are.
There are multiple solutions to this. The best one, IMO is to explicitly pass the DB name along with host and port name to mongo like this:
mongo hostname:27017/dbname mongoscript.js // replace 27017 with your port number
A better way to do this would be to define the DB at the beginning of your script:
mydb=db.getSiblingDB("yourdbname");
mydb.collection.findOne();
etc.
The latter is preferable as it allows you to interact with multiple DBs in the same script if you need to do so.
You can specify the database while starting the mongo client:
mongo somedb text.js
To get the output from the client to stdout just use the printjson function in your script:
printjson(db.somecollection.findOne());
Mongo needs to be invoked from a shell to get that mode, with Ansible you would have this:
- name: mongo using different databases
action: shell /usr/bin/mongo < text.js
Instead of this:
- name: mongo breaking
command: /usr/bin/mongo < text.js
This is what finally worked for me on Windows + Sublime Text 2 + MongoDB 2.6.5
{
"selector": "source.js",
"shell":true,
"cmd": ["mongo","<", "$file"],
"working_dir" : "C:\\MongoDB\\bin"
}

About the mongodb's default db path?

Environment:
3.4.9-gentoo
mongodb (OpenRC) 0.9.8.4 (Gentoo Linux)
if I use mongod daemon to start mongodb,the default db path is /data/db
But if I use /etc/init.d/mongodb script to start mongodb, the /etc/conf.d/mongdb write the default db path is /var/lib/mongodb,
I am puzzled that why the db path is not the same?
The default dbpath if you start MongoDB without a configuration file is /data/db.
Your init script (/etc/init.d/mongodb) is starting mongodb with the --config (aka -f) option and a path to a config file to use (/etc/conf.d/mongodb).
If you look at the contents of your /etc/config.mongodb configuration file, you should see the dbpath setting with the /var/lib/mongodb directory path that overrides the default. In this case the maintainer of your MongoDB install package has decided that /var/lib is the most appropriate default directory for data files. Generally this is done to be more consistent with the default locations used by other packages in your distribution; the MongoDB data files can live anywhere on your filesystem.
You can also check for any settings that have been overridden by your configuration file in the mongo shell using:
getCommandLineOpts()
The output will be similar to:
{
"argv" : [
"mongod",
"--dbpath",
"/var/lib/mongodb"
],
"parsed" : {
"dbpath" : "/var/lib/mongodb"
},
"ok" : 1
}