Skip to content

CodeDemos/demo-mongo-queries

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Mongo Core Components (run from command line):

  • 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

Shell Helpers

--- 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(); }

mongo import

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

Writing Scripts for the mongo Shell

  • 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(), use print() 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

Demo queries

Hint: use .pretty(); Pro Tip: Run the following to enable pretty print for all queries

echo DBQuery.prototype._prettyShell = true >> ~/.mongorc.js

Basic queries (20 min)

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()

About

Basic mongo queries against items data

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published