Skip to content

Mongodb Notes

Raymond Rusk edited this page Dec 5, 2013 · 3 revisions

# Basic Usage

To get help while in the MongoDB shell

db.help()

To list available commands

db.listCommands()

To see the databases in mongod

show dbs

To select the query_gateway_development database use

use query_gateway_development

To obtain help regarding methods operating on collections

db.query_gateway_development.help()

To see the collections/tables in the selected database use

show collections

To count the number of entries in the records collection of query_gateway_development do

db.records.count()

To remove all the entries in records use

db.records.remove()

To view the (first 20) documents in the records collection

db.records.find()

To view the next 20 documents in the records collection

it

To see all the entries in records in json format use

var c = db.records.find() ; while (c.hasNext()) printjson(c.next())

To see only the medication section of the entries in json format use

var c = db.records.find() ; while (c.hasNext()) printjson(c.next()['medications'])

To gather only the medication codes and descriptions for all patients

var c = db.records.find({},{"medications.codes": 1, "medications.description": 1}) ; c.forEach(printjson)

To remove all old queries from the query-gateway mongo database

db.queries.remove()

Queries

To determine the number of records of gender "M" in the database

db.records.find({ gender: "M" }).count()

To find the record for AGNES ASANTA and to pretty-print it

db.records.find({ first: "AGNES", last: "ASANTE" }).pretty()

To print first and last name fields (but not the _id field)

db.records.find({}, { first: 1, last: 1, _id: 0} )

To print first and last name fields sorted alphabetically on first name

db.records.find({}, {first:1, last:1, _id:0}).sort({first:1})

To print first and last name fields for patients with abnormal lab results

db.records.find({ "results.interpretation.code": /a/i}, {first: 1, last: 1, _id: 0})

To find lab result documents for "AGNES ASANTE", displaying last name, time, value, description, referenceRange and interpretation

db.records.find({ last: "ASANTE", first: "AGNES" }, {last: 1, "results.time":1, "results.value": 1, "results.description": 1, "results.referenceRange":1, "results.interpretation.code":1}).pretty()

MongoDB shell internals

The MongoDB shell provides create, read, update and delete operations through Javascript rather than SQL.

use crud
db.crud.insert({username: "rrusk"})
db.crud.find()
db.crud.save({username: "morganprice"})
db.crud.count()
db.crud.update({username: "rrusk"}, {$set: {office: "ECS 448"}})
db.crud.find({username: "rrusk"})
db.crud.update({username: "rrusk"}, {$unset: {office: 1}})
db.crud.find({username: "rrusk"})
db.crud.remove()
show collections
show dbs
use crud
db.dropDatabase()

To find out how a mongodb command is implemented enter the command without parameters

db.records.find

Current Iteration: 13

General Topics

Resources


Previous Iteration: 12

Previous Iteration: 11

Previous Iteration: 10

Previous Iteration: 9

Previous Iteration: 8

Previous Iteration: 7

Previous Iteration: 6

Previous Iteration: 5

Previous Iteration: 4

Previous Iteration: 3

Previous Iteration: 2

Previous Iteration: 1

Previous Iteration: 0

Clone this wiki locally