- mongod* - core database process (aka the server)
- mongo* - interactive MongoDB shell
- mongos (we don't use this)
Other Command Line tools
- mongoimport*
- mongoexport
- mongodump
- mongorestore
- bsondump
--- helpers --- --- javascript --- show dbs, show databases db.adminCommand('listDatabases') use db = db.getSiblingDB('') show collections db.getCollectionNames() show users db.getUsers() show roles db.getRoles({showBuiltinRoles: true}) show log db.adminCommand({ 'getLog' : '' }) show logs db.adminCommand({ 'getLog' : '*' }) it cursor = db.collection.find() if ( cursor.hasNext() ){ cursor.next(); }
import locally
mongoimport -d listful -c items --drop -v --file ./items.json --jsonArray
import into mlab
mongoimport -h ds245287.mlab.com:45287 -d list-demo -c <collection> -u <user> -p <password> --file ./items.json --jsonArray
-
You can write and execute scripts directly in the shell but it is not the best editing environment.
-
Alternatively, you can write your queries in a script file have
mongo
execute them. The Mongo documentation provides a few examples here Write Scripts for the mongo Shell -
Our recommended approach uses Bash shell Input Redirection
<
. Pros:- No special result cursor code is needed to log the results
- Mongo queries are identical that used in the shell Cons:
- Requires Bash or support for input redirection
- No
console.log()
, useprint()
instead
Create a file named queries.js
or whatever is appropriate for your task.
db.items.find().limit(1).pretty()
mongo listful < ./queries.js
mongo ds121896.mlab.com:21896/<MY-DATABASE> -u <USERNAME> -p <PASSWORD> < ~/Desktop/myScript.js
Hint: use
.pretty();
Pro Tip: Run the following to enable pretty print for all queries
echo DBQuery.prototype._prettyShell = true >> ~/.mongorc.js
execute $ mongo listful < ./basic/find.js
execute $ mongo listful < ./basic/insert.js
execute $ mongo listful < ./basic/remove.js
.update()
.updateOne()
.updateMany()
.findOneAndUpdate()
.findAndModify()
.findOneAndReplace()
.replaceOne()
.insert()
.insertOne()
.insertMany()
.remove()
.deleteOne()
.deleteMany()
.findOneAndDelete()