MongoDB
How Dates are saved
"date": new Date(2012,8,13) September not August since JavaScript counts month from zero Then MongoDB saves it in ISO format
Working with arrays... adding UNIQUE values to arrays
$addToSet update operator
Range queries on Arrays
$elemMatch - element match array query operator to make sure atleast 1 element matches the criteria specified
Query of comparisons in MongoDB What are the comparison query operators?
$gt - greater than $lt - less than $gte - greater than equal to $lte - less than equal to $ne - not equal to DONT FORGET THE $
More update operators
$max - updates if new value is greater than current $min - updates if new value is lesser than current $mul - updates field value * supplied value
Working with arrays... removing values from arrays
$pop update operator -1 pop first element 1 pop last element
How to remove values from an array
$pull update operator pull remove all instances of a matching value
Working with arrays... adding values to arrays
$push update operator
Collections and Documents
Collections are groups of documents documents exist independently and can have different fields. documents are JSON-like objects. documents are persisted in a format called BSON
What is NoSQL
Databases that are not relational and don't have a query language like SQL
Important note about update function
It only updates the FIRST MATCHING document
Can we have inclusion and exclusion specified in a projection?
No! you can have only inclusion or exclusion Except if you want to exclude ID field
Filtering in MongoDB
Passing multiple queries to find method
data types that can be stored are
String , boolean, null, numbers, Arrays , Object EXTRA : objectId, ISODate
What happens after we execute a query
We get a WriteResult object returned
How to access a value in an array when location of value is not known?
We have to use positional operator
What is the cursor object?
Whenever we search for documents, an object is returned from the find method known as the cursor object. Using find method you can only get the first 20 documents back We have to type 'it' for more documents
If update operators are not specified what happens?
Whole doc is overwritten with the update object
What can you do with projections
You can SELECT the fields you want OR EXCLUDE the fields you do not want
MongoDB Vs SQL
both store data in a database SQL uses tables | MongoDB uses collections Tables - rows | Collections - documents fixed schema | dynamic schema
Cursor methods
count() sort() skip() limit()
Command to know which database your using
db
how to retrieve all documents in a collection
db.<collection name>.find()
How to find particular documents in a collection
db.<collection name>.find({"key":"value"}) AKA "query of equality"
how to insert document
db.<collection name>.insert( { "<key>":"value" } ) Note : if the collection does not exist it will be created
Example of projections - exclusion
db.<collection-name>.find( {"grade" : {"$gte":80}}, //fields that you don't want to include in the output {"vendor" : false , "price" : false} )
Example for Range queries on Arrays
db.<collection-name>.find( {"sizes :{ "$elemMatch":{ "$gt":8 , "$lt":16} } })
Range queries on Arrays - odd behavior without $elemMatch
db.<collection-name>.find( {"sizes :{ "$gt":8 , "$lt":16} }) each value in the array is checked individually if atleast one array value is true for each criteria the entire document matches
Example of query using comparison operators for range of values
db.<collection-name>.find( { "price" : { "$gt" : 10 , "$lt" : 20 }})
Example of query using comparison operators
db.<collection-name>.find( { "price" : { "$lt" : 20 }})
Cursor methods for pagination
db.<collection-name>.find() .skip(<no. of docs you want to skip>) .limit(<no. of docs you want to retrieve>) //this approach becomes expensive for large collections
Example of sort() cursor method
db.<collection-name>.find().sort({"price":1}) 1 for ascending order -1 for descending order
How to remove documents from a collection
db.<collection-name>.remove({<specify key and value>});
How to update values in a document?
db.<collection-name>.update( {<query parameter>}, {<update parameter>})
Example using positional operator
db.potions.update( {"ingredients":"secret"}, \\ the value you want to update {"$set":{"ingredients . $ " : 42}}, {"multi" : true} )
Example of $pop update operator syntax
db.potions.update( {"name" : "Shrinking" }, {"$pop":{"category":1}} )
Example of $pull update operator
db.potions.update( {"name" : "Shrinking" }, {"$pull":{"category":"<value you want to remove>"}} )
Example of $push update operator syntax
db.potions.update( {"name" : "Shrinking" }, {"$push":{"category":"<value you want to push>"}} )
Example of $rename update operator syntax
db.potions.update( {}, {"$rename":{"old-field-name":"new-field-name"}}, {"multi":true} )
Example of $unset update operator syntax
db.potions.update( {}, // empty query parameter means select all documents {"$unset":{"color":""}}, {"multi": true} )
MongoDB unique id
each document must have a unique id "_id"
What are Projections?
find() method has a 2nd parameter that takes field-boolean_value pairs to specify which fields we want in the output By default document Ids are always returned
What WriteResult after an update
nMatched - number of docs matched nUpserted - no. of docs created nModified - no. of docs modifed
finding documents that contain embedded documents
need to use dot notation
What constraints MongoDB checks
no other doc has the same id no syntax errors document is less than 16 mb
What is MongoDB
opensource nosql document-oriented database . Good for lots of unstructured data. (access through shell using javascript)
What is positional operator
positional operator is a placeholder that will set the proper position of the value specified in the query parameter <field>.$ - placeholder
finding documents based on data in array
same as query of equality
command to see all databases and their sizes
show dbs
Command to Access or Create database
use <database name>
How to rename a field in a collection
using $rename update operator
how to remove fields from a collection
using $unset update operator
How to access a value in an array? Location of value is known
using dot notation and index position
What do you do in order to update all matching documents
we need to pass the Options parameter {"multi" : true}
What do you do if the query parameter does not find any matches but still want the update value to get inserted?
we need to pass the Options parameter {"upsert" : true} creates a document using the values in the query and update parameters
how to write update parameters
{"$<update operator>" : {<key>:<value>}} update operators always begin with $ Example $set
Which update operator to use to increment value of a field
{"$inc":{"<field>":<increment by>}}