Skip to content

Instantly share code, notes, and snippets.

@zerin108
Last active January 27, 2016 06:37
Show Gist options
  • Select an option

  • Save zerin108/886861be59cf8f769ec9 to your computer and use it in GitHub Desktop.

Select an option

Save zerin108/886861be59cf8f769ec9 to your computer and use it in GitHub Desktop.
Week 2. Mongo for Java Developers
Mongo shell is interactive javascript interpretor. Mongo shell can return previous input.
BSON is binary encoded a computer data interchange format used mainly as data storge and network transfer format in mongodb. Is based on the term json.
Inserting Docs
Documents in mongo live inside a collection. Inside shel language we see collections as properties of database.
findOne
method find one document that contains query argument
findOne(a, b), where a - what you want to find, b - what you want to display. example:
findOne({"name": "John"}, {name:true, _id:false}) display only name without id.
db.someCollection.find().pretty() display readable information to console
Queries in mongoshell in objects:
$gt = greater than, equals to >. example:
db.scores.find({score:{$gt:90}})
$gte = ">=",
$lt = "<",
$lte = "<=",
$or work with array. Return some when one of array elements exits in collection.
$and
$in is a key that work indently to $all, but return all documents that contains one or more of array elements.
$all is a key for array argument for searching documents that contains all of array elements.
$inc increment existing argument
$set can set and modyfy existing documents
$unset delete the field from the document
$pushAll add to array element all arr[i]
$push insert aone element
$pull remove one element
$addToSet
update
update(a, b) where "a" is an query for document that will be changed, "b" is property to change. But the old document with all properties will be removed and with new document replesed.
Each collection has method "remove".
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment