-
shell
- db
- use database
- insert
- batchInsert
- update
-
Upserts
- insert or update
- remove
-
save
- if _id is exist it will update otherwise
-
help
- db.help()
- db.mycoll.help()
- ...
-
show
- show dbs
- show collections
- show users
- ...
-
modifier
- $set
-
$inc
- Incrementing
-
decrementing
- Subtopic 1
-
modifier array
- $push
- $addToSet
- $each
- $pop
- $pull
- db.getLastError()
-
Query
-
find
-
example
- db.users.find({"username" : "joe", "age" : 27})
- Specifying Which Keys to Return
-
Criteria
- $lt
- $lte
- $gt
- $gte
- $or
- db.raffle.find({"$or" : [{"ticket_no" : 725}, {"winner" : true}]})
- $in
- db.raffle.find({"ticket_no" : {"$in" : [725, 542, 390]}})
- $not
- db.users.find({"id_num" : {"$not" : {"$mod" : [5, 1]}}})
- $mod
- db.users.find({"id_num" : {"$mod" : [5, 1]}})
-
Conditional Semantics
- db.users.find({"age" : {"$lt" : 30, "$gt" : 20}})
- db.users.find({"$and" : [{"x" : {"$lt" : 1}}, {"x" : 4}]})
-
null
- list all
- db.c.find()
-
array
- one of them
- $all
- match all
- $size
- $slice
-
Embedded Documents
- Subtopic 1
- $where
- sort
- limit
- skip
-
_addSpecial
- $maxscan
- $min
- $max
- $showDiskLoc
- explain
- hint
-
Server-Side Scripting
-
turn javascript off
- running mongod with the --noscripting option.
- use parameters like sql
-
Cursors
-
open
- var cursor = db.collection.find();
- cursor.hasNext()
- obj = cursor.next();
- forEach
-
database command
- drop
- getLastError
- shutdown
-
data type
- null
- boolean
- number
- string
- date
- regular expression
- array
- embedded document
- object id
- binary data
-
code
- JavaScript code:
-
Index
- ensureIndex
-
Aggregation
- aggregation framework lets you transform and combine documents in a collection
-
pipeline
-
filtering,
-
projecting,
- grouping,
- sorting,
- limiting,
- skipping.
- example
- limited to 16 MB
- debug?
-
function
- $match
-
$project
- "$fieldname"
-
Pipeline expressions
- Mathematical expressions.
- $add
- $subtract
- $multiply
- $divide
- $mod
- Date expressions.
- $year
- $month
- $week
- $dayOfMonth
- $dayOfWeek
- $dayOfYear
- $hour
- $minute
- $second
- String expressions.
- $substr
- $concat
- $toLower
- $toUpper
- Logical expressions.
- $cmp
- compare
- $strcasecmp
- "$eq"/"$ne"/"$gt"/"$gte"/"$lt"/"$lte"
- $and
- "$or"
- "$not"
- "$cond"
- "$ifNull"
-
$group
-
field name
- _id
- example
-
operator
- $sum
- $average
- $avg
- $max
- $min
- $first
- $last
-
Array operators.
- $addToSet
- $push
- $unwind
- $sort
- $limit
- $skip
-
MapReduce
-
syntax
-
db.[collection].mapReduce
-
mapfunction
- this is the current document
- return
- emit(key, value);
-
reduce function
- params
- key
- list of values above
- return newobject
-
{}
- out
- outputs the results to a collection
- query
- finalize