Skip to content

Instantly share code, notes, and snippets.

@michaelerobertsjr
Last active March 8, 2017 22:37
Show Gist options
  • Save michaelerobertsjr/1223bcd0f6f3083f715e60dc5bee0d9a to your computer and use it in GitHub Desktop.
Save michaelerobertsjr/1223bcd0f6f3083f715e60dc5bee0d9a to your computer and use it in GitHub Desktop.
Mongo Queries

#MongoDB

What is mongo - document based database, no SQL How do you install it by default its not secure, you should add a user and restrict access before production

digital ocean mongo labs using mongoLabs mongo ds021671.mlab.com:21671/sdcs -u michael -p michael robo mongo

local use db .find({filter}, {projection})

db.collection.find({ age: { $gt: 10}}, {history:0}) projection (limit the fields returned, cant include and exclude)

.insert({})

Insert multiple objects

.insert( [ { "name": "banana", "color": "yellow", "number": 8 }, { "name": "grape", "color": "green", "number": 1020, "foodGroup": { "size": "small", "healthy": true, "snack": true } }, { "name": "peach", "color": "pink", "number": 1020, "foodGroup": { "size": "medium", "healthy": true, "snack": true } } ])

update nested object

.insert({"foodGroup.size":"medium"},{ $set: { "name": “pear”, "color”:"green" } })

.remove() .count() .sort({name: 1}) .limit(2) .pretty()

cursors (how queries are executed, so it can be done in one request (call) when connecting the the db)

.sort({name: 1}) .limit(5) .pretty()

queries for record by object id

ObjectId() Returns a new ObjectId value. The 12-byte ObjectId value consists of:

  • a 4-byte value representing the seconds since the Unix epoch,
  • a 3-byte machine identifier,
  • a 2-byte process id, and
  • a 3-byte counter, starting with a random value.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment