项目作者: Clivern

项目描述 :
:books: It's worth to give it a try
高级语言:
项目地址: git://github.com/Clivern/MongoPlaybook.git
创建时间: 2016-05-02T12:44:46Z
项目社区:https://github.com/Clivern/MongoPlaybook

开源协议:

下载


Run Mongo

  1. > mongo

Import Collection

  1. > mongoimport --db test --collection restaurants --drop --file primer-dataset.json

Show All Databases

  1. > show databases

Get Current Database

  1. > db

Use Another Database

  1. > use test

Show Database Collections

  1. > show collections

Datatypes

  1. {'x': null} # null
  2. {'x': true} # Boolean
  3. {'x': 3} # Integer
  4. {'x': 3.2} # Float Point
  5. {'x': 'hello world'} # String
  6. {'x': new Date()} # Date
  7. {'x': /foobar/i} # Regex
  8. {'x': ['a','b','c']} # Array
  9. {'x': {'foo':'bar'}} # Embedded Documents
  10. {'x': ObjectId()} # Id

Counting Collection Records

  1. > db.restaurants.count()
  2. 25359

Insert Record

  1. > db.posts.insert({'title': 'hello world', 'author': 5, 'created_at': new Date()})
  2. WriteResult({ "nInserted" : 1 })

Batch Insert

  1. > db.posts.insert([{'title': 'hello world1', 'author': 6, 'created_at': new Date()}, {'title': 'hello world2', 'author': 7, 'created_at': new Date()}])
  2. BulkWriteResult({
  3. "writeErrors" : [ ],
  4. "writeConcernErrors" : [ ],
  5. "nInserted" : 2,
  6. "nUpserted" : 0,
  7. "nMatched" : 0,
  8. "nModified" : 0,
  9. "nRemoved" : 0,
  10. "upserted" : [ ]
  11. })

Find All

  1. > db.posts.find()
  2. { "_id" : ObjectId("571bde257c1d1340ef01c29f"), "title" : "hello world", "author" : 5, "created_at" : ISODate("2016-04-23T20:42:13.971Z") }
  3. { "_id" : ObjectId("571bdf367c1d1340ef01c2a0"), "title" : "hello world1", "author" : 6, "created_at" : ISODate("2016-04-23T20:46:46.132Z") }
  4. { "_id" : ObjectId("571bdf367c1d1340ef01c2a1"), "title" : "hello world2", "author" : 7, "created_at" : ISODate("2016-04-23T20:46:46.132Z") }

Remove With Query

  1. > db.posts.find()
  2. { "_id" : ObjectId("571bde257c1d1340ef01c29f"), "title" : "hello world", "author" : 5, "created_at" : ISODate("2016-04-23T20:42:13.971Z") }
  3. { "_id" : ObjectId("571bdf367c1d1340ef01c2a0"), "title" : "hello world1", "author" : 6, "created_at" : ISODate("2016-04-23T20:46:46.132Z") }
  4. { "_id" : ObjectId("571bdf367c1d1340ef01c2a1"), "title" : "hello world2", "author" : 7, "created_at" : ISODate("2016-04-23T20:46:46.132Z") }
  5. > db.posts.remove({'title':'hello world'})
  6. WriteResult({ "nRemoved" : 1 })
  7. > db.posts.find()
  8. { "_id" : ObjectId("571bdf367c1d1340ef01c2a0"), "title" : "hello world1", "author" : 6, "created_at" : ISODate("2016-04-23T20:46:46.132Z") }
  9. { "_id" : ObjectId("571bdf367c1d1340ef01c2a1"), "title" : "hello world2", "author" : 7, "created_at" : ISODate("2016-04-23T20:46:46.132Z") }

Find One

  1. > db.posts.findOne({'title':'hello world'})
  2. null
  3. > db.posts.findOne({'title':'hello world1'})
  4. {
  5. "_id" : ObjectId("571bdf367c1d1340ef01c2a0"),
  6. "title" : "hello world1",
  7. "author" : 6,
  8. "created_at" : ISODate("2016-04-23T20:46:46.132Z")
  9. }

Find All

  1. > db.posts.find()
  2. { "_id" : ObjectId("571bdf367c1d1340ef01c2a0"), "title" : "hello world1", "author" : 6, "created_at" : ISODate("2016-04-23T20:46:46.132Z") }
  3. { "_id" : ObjectId("571bdf367c1d1340ef01c2a1"), "title" : "hello world2", "author" : 7, "created_at" : ISODate("2016-04-23T20:46:46.132Z") }
  4. > db.posts.find({'author':6})
  5. { "_id" : ObjectId("571bdf367c1d1340ef01c2a0"), "title" : "hello world1", "author" : 6, "created_at" : ISODate("2016-04-23T20:46:46.132Z") }

Replace Document

  1. > post = db.posts.findOne()
  2. {
  3. "_id" : ObjectId("571bdf367c1d1340ef01c2a0"),
  4. "title" : "hello world1",
  5. "author" : 6,
  6. "created_at" : ISODate("2016-04-23T20:46:46.132Z")
  7. }
  8. > post.title = "new hello world"
  9. new hello world
  10. > post
  11. {
  12. "_id" : ObjectId("571bdf367c1d1340ef01c2a0"),
  13. "title" : "new hello world",
  14. "author" : 6,
  15. "created_at" : ISODate("2016-04-23T20:46:46.132Z")
  16. }
  17. > db.posts.update({'title': 'hello world1'}, post)
  18. WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
  19. > db.posts.findOne()
  20. {
  21. "_id" : ObjectId("571bdf367c1d1340ef01c2a0"),
  22. "title" : "new hello world",
  23. "author" : 6,
  24. "created_at" : ISODate("2016-04-23T20:46:46.132Z")
  25. }

Using Modifiers

$inc Modifier

  1. > post = db.posts.findOne()
  2. {
  3. "_id" : ObjectId("571bdf367c1d1340ef01c2a0"),
  4. "title" : "new hello world",
  5. "author" : 6,
  6. "created_at" : ISODate("2016-04-23T20:46:46.132Z")
  7. }
  8. > post.views = 0
  9. 0
  10. > db.posts.findOne()
  11. {
  12. "_id" : ObjectId("571bdf367c1d1340ef01c2a0"),
  13. "title" : "new hello world",
  14. "author" : 6,
  15. "created_at" : ISODate("2016-04-23T20:46:46.132Z")
  16. }
  17. > db.posts.update({'title': 'new hello world'}, post)
  18. WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
  19. > db.posts.findOne()
  20. {
  21. "_id" : ObjectId("571bdf367c1d1340ef01c2a0"),
  22. "title" : "new hello world",
  23. "author" : 6,
  24. "created_at" : ISODate("2016-04-23T20:46:46.132Z"),
  25. "views" : 0
  26. }
  27. > db.posts.update({'title': 'new hello world'}, {'$inc':{'views': 1}})
  28. WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
  29. > db.posts.findOne()
  30. {
  31. "_id" : ObjectId("571bdf367c1d1340ef01c2a0"),
  32. "title" : "new hello world",
  33. "author" : 6,
  34. "created_at" : ISODate("2016-04-23T20:46:46.132Z"),
  35. "views" : 1
  36. }
  37. > db.posts.update({'title': 'new hello world'}, {'$inc':{'views': -1}})
  38. WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
  39. > db.posts.findOne()
  40. {
  41. "_id" : ObjectId("571bdf367c1d1340ef01c2a0"),
  42. "title" : "new hello world",
  43. "author" : 6,
  44. "created_at" : ISODate("2016-04-23T20:46:46.132Z"),
  45. "views" : 0
  46. }
  47. > db.posts.update({'title': 'new hello world'}, {'$inc':{'views': 50}})
  48. WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
  49. > db.posts.findOne()
  50. {
  51. "_id" : ObjectId("571bdf367c1d1340ef01c2a0"),
  52. "title" : "new hello world",
  53. "author" : 6,
  54. "created_at" : ISODate("2016-04-23T20:46:46.132Z"),
  55. "views" : 50
  56. }

$set Modifier

  1. > db.posts.update({'title': 'new hello world'}, {'$set':{'comments': []}})
  2. WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
  3. > db.posts.findOne()
  4. {
  5. "_id" : ObjectId("571bdf367c1d1340ef01c2a0"),
  6. "title" : "new hello world",
  7. "author" : 6,
  8. "created_at" : ISODate("2016-04-23T20:46:46.132Z"),
  9. "views" : 1,
  10. "comments" : [ ]
  11. }

$unset Modifier

  1. > db.posts.update({'title': 'new hello world'}, {'$unset':{'comments': 1}})
  2. WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
  3. > db.posts.findOne()
  4. {
  5. "_id" : ObjectId("571bdf367c1d1340ef01c2a0"),
  6. "title" : "new hello world",
  7. "author" : 6,
  8. "created_at" : ISODate("2016-04-23T20:46:46.132Z"),
  9. "views" : 1
  10. }

$push Modifier

  1. > db.posts.update({'title': 'new hello world'}, {'$set':{'comments': []}})
  2. WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
  3. > db.posts.findOne()
  4. {
  5. "_id" : ObjectId("571bdf367c1d1340ef01c2a0"),
  6. "title" : "new hello world",
  7. "author" : 6,
  8. "created_at" : ISODate("2016-04-23T20:46:46.132Z"),
  9. "views" : 50,
  10. "comments" : [ ]
  11. }
  12. > db.posts.update({'title': 'new hello world'}, {'$push':{'comments': {'author': 'hello@clivern.com', 'comment': 'bla bla bla', 'created_at': new Date()}}})
  13. WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
  14. > db.posts.findOne()
  15. {
  16. "_id" : ObjectId("571bdf367c1d1340ef01c2a0"),
  17. "title" : "new hello world",
  18. "author" : 6,
  19. "created_at" : ISODate("2016-04-23T20:46:46.132Z"),
  20. "views" : 50,
  21. "comments" : [
  22. {
  23. "author" : "hello@clivern.com",
  24. "comment" : "bla bla bla",
  25. "created_at" : ISODate("2016-04-23T21:27:51.863Z")
  26. }
  27. ]
  28. }

$push and $each Modifier

  1. > db.posts.update({'title': 'new hello world'}, {'$push':{'comments': {'$each':[{'author': 'hello1@clivern.com', 'comment': 'bla bla bla1', 'created_at': new Date()}, {'author': 'hello2@clivern.com', 'comment': 'bla bla bla2', 'created_at': new Date()}, {'author': 'hello@clivern.com3', 'comment': 'bla bla bla3', 'created_at': new Date()}]}}})
  2. WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
  3. > db.posts.findOne()
  4. {
  5. "_id" : ObjectId("571bdf367c1d1340ef01c2a0"),
  6. "title" : "new hello world",
  7. "author" : 6,
  8. "created_at" : ISODate("2016-04-23T20:46:46.132Z"),
  9. "views" : 50,
  10. "comments" : [
  11. {
  12. "author" : "hello@clivern.com",
  13. "comment" : "bla bla bla",
  14. "created_at" : ISODate("2016-04-23T21:27:51.863Z")
  15. },
  16. {
  17. "author" : "hello1@clivern.com",
  18. "comment" : "bla bla bla1",
  19. "created_at" : ISODate("2016-04-23T21:30:21.597Z")
  20. },
  21. {
  22. "author" : "hello2@clivern.com",
  23. "comment" : "bla bla bla2",
  24. "created_at" : ISODate("2016-04-23T21:30:21.597Z")
  25. },
  26. {
  27. "author" : "hello@clivern.com3",
  28. "comment" : "bla bla bla3",
  29. "created_at" : ISODate("2016-04-23T21:30:21.597Z")
  30. }
  31. ]
  32. }

$push, $each and $slice Modifier

  1. > db.posts.update({'title': 'new hello world'}, {'$push':{'comments': {'$each':[{'author': 'hello1@clivern.com', 'comment': 'bla bla bla1', 'created_at': new Date()}, {'author': 'hello2@clivern.com', 'comment': 'bla bla bla2', 'created_at': new Date()}, {'author': 'hello@clivern.com3', 'comment': 'bla bla bla3', 'created_at': new Date()}]}}})
  2. WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
  3. > db.posts.findOne()
  4. {
  5. "_id" : ObjectId("571bdf367c1d1340ef01c2a0"),
  6. "title" : "new hello world",
  7. "author" : 6,
  8. "created_at" : ISODate("2016-04-23T20:46:46.132Z"),
  9. "views" : 50,
  10. "comments" : [
  11. {
  12. "author" : "hello@clivern.com",
  13. "comment" : "bla bla bla",
  14. "created_at" : ISODate("2016-04-23T21:27:51.863Z")
  15. },
  16. {
  17. "author" : "hello1@clivern.com",
  18. "comment" : "bla bla bla1",
  19. "created_at" : ISODate("2016-04-23T21:30:21.597Z")
  20. },
  21. {
  22. "author" : "hello2@clivern.com",
  23. "comment" : "bla bla bla2",
  24. "created_at" : ISODate("2016-04-23T21:30:21.597Z")
  25. },
  26. {
  27. "author" : "hello@clivern.com3",
  28. "comment" : "bla bla bla3",
  29. "created_at" : ISODate("2016-04-23T21:30:21.597Z")
  30. }
  31. ]
  32. }
  33. > db.posts.update({'title': 'new hello world'}, {'$push':{'comments': {'$each':[{'author': 'hello1@clivern.com', 'comment': 'bla bla bla1', 'created_at': new Date()}, {'author': 'hello2@clivern.com', 'comment': 'bla bla bla2', 'created_at': new Date()}, {'author': 'hello@clivern.com3', 'comment': 'bla bla bla3', 'created_at': new Date()}], '$slice': -4}}})
  34. WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
  35. > db.posts.findOne()
  36. {
  37. "_id" : ObjectId("571bdf367c1d1340ef01c2a0"),
  38. "title" : "new hello world",
  39. "author" : 6,
  40. "created_at" : ISODate("2016-04-23T20:46:46.132Z"),
  41. "views" : 50,
  42. "comments" : [
  43. {
  44. "author" : "hello@clivern.com3",
  45. "comment" : "bla bla bla3",
  46. "created_at" : ISODate("2016-04-23T21:30:21.597Z")
  47. },
  48. {
  49. "author" : "hello1@clivern.com",
  50. "comment" : "bla bla bla1",
  51. "created_at" : ISODate("2016-04-23T21:33:50.653Z")
  52. },
  53. {
  54. "author" : "hello2@clivern.com",
  55. "comment" : "bla bla bla2",
  56. "created_at" : ISODate("2016-04-23T21:33:50.653Z")
  57. },
  58. {
  59. "author" : "hello@clivern.com3",
  60. "comment" : "bla bla bla3",
  61. "created_at" : ISODate("2016-04-23T21:33:50.653Z")
  62. }
  63. ]
  64. }

$push, $each, $slice and $sort Modifier

  1. > db.posts.update({'title': 'new hello world'}, {'$push':{'comments': {'$each':[{'author': 'hello1@clivern.com', 'comment': 'bla bla bla1', 'created_at': new Date()}, {'author': 'hello2@clivern.com', 'comment': 'bla bla bla2', 'created_at': new Date()}, {'author': 'hello@clivern.com3', 'comment': 'bla bla bla3', 'created_at': new Date()}], '$slice': -4}}})
  2. WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
  3. > db.posts.findOne()
  4. {
  5. "_id" : ObjectId("571bdf367c1d1340ef01c2a0"),
  6. "title" : "new hello world",
  7. "author" : 6,
  8. "created_at" : ISODate("2016-04-23T20:46:46.132Z"),
  9. "views" : 50,
  10. "comments" : [
  11. {
  12. "author" : "hello@clivern.com3",
  13. "comment" : "bla bla bla3",
  14. "created_at" : ISODate("2016-04-23T21:35:56.109Z")
  15. },
  16. {
  17. "author" : "hello1@clivern.com",
  18. "comment" : "bla bla bla1",
  19. "created_at" : ISODate("2016-04-23T21:36:12.697Z")
  20. },
  21. {
  22. "author" : "hello2@clivern.com",
  23. "comment" : "bla bla bla2",
  24. "created_at" : ISODate("2016-04-23T21:36:12.697Z")
  25. },
  26. {
  27. "author" : "hello@clivern.com3",
  28. "comment" : "bla bla bla3",
  29. "created_at" : ISODate("2016-04-23T21:36:12.697Z")
  30. }
  31. ]
  32. }
  33. > db.posts.update({'title': 'new hello world'}, {'$push':{'comments': {'$each':[{'author': 'hello1@clivern.com', 'comment': 'bla bla bla1', 'created_at': new Date()}, {'author': 'hello2@clivern.com', 'comment': 'bla bla bla2', 'created_at': new Date()}, {'author': 'hello@clivern.com3', 'comment': 'bla bla bla3', 'created_at': new Date()}], '$slice': -4, '$sort': {'author': 1}}}})
  34. WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
  35. > db.posts.findOne()
  36. {
  37. "_id" : ObjectId("571bdf367c1d1340ef01c2a0"),
  38. "title" : "new hello world",
  39. "author" : 6,
  40. "created_at" : ISODate("2016-04-23T20:46:46.132Z"),
  41. "views" : 50,
  42. "comments" : [
  43. {
  44. "author" : "hello2@clivern.com",
  45. "comment" : "bla bla bla2",
  46. "created_at" : ISODate("2016-04-23T21:36:21.669Z")
  47. },
  48. {
  49. "author" : "hello@clivern.com3",
  50. "comment" : "bla bla bla3",
  51. "created_at" : ISODate("2016-04-23T21:35:56.109Z")
  52. },
  53. {
  54. "author" : "hello@clivern.com3",
  55. "comment" : "bla bla bla3",
  56. "created_at" : ISODate("2016-04-23T21:36:12.697Z")
  57. },
  58. {
  59. "author" : "hello@clivern.com3",
  60. "comment" : "bla bla bla3",
  61. "created_at" : ISODate("2016-04-23T21:36:21.669Z")
  62. }
  63. ]
  64. }

$addToSet Modifier

  1. > db.posts.findOne()
  2. {
  3. "_id" : ObjectId("571bdf367c1d1340ef01c2a0"),
  4. "title" : "new hello world",
  5. "author" : 6,
  6. "created_at" : ISODate("2016-04-23T20:46:46.132Z"),
  7. "views" : 50,
  8. "comments" : [
  9. {
  10. "author" : "hello2@clivern.com",
  11. "comment" : "bla bla bla2",
  12. "created_at" : ISODate("2016-04-23T21:36:21.669Z")
  13. },
  14. {
  15. "author" : "hello@clivern.com3",
  16. "comment" : "bla bla bla3",
  17. "created_at" : ISODate("2016-04-23T21:35:56.109Z")
  18. },
  19. {
  20. "author" : "hello@clivern.com3",
  21. "comment" : "bla bla bla3",
  22. "created_at" : ISODate("2016-04-23T21:36:12.697Z")
  23. },
  24. {
  25. "author" : "hello@clivern.com3",
  26. "comment" : "bla bla bla3",
  27. "created_at" : ISODate("2016-04-23T21:36:21.669Z")
  28. }
  29. ]
  30. }
  31. > db.posts.update({'title': 'new hello world'}, {'$set':{'tags':['php']}})
  32. WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
  33. > db.posts.findOne()
  34. {
  35. "_id" : ObjectId("571bdf367c1d1340ef01c2a0"),
  36. "title" : "new hello world",
  37. "author" : 6,
  38. "created_at" : ISODate("2016-04-23T20:46:46.132Z"),
  39. "views" : 50,
  40. "comments" : [
  41. {
  42. "author" : "hello2@clivern.com",
  43. "comment" : "bla bla bla2",
  44. "created_at" : ISODate("2016-04-23T21:36:21.669Z")
  45. },
  46. {
  47. "author" : "hello@clivern.com3",
  48. "comment" : "bla bla bla3",
  49. "created_at" : ISODate("2016-04-23T21:35:56.109Z")
  50. },
  51. {
  52. "author" : "hello@clivern.com3",
  53. "comment" : "bla bla bla3",
  54. "created_at" : ISODate("2016-04-23T21:36:12.697Z")
  55. },
  56. {
  57. "author" : "hello@clivern.com3",
  58. "comment" : "bla bla bla3",
  59. "created_at" : ISODate("2016-04-23T21:36:21.669Z")
  60. }
  61. ],
  62. "tags" : [
  63. "php"
  64. ]
  65. }
  66. > db.posts.update({'title': 'new hello world'}, {'$addToSet':{'tags':'php'}})
  67. WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 0 })
  68. > db.posts.findOne()
  69. {
  70. "_id" : ObjectId("571bdf367c1d1340ef01c2a0"),
  71. "title" : "new hello world",
  72. "author" : 6,
  73. "created_at" : ISODate("2016-04-23T20:46:46.132Z"),
  74. "views" : 50,
  75. "comments" : [
  76. {
  77. "author" : "hello2@clivern.com",
  78. "comment" : "bla bla bla2",
  79. "created_at" : ISODate("2016-04-23T21:36:21.669Z")
  80. },
  81. {
  82. "author" : "hello@clivern.com3",
  83. "comment" : "bla bla bla3",
  84. "created_at" : ISODate("2016-04-23T21:35:56.109Z")
  85. },
  86. {
  87. "author" : "hello@clivern.com3",
  88. "comment" : "bla bla bla3",
  89. "created_at" : ISODate("2016-04-23T21:36:12.697Z")
  90. },
  91. {
  92. "author" : "hello@clivern.com3",
  93. "comment" : "bla bla bla3",
  94. "created_at" : ISODate("2016-04-23T21:36:21.669Z")
  95. }
  96. ],
  97. "tags" : [
  98. "php"
  99. ]
  100. }
  101. > db.posts.update({'title': 'new hello world'}, {'$addToSet':{'tags':'jQuery'}})
  102. WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
  103. > db.posts.findOne()
  104. {
  105. "_id" : ObjectId("571bdf367c1d1340ef01c2a0"),
  106. "title" : "new hello world",
  107. "author" : 6,
  108. "created_at" : ISODate("2016-04-23T20:46:46.132Z"),
  109. "views" : 50,
  110. "comments" : [
  111. {
  112. "author" : "hello2@clivern.com",
  113. "comment" : "bla bla bla2",
  114. "created_at" : ISODate("2016-04-23T21:36:21.669Z")
  115. },
  116. {
  117. "author" : "hello@clivern.com3",
  118. "comment" : "bla bla bla3",
  119. "created_at" : ISODate("2016-04-23T21:35:56.109Z")
  120. },
  121. {
  122. "author" : "hello@clivern.com3",
  123. "comment" : "bla bla bla3",
  124. "created_at" : ISODate("2016-04-23T21:36:12.697Z")
  125. },
  126. {
  127. "author" : "hello@clivern.com3",
  128. "comment" : "bla bla bla3",
  129. "created_at" : ISODate("2016-04-23T21:36:21.669Z")
  130. }
  131. ],
  132. "tags" : [
  133. "php",
  134. "jQuery"
  135. ]
  136. }

$pull Modifier

  1. > db.posts.findOne()
  2. {
  3. "_id" : ObjectId("571bdf367c1d1340ef01c2a0"),
  4. "title" : "new hello world",
  5. "author" : 6,
  6. "created_at" : ISODate("2016-04-23T20:46:46.132Z"),
  7. "views" : 50,
  8. "comments" : [
  9. {
  10. "author" : "hello2@clivern.com",
  11. "comment" : "bla bla bla2",
  12. "created_at" : ISODate("2016-04-23T21:36:21.669Z")
  13. },
  14. {
  15. "author" : "hello@clivern.com3",
  16. "comment" : "bla bla bla3",
  17. "created_at" : ISODate("2016-04-23T21:35:56.109Z")
  18. },
  19. {
  20. "author" : "hello@clivern.com3",
  21. "comment" : "bla bla bla3",
  22. "created_at" : ISODate("2016-04-23T21:36:12.697Z")
  23. },
  24. {
  25. "author" : "hello@clivern.com3",
  26. "comment" : "bla bla bla3",
  27. "created_at" : ISODate("2016-04-23T21:36:21.669Z")
  28. }
  29. ],
  30. "tags" : [
  31. "php",
  32. "jQuery"
  33. ]
  34. }
  35. > db.posts.update({'title': 'new hello world'}, {'$pull':{'tags':'jQuery'}})
  36. WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
  37. > db.posts.findOne()
  38. {
  39. "_id" : ObjectId("571bdf367c1d1340ef01c2a0"),
  40. "title" : "new hello world",
  41. "author" : 6,
  42. "created_at" : ISODate("2016-04-23T20:46:46.132Z"),
  43. "views" : 50,
  44. "comments" : [
  45. {
  46. "author" : "hello2@clivern.com",
  47. "comment" : "bla bla bla2",
  48. "created_at" : ISODate("2016-04-23T21:36:21.669Z")
  49. },
  50. {
  51. "author" : "hello@clivern.com3",
  52. "comment" : "bla bla bla3",
  53. "created_at" : ISODate("2016-04-23T21:35:56.109Z")
  54. },
  55. {
  56. "author" : "hello@clivern.com3",
  57. "comment" : "bla bla bla3",
  58. "created_at" : ISODate("2016-04-23T21:36:12.697Z")
  59. },
  60. {
  61. "author" : "hello@clivern.com3",
  62. "comment" : "bla bla bla3",
  63. "created_at" : ISODate("2016-04-23T21:36:21.669Z")
  64. }
  65. ],
  66. "tags" : [
  67. "php"
  68. ]
  69. }

Positional Edits

  1. > db.posts.findOne()
  2. {
  3. "_id" : ObjectId("571bdf367c1d1340ef01c2a0"),
  4. "title" : "new hello world",
  5. "author" : 6,
  6. "created_at" : ISODate("2016-04-23T20:46:46.132Z"),
  7. "views" : 50,
  8. "comments" : [
  9. {
  10. "author" : "hello2@clivern.com",
  11. "comment" : "bla bla bla2",
  12. "created_at" : ISODate("2016-04-23T21:36:21.669Z")
  13. },
  14. {
  15. "author" : "hello@clivern.com3",
  16. "comment" : "bla bla bla3",
  17. "created_at" : ISODate("2016-04-23T21:35:56.109Z")
  18. },
  19. {
  20. "author" : "hello@clivern.com3",
  21. "comment" : "bla bla bla3",
  22. "created_at" : ISODate("2016-04-23T21:36:12.697Z")
  23. },
  24. {
  25. "author" : "hello@clivern.com3",
  26. "comment" : "bla bla bla3",
  27. "created_at" : ISODate("2016-04-23T21:36:21.669Z")
  28. }
  29. ],
  30. "tags" : [
  31. "php"
  32. ]
  33. }
  34. > db.posts.update({'title': 'new hello world'}, {'$set':{'comments.0.votes':20}})
  35. WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
  36. > db.posts.findOne()
  37. {
  38. "_id" : ObjectId("571bdf367c1d1340ef01c2a0"),
  39. "title" : "new hello world",
  40. "author" : 6,
  41. "created_at" : ISODate("2016-04-23T20:46:46.132Z"),
  42. "views" : 50,
  43. "comments" : [
  44. {
  45. "author" : "hello2@clivern.com",
  46. "comment" : "bla bla bla2",
  47. "created_at" : ISODate("2016-04-23T21:36:21.669Z"),
  48. "votes" : 20
  49. },
  50. {
  51. "author" : "hello@clivern.com3",
  52. "comment" : "bla bla bla3",
  53. "created_at" : ISODate("2016-04-23T21:35:56.109Z")
  54. },
  55. {
  56. "author" : "hello@clivern.com3",
  57. "comment" : "bla bla bla3",
  58. "created_at" : ISODate("2016-04-23T21:36:12.697Z")
  59. },
  60. {
  61. "author" : "hello@clivern.com3",
  62. "comment" : "bla bla bla3",
  63. "created_at" : ISODate("2016-04-23T21:36:21.669Z")
  64. }
  65. ],
  66. "tags" : [
  67. "php"
  68. ]
  69. }

Update With Upsert is True

  1. > db.posts.update({'title': 'new world with upsert'}, {'$set':{'comments':[]}}, true)
  2. WriteResult({
  3. "nMatched" : 0,
  4. "nUpserted" : 1,
  5. "nModified" : 0,
  6. "_id" : ObjectId("571bed5c9c6a01cfb58206e8")
  7. })
  8. > db.posts.findOne({'title': 'new world with upsert'})
  9. {
  10. "_id" : ObjectId("571bed5c9c6a01cfb58206e8"),
  11. "title" : "new world with upsert",
  12. "comments" : [ ]
  13. }
  14. > db.posts.update({'title': 'new world with upsert'}, {'$setOnInsert':{'comments':[]}}, true)
  15. WriteResult({
  16. "nMatched" : 0,
  17. "nUpserted" : 1,
  18. "nModified" : 0,
  19. "_id" : ObjectId("571bed5c9c6a01cfb58206e8")
  20. })
  21. > db.posts.findOne({'title': 'new world with upsert'})
  22. {
  23. "_id" : ObjectId("571bed5c9c6a01cfb58206e8"),
  24. "title" : "new world with upsert",
  25. "comments" : [ ]
  26. }

Remove All Documents in Collection

  1. > db.posts.remove({})
  2. WriteResult({ "nRemoved" : 5 })

Querying

Bulk Insert

  1. > db.users.insert([{'_id': 1, 'first_name': 'Mark', 'last_name': 'Doe', 'username': 'admin', 'email': 'admin@clivern.com', 'job_title': 'CEO', 'created_at': new Date(), 'updated_at': new Date()}, {'_id': 2, 'first_name': 'Selena', 'last_name': 'Gomez', 'username': 'selena', 'email': 'selena@clivern.com', 'job_title': 'Backend Developer', 'created_at': new Date(), 'updated_at': new Date()}, {'_id': 3, 'first_name': 'Helen', 'last_name': 'Otal', 'username': 'helen', 'email': 'helen@clivern.com', 'job_title': 'Web Designer', 'created_at': new Date(), 'updated_at': new Date()}, {'_id': 4, 'first_name': 'Joe', 'last_name': 'Mar', 'username': 'joe', 'email': 'joe@clivern.com', 'job_title': 'Support Agent', 'created_at': new Date(), 'updated_at': new Date()}])
  2. BulkWriteResult({
  3. "writeErrors" : [ ],
  4. "writeConcernErrors" : [ ],
  5. "nInserted" : 4,
  6. "nUpserted" : 0,
  7. "nMatched" : 0,
  8. "nModified" : 0,
  9. "nRemoved" : 0,
  10. "upserted" : [ ]
  11. })

Find All

  1. > db.users.find({}).pretty()
  2. {
  3. "_id" : 1,
  4. "first_name" : "Mark",
  5. "last_name" : "Doe",
  6. "username" : "admin",
  7. "email" : "admin@clivern.com",
  8. "job_title" : "CEO",
  9. "created_at" : ISODate("2016-04-24T17:52:24.040Z"),
  10. "updated_at" : ISODate("2016-04-24T17:52:24.040Z")
  11. }
  12. {
  13. "_id" : 2,
  14. "first_name" : "Selena",
  15. "last_name" : "Gomez",
  16. "username" : "selena",
  17. "email" : "selena@clivern.com",
  18. "job_title" : "Backend Developer",
  19. "created_at" : ISODate("2016-04-24T17:52:24.040Z"),
  20. "updated_at" : ISODate("2016-04-24T17:52:24.040Z")
  21. }
  22. {
  23. "_id" : 3,
  24. "first_name" : "Helen",
  25. "last_name" : "Otal",
  26. "username" : "helen",
  27. "email" : "helen@clivern.com",
  28. "job_title" : "Web Designer",
  29. "created_at" : ISODate("2016-04-24T17:52:24.040Z"),
  30. "updated_at" : ISODate("2016-04-24T17:52:24.040Z")
  31. }
  32. {
  33. "_id" : 4,
  34. "first_name" : "Joe",
  35. "last_name" : "Mar",
  36. "username" : "joe",
  37. "email" : "joe@clivern.com",
  38. "job_title" : "Support Agent",
  39. "created_at" : ISODate("2016-04-24T17:52:24.040Z"),
  40. "updated_at" : ISODate("2016-04-24T17:52:24.040Z")
  41. }

Exclude Keys

  1. > db.users.find({}, {'created_at': 0, 'updated_at': 0}).pretty()
  2. {
  3. "_id" : 1,
  4. "first_name" : "Mark",
  5. "last_name" : "Doe",
  6. "username" : "admin",
  7. "email" : "admin@clivern.com",
  8. "job_title" : "CEO"
  9. }
  10. {
  11. "_id" : 2,
  12. "first_name" : "Selena",
  13. "last_name" : "Gomez",
  14. "username" : "selena",
  15. "email" : "selena@clivern.com",
  16. "job_title" : "Backend Developer"
  17. }
  18. {
  19. "_id" : 3,
  20. "first_name" : "Helen",
  21. "last_name" : "Otal",
  22. "username" : "helen",
  23. "email" : "helen@clivern.com",
  24. "job_title" : "Web Designer"
  25. }
  26. {
  27. "_id" : 4,
  28. "first_name" : "Joe",
  29. "last_name" : "Mar",
  30. "username" : "joe",
  31. "email" : "joe@clivern.com",
  32. "job_title" : "Support Agent"
  33. }

Query Conditionals

  1. > db.users.find({'username': 'admin'}, {'created_at': 0, 'updated_at': 0}).pretty()
  2. {
  3. "_id" : 1,
  4. "first_name" : "Mark",
  5. "last_name" : "Doe",
  6. "username" : "admin",
  7. "email" : "admin@clivern.com",
  8. "job_title" : "CEO"
  9. }
  10. > db.users.find({'_id': {'$gt': 2}}, {'created_at': 0, 'updated_at': 0}).pretty()
  11. {
  12. "_id" : 3,
  13. "first_name" : "Helen",
  14. "last_name" : "Otal",
  15. "username" : "helen",
  16. "email" : "helen@clivern.com",
  17. "job_title" : "Web Designer"
  18. }
  19. {
  20. "_id" : 4,
  21. "first_name" : "Joe",
  22. "last_name" : "Mar",
  23. "username" : "joe",
  24. "email" : "joe@clivern.com",
  25. "job_title" : "Support Agent"
  26. }
  27. > db.users.find({'_id': {'$gte': 2}}, {'created_at': 0, 'updated_at': 0}).pretty()
  28. {
  29. "_id" : 2,
  30. "first_name" : "Selena",
  31. "last_name" : "Gomez",
  32. "username" : "selena",
  33. "email" : "selena@clivern.com",
  34. "job_title" : "Backend Developer"
  35. }
  36. {
  37. "_id" : 3,
  38. "first_name" : "Helen",
  39. "last_name" : "Otal",
  40. "username" : "helen",
  41. "email" : "helen@clivern.com",
  42. "job_title" : "Web Designer"
  43. }
  44. {
  45. "_id" : 4,
  46. "first_name" : "Joe",
  47. "last_name" : "Mar",
  48. "username" : "joe",
  49. "email" : "joe@clivern.com",
  50. "job_title" : "Support Agent"
  51. }
  52. > db.users.find({'_id': {'$lt': 2}}, {'created_at': 0, 'updated_at': 0}).pretty()
  53. {
  54. "_id" : 1,
  55. "first_name" : "Mark",
  56. "last_name" : "Doe",
  57. "username" : "admin",
  58. "email" : "admin@clivern.com",
  59. "job_title" : "CEO"
  60. }
  61. > db.users.find({'_id': {'$lte': 2}}, {'created_at': 0, 'updated_at': 0}).pretty()
  62. {
  63. "_id" : 1,
  64. "first_name" : "Mark",
  65. "last_name" : "Doe",
  66. "username" : "admin",
  67. "email" : "admin@clivern.com",
  68. "job_title" : "CEO"
  69. }
  70. {
  71. "_id" : 2,
  72. "first_name" : "Selena",
  73. "last_name" : "Gomez",
  74. "username" : "selena",
  75. "email" : "selena@clivern.com",
  76. "job_title" : "Backend Developer"
  77. }
  78. > db.users.find({'_id': {'$ne': 2}}, {'created_at': 0, 'updated_at': 0}).pretty()
  79. {
  80. "_id" : 1,
  81. "first_name" : "Mark",
  82. "last_name" : "Doe",
  83. "username" : "admin",
  84. "email" : "admin@clivern.com",
  85. "job_title" : "CEO"
  86. }
  87. {
  88. "_id" : 3,
  89. "first_name" : "Helen",
  90. "last_name" : "Otal",
  91. "username" : "helen",
  92. "email" : "helen@clivern.com",
  93. "job_title" : "Web Designer"
  94. }
  95. {
  96. "_id" : 4,
  97. "first_name" : "Joe",
  98. "last_name" : "Mar",
  99. "username" : "joe",
  100. "email" : "joe@clivern.com",
  101. "job_title" : "Support Agent"
  102. }
  103. > db.users.find({'created_at': {'$gt': new Date()}}, {'created_at': 0, 'updated_at': 0}).pretty()
  104. > db.users.find({'created_at': {'$lt': new Date()}}, {'created_at': 0, 'updated_at': 0}).pretty()
  105. {
  106. "_id" : 1,
  107. "first_name" : "Mark",
  108. "last_name" : "Doe",
  109. "username" : "admin",
  110. "email" : "admin@clivern.com",
  111. "job_title" : "CEO"
  112. }
  113. {
  114. "_id" : 2,
  115. "first_name" : "Selena",
  116. "last_name" : "Gomez",
  117. "username" : "selena",
  118. "email" : "selena@clivern.com",
  119. "job_title" : "Backend Developer"
  120. }
  121. {
  122. "_id" : 3,
  123. "first_name" : "Helen",
  124. "last_name" : "Otal",
  125. "username" : "helen",
  126. "email" : "helen@clivern.com",
  127. "job_title" : "Web Designer"
  128. }
  129. {
  130. "_id" : 4,
  131. "first_name" : "Joe",
  132. "last_name" : "Mar",
  133. "username" : "joe",
  134. "email" : "joe@clivern.com",
  135. "job_title" : "Support Agent"
  136. }
  137. > db.users.find({'_id': {'$in': [1, 2]}}, {'created_at': 0, 'updated_at': 0}).pretty()
  138. {
  139. "_id" : 1,
  140. "first_name" : "Mark",
  141. "last_name" : "Doe",
  142. "username" : "admin",
  143. "email" : "admin@clivern.com",
  144. "job_title" : "CEO"
  145. }
  146. {
  147. "_id" : 2,
  148. "first_name" : "Selena",
  149. "last_name" : "Gomez",
  150. "username" : "selena",
  151. "email" : "selena@clivern.com",
  152. "job_title" : "Backend Developer"
  153. }
  154. > db.users.find({'_id': {'$nin': [1, 2]}}, {'created_at': 0, 'updated_at': 0}).pretty()
  155. {
  156. "_id" : 3,
  157. "first_name" : "Helen",
  158. "last_name" : "Otal",
  159. "username" : "helen",
  160. "email" : "helen@clivern.com",
  161. "job_title" : "Web Designer"
  162. }
  163. {
  164. "_id" : 4,
  165. "first_name" : "Joe",
  166. "last_name" : "Mar",
  167. "username" : "joe",
  168. "email" : "joe@clivern.com",
  169. "job_title" : "Support Agent"
  170. }
  171. > db.users.find({'$or': [{'first_name': 'Helen' }, {'first_name': 'Selena'}]}, {'created_at': 0, 'updated_at': 0}).pretty()
  172. {
  173. "_id" : 2,
  174. "first_name" : "Selena",
  175. "last_name" : "Gomez",
  176. "username" : "selena",
  177. "email" : "selena@clivern.com",
  178. "job_title" : "Backend Developer"
  179. }
  180. {
  181. "_id" : 3,
  182. "first_name" : "Helen",
  183. "last_name" : "Otal",
  184. "username" : "helen",
  185. "email" : "helen@clivern.com",
  186. "job_title" : "Web Designer"
  187. }
  188. > db.users.find({'$and': [{'first_name': 'Helen' }, {'first_name': 'Selena'}]}, {'created_at': 0, 'updated_at': 0}).pretty()
  189. > db.users.find({'$and': [{'first_name': 'Helen' }, {'last_name': 'Otal'}]}, {'created_at': 0, 'updated_at': 0}).pretty()
  190. {
  191. "_id" : 3,
  192. "first_name" : "Helen",
  193. "last_name" : "Otal",
  194. "username" : "helen",
  195. "email" : "helen@clivern.com",
  196. "job_title" : "Web Designer"
  197. }
  198. > db.users.find({'votes': null}, {'created_at': 0, 'updated_at': 0}).pretty()
  199. {
  200. "_id" : 1,
  201. "first_name" : "Mark",
  202. "last_name" : "Doe",
  203. "username" : "admin",
  204. "email" : "admin@clivern.com",
  205. "job_title" : "CEO"
  206. }
  207. {
  208. "_id" : 2,
  209. "first_name" : "Selena",
  210. "last_name" : "Gomez",
  211. "username" : "selena",
  212. "email" : "selena@clivern.com",
  213. "job_title" : "Backend Developer"
  214. }
  215. {
  216. "_id" : 3,
  217. "first_name" : "Helen",
  218. "last_name" : "Otal",
  219. "username" : "helen",
  220. "email" : "helen@clivern.com",
  221. "job_title" : "Web Designer"
  222. }
  223. {
  224. "_id" : 4,
  225. "first_name" : "Joe",
  226. "last_name" : "Mar",
  227. "username" : "joe",
  228. "email" : "joe@clivern.com",
  229. "job_title" : "Support Agent"
  230. }
  231. > db.users.find({'votes': {'$exists': true}}, {'created_at': 0, 'updated_at': 0}).pretty()
  232. > db.users.find({'votes': {'$exists': false}}, {'created_at': 0, 'updated_at': 0}).pretty()
  233. {
  234. "_id" : 1,
  235. "first_name" : "Mark",
  236. "last_name" : "Doe",
  237. "username" : "admin",
  238. "email" : "admin@clivern.com",
  239. "job_title" : "CEO"
  240. }
  241. {
  242. "_id" : 2,
  243. "first_name" : "Selena",
  244. "last_name" : "Gomez",
  245. "username" : "selena",
  246. "email" : "selena@clivern.com",
  247. "job_title" : "Backend Developer"
  248. }
  249. {
  250. "_id" : 3,
  251. "first_name" : "Helen",
  252. "last_name" : "Otal",
  253. "username" : "helen",
  254. "email" : "helen@clivern.com",
  255. "job_title" : "Web Designer"
  256. }
  257. {
  258. "_id" : 4,
  259. "first_name" : "Joe",
  260. "last_name" : "Mar",
  261. "username" : "joe",
  262. "email" : "joe@clivern.com",
  263. "job_title" : "Support Agent"
  264. }
  265. > db.users.find({'first_name': /mark/i}, {'created_at': 0, 'updated_at': 0}).pretty()
  266. {
  267. "_id" : 1,
  268. "first_name" : "Mark",
  269. "last_name" : "Doe",
  270. "username" : "admin",
  271. "email" : "admin@clivern.com",
  272. "job_title" : "CEO"
  273. }
  274. > db.users.find({'first_name': /mar/i}, {'created_at': 0, 'updated_at': 0}).pretty()
  275. {
  276. "_id" : 1,
  277. "first_name" : "Mark",
  278. "last_name" : "Doe",
  279. "username" : "admin",
  280. "email" : "admin@clivern.com",
  281. "job_title" : "CEO"
  282. }
  283. > db.users.find({'first_name': /^mar$/i}, {'created_at': 0, 'updated_at': 0}).pretty()
  284. > db.users.find({'first_name': /^mark$/i}, {'created_at': 0, 'updated_at': 0}).pretty()
  285. {
  286. "_id" : 1,
  287. "first_name" : "Mark",
  288. "last_name" : "Doe",
  289. "username" : "admin",
  290. "email" : "admin@clivern.com",
  291. "job_title" : "CEO"
  292. }
  293. > db.users.update({'_id': 1}, {'$set': {'tags': ['php', 'javascript', 'css']}})
  294. WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
  295. > db.users.update({'_id': 2}, {'$set': {'tags': ['jquery', 'javascript', 'python']}})
  296. WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
  297. > db.users.update({'_id': 3}, {'$set': {'tags': ['angular', 'javascript', 'haproxy']}})
  298. WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
  299. > db.users.update({'_id': 4}, {'$set': {'tags': ['angular3', 'vuejs', 'haproxy']}})
  300. WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
  301. > db.users.find({}).pretty()
  302. {
  303. "_id" : 1,
  304. "first_name" : "Mark",
  305. "last_name" : "Doe",
  306. "username" : "admin",
  307. "email" : "admin@clivern.com",
  308. "job_title" : "CEO",
  309. "created_at" : ISODate("2016-04-24T17:52:24.040Z"),
  310. "updated_at" : ISODate("2016-04-24T17:52:24.040Z"),
  311. "tags" : [
  312. "php",
  313. "javascript",
  314. "css"
  315. ]
  316. }
  317. {
  318. "_id" : 2,
  319. "first_name" : "Selena",
  320. "last_name" : "Gomez",
  321. "username" : "selena",
  322. "email" : "selena@clivern.com",
  323. "job_title" : "Backend Developer",
  324. "created_at" : ISODate("2016-04-24T17:52:24.040Z"),
  325. "updated_at" : ISODate("2016-04-24T17:52:24.040Z"),
  326. "tags" : [
  327. "jquery",
  328. "javascript",
  329. "python"
  330. ]
  331. }
  332. {
  333. "_id" : 3,
  334. "first_name" : "Helen",
  335. "last_name" : "Otal",
  336. "username" : "helen",
  337. "email" : "helen@clivern.com",
  338. "job_title" : "Web Designer",
  339. "created_at" : ISODate("2016-04-24T17:52:24.040Z"),
  340. "updated_at" : ISODate("2016-04-24T17:52:24.040Z"),
  341. "tags" : [
  342. "angular",
  343. "javascript",
  344. "haproxy"
  345. ]
  346. }
  347. {
  348. "_id" : 4,
  349. "first_name" : "Joe",
  350. "last_name" : "Mar",
  351. "username" : "joe",
  352. "email" : "joe@clivern.com",
  353. "job_title" : "Support Agent",
  354. "created_at" : ISODate("2016-04-24T17:52:24.040Z"),
  355. "updated_at" : ISODate("2016-04-24T17:52:24.040Z"),
  356. "tags" : [
  357. "angular3",
  358. "vuejs",
  359. "haproxy"
  360. ]
  361. }
  362. > db.users.find({'tags': ['angular3', 'vuejs', 'haproxy']}).pretty()
  363. {
  364. "_id" : 4,
  365. "first_name" : "Joe",
  366. "last_name" : "Mar",
  367. "username" : "joe",
  368. "email" : "joe@clivern.com",
  369. "job_title" : "Support Agent",
  370. "created_at" : ISODate("2016-04-24T17:52:24.040Z"),
  371. "updated_at" : ISODate("2016-04-24T17:52:24.040Z"),
  372. "tags" : [
  373. "angular3",
  374. "vuejs",
  375. "haproxy"
  376. ]
  377. }
  378. > db.users.find({'tags': ['angular3', 'haproxy', 'vuejs']}).pretty()
  379. > db.users.find({'tags': {'$all': ['angular3', 'haproxy', 'vuejs']}}).pretty()
  380. {
  381. "_id" : 4,
  382. "first_name" : "Joe",
  383. "last_name" : "Mar",
  384. "username" : "joe",
  385. "email" : "joe@clivern.com",
  386. "job_title" : "Support Agent",
  387. "created_at" : ISODate("2016-04-24T17:52:24.040Z"),
  388. "updated_at" : ISODate("2016-04-24T17:52:24.040Z"),
  389. "tags" : [
  390. "angular3",
  391. "vuejs",
  392. "haproxy"
  393. ]
  394. }
  395. > db.users.find({'tags': {'$size': 3}}).pretty()
  396. {
  397. "_id" : 1,
  398. "first_name" : "Mark",
  399. "last_name" : "Doe",
  400. "username" : "admin",
  401. "email" : "admin@clivern.com",
  402. "job_title" : "CEO",
  403. "created_at" : ISODate("2016-04-24T17:52:24.040Z"),
  404. "updated_at" : ISODate("2016-04-24T17:52:24.040Z"),
  405. "tags" : [
  406. "php",
  407. "javascript",
  408. "css"
  409. ]
  410. }
  411. {
  412. "_id" : 2,
  413. "first_name" : "Selena",
  414. "last_name" : "Gomez",
  415. "username" : "selena",
  416. "email" : "selena@clivern.com",
  417. "job_title" : "Backend Developer",
  418. "created_at" : ISODate("2016-04-24T17:52:24.040Z"),
  419. "updated_at" : ISODate("2016-04-24T17:52:24.040Z"),
  420. "tags" : [
  421. "jquery",
  422. "javascript",
  423. "python"
  424. ]
  425. }
  426. {
  427. "_id" : 3,
  428. "first_name" : "Helen",
  429. "last_name" : "Otal",
  430. "username" : "helen",
  431. "email" : "helen@clivern.com",
  432. "job_title" : "Web Designer",
  433. "created_at" : ISODate("2016-04-24T17:52:24.040Z"),
  434. "updated_at" : ISODate("2016-04-24T17:52:24.040Z"),
  435. "tags" : [
  436. "angular",
  437. "javascript",
  438. "haproxy"
  439. ]
  440. }
  441. {
  442. "_id" : 4,
  443. "first_name" : "Joe",
  444. "last_name" : "Mar",
  445. "username" : "joe",
  446. "email" : "joe@clivern.com",
  447. "job_title" : "Support Agent",
  448. "created_at" : ISODate("2016-04-24T17:52:24.040Z"),
  449. "updated_at" : ISODate("2016-04-24T17:52:24.040Z"),
  450. "tags" : [
  451. "angular3",
  452. "vuejs",
  453. "haproxy"
  454. ]
  455. }
  456. > db.users.find({'tags': {'$size': 1}}).pretty()
  457. > db.users.find({'tags': {'$size': 2}}).pretty()
  458. > db.users.find({'tags.1': 'vuejs'}).pretty()
  459. {
  460. "_id" : 4,
  461. "first_name" : "Joe",
  462. "last_name" : "Mar",
  463. "username" : "joe",
  464. "email" : "joe@clivern.com",
  465. "job_title" : "Support Agent",
  466. "created_at" : ISODate("2016-04-24T17:52:24.040Z"),
  467. "updated_at" : ISODate("2016-04-24T17:52:24.040Z"),
  468. "tags" : [
  469. "angular3",
  470. "vuejs",
  471. "haproxy"
  472. ]
  473. }
  474. > db.users.find({}).pretty()
  475. {
  476. "_id" : 1,
  477. "first_name" : "Mark",
  478. "last_name" : "Doe",
  479. "username" : "admin",
  480. "email" : "admin@clivern.com",
  481. "job_title" : "CEO",
  482. "created_at" : ISODate("2016-04-24T17:52:24.040Z"),
  483. "updated_at" : ISODate("2016-04-24T17:52:24.040Z"),
  484. "tags" : [
  485. "php",
  486. "javascript",
  487. "css"
  488. ]
  489. }
  490. {
  491. "_id" : 2,
  492. "first_name" : "Selena",
  493. "last_name" : "Gomez",
  494. "username" : "selena",
  495. "email" : "selena@clivern.com",
  496. "job_title" : "Backend Developer",
  497. "created_at" : ISODate("2016-04-24T17:52:24.040Z"),
  498. "updated_at" : ISODate("2016-04-24T17:52:24.040Z"),
  499. "tags" : [
  500. "jquery",
  501. "javascript",
  502. "python"
  503. ]
  504. }
  505. {
  506. "_id" : 3,
  507. "first_name" : "Helen",
  508. "last_name" : "Otal",
  509. "username" : "helen",
  510. "email" : "helen@clivern.com",
  511. "job_title" : "Web Designer",
  512. "created_at" : ISODate("2016-04-24T17:52:24.040Z"),
  513. "updated_at" : ISODate("2016-04-24T17:52:24.040Z"),
  514. "tags" : [
  515. "angular",
  516. "javascript",
  517. "haproxy"
  518. ]
  519. }
  520. {
  521. "_id" : 4,
  522. "first_name" : "Joe",
  523. "last_name" : "Mar",
  524. "username" : "joe",
  525. "email" : "joe@clivern.com",
  526. "job_title" : "Support Agent",
  527. "created_at" : ISODate("2016-04-24T17:52:24.040Z"),
  528. "updated_at" : ISODate("2016-04-24T17:52:24.040Z"),
  529. "tags" : [
  530. "angular3",
  531. "vuejs",
  532. "haproxy"
  533. ]
  534. }
  535. > db.users.find({}).skip(2).pretty()
  536. {
  537. "_id" : 3,
  538. "first_name" : "Helen",
  539. "last_name" : "Otal",
  540. "username" : "helen",
  541. "email" : "helen@clivern.com",
  542. "job_title" : "Web Designer",
  543. "created_at" : ISODate("2016-04-24T17:52:24.040Z"),
  544. "updated_at" : ISODate("2016-04-24T17:52:24.040Z"),
  545. "tags" : [
  546. "angular",
  547. "javascript",
  548. "haproxy"
  549. ]
  550. }
  551. {
  552. "_id" : 4,
  553. "first_name" : "Joe",
  554. "last_name" : "Mar",
  555. "username" : "joe",
  556. "email" : "joe@clivern.com",
  557. "job_title" : "Support Agent",
  558. "created_at" : ISODate("2016-04-24T17:52:24.040Z"),
  559. "updated_at" : ISODate("2016-04-24T17:52:24.040Z"),
  560. "tags" : [
  561. "angular3",
  562. "vuejs",
  563. "haproxy"
  564. ]
  565. }
  566. > db.users.find({}).skip(2).limit(1).pretty()
  567. {
  568. "_id" : 3,
  569. "first_name" : "Helen",
  570. "last_name" : "Otal",
  571. "username" : "helen",
  572. "email" : "helen@clivern.com",
  573. "job_title" : "Web Designer",
  574. "created_at" : ISODate("2016-04-24T17:52:24.040Z"),
  575. "updated_at" : ISODate("2016-04-24T17:52:24.040Z"),
  576. "tags" : [
  577. "angular",
  578. "javascript",
  579. "haproxy"
  580. ]
  581. }
  582. > db.users.find({}).sort({'_id': 1}).pretty()
  583. {
  584. "_id" : 1,
  585. "first_name" : "Mark",
  586. "last_name" : "Doe",
  587. "username" : "admin",
  588. "email" : "admin@clivern.com",
  589. "job_title" : "CEO",
  590. "created_at" : ISODate("2016-04-24T17:52:24.040Z"),
  591. "updated_at" : ISODate("2016-04-24T17:52:24.040Z"),
  592. "tags" : [
  593. "php",
  594. "javascript",
  595. "css"
  596. ]
  597. }
  598. {
  599. "_id" : 2,
  600. "first_name" : "Selena",
  601. "last_name" : "Gomez",
  602. "username" : "selena",
  603. "email" : "selena@clivern.com",
  604. "job_title" : "Backend Developer",
  605. "created_at" : ISODate("2016-04-24T17:52:24.040Z"),
  606. "updated_at" : ISODate("2016-04-24T17:52:24.040Z"),
  607. "tags" : [
  608. "jquery",
  609. "javascript",
  610. "python"
  611. ]
  612. }
  613. {
  614. "_id" : 3,
  615. "first_name" : "Helen",
  616. "last_name" : "Otal",
  617. "username" : "helen",
  618. "email" : "helen@clivern.com",
  619. "job_title" : "Web Designer",
  620. "created_at" : ISODate("2016-04-24T17:52:24.040Z"),
  621. "updated_at" : ISODate("2016-04-24T17:52:24.040Z"),
  622. "tags" : [
  623. "angular",
  624. "javascript",
  625. "haproxy"
  626. ]
  627. }
  628. {
  629. "_id" : 4,
  630. "first_name" : "Joe",
  631. "last_name" : "Mar",
  632. "username" : "joe",
  633. "email" : "joe@clivern.com",
  634. "job_title" : "Support Agent",
  635. "created_at" : ISODate("2016-04-24T17:52:24.040Z"),
  636. "updated_at" : ISODate("2016-04-24T17:52:24.040Z"),
  637. "tags" : [
  638. "angular3",
  639. "vuejs",
  640. "haproxy"
  641. ]
  642. }
  643. > db.users.find({}).sort({'_id': -1}).pretty()
  644. {
  645. "_id" : 4,
  646. "first_name" : "Joe",
  647. "last_name" : "Mar",
  648. "username" : "joe",
  649. "email" : "joe@clivern.com",
  650. "job_title" : "Support Agent",
  651. "created_at" : ISODate("2016-04-24T17:52:24.040Z"),
  652. "updated_at" : ISODate("2016-04-24T17:52:24.040Z"),
  653. "tags" : [
  654. "angular3",
  655. "vuejs",
  656. "haproxy"
  657. ]
  658. }
  659. {
  660. "_id" : 3,
  661. "first_name" : "Helen",
  662. "last_name" : "Otal",
  663. "username" : "helen",
  664. "email" : "helen@clivern.com",
  665. "job_title" : "Web Designer",
  666. "created_at" : ISODate("2016-04-24T17:52:24.040Z"),
  667. "updated_at" : ISODate("2016-04-24T17:52:24.040Z"),
  668. "tags" : [
  669. "angular",
  670. "javascript",
  671. "haproxy"
  672. ]
  673. }
  674. {
  675. "_id" : 2,
  676. "first_name" : "Selena",
  677. "last_name" : "Gomez",
  678. "username" : "selena",
  679. "email" : "selena@clivern.com",
  680. "job_title" : "Backend Developer",
  681. "created_at" : ISODate("2016-04-24T17:52:24.040Z"),
  682. "updated_at" : ISODate("2016-04-24T17:52:24.040Z"),
  683. "tags" : [
  684. "jquery",
  685. "javascript",
  686. "python"
  687. ]
  688. }
  689. {
  690. "_id" : 1,
  691. "first_name" : "Mark",
  692. "last_name" : "Doe",
  693. "username" : "admin",
  694. "email" : "admin@clivern.com",
  695. "job_title" : "CEO",
  696. "created_at" : ISODate("2016-04-24T17:52:24.040Z"),
  697. "updated_at" : ISODate("2016-04-24T17:52:24.040Z"),
  698. "tags" : [
  699. "php",
  700. "javascript",
  701. "css"
  702. ]
  703. }

Indexing

  1. > db.restaurants.find({'address.street': '3 Avenue'}).explain('executionStats')
  2. {
  3. "queryPlanner" : {
  4. "plannerVersion" : 1,
  5. "namespace" : "test.restaurants",
  6. "indexFilterSet" : false,
  7. "parsedQuery" : {
  8. "address.street" : {
  9. "$eq" : "3 Avenue"
  10. }
  11. },
  12. "winningPlan" : {
  13. "stage" : "COLLSCAN",
  14. "filter" : {
  15. "address.street" : {
  16. "$eq" : "3 Avenue"
  17. }
  18. },
  19. "direction" : "forward"
  20. },
  21. "rejectedPlans" : [ ]
  22. },
  23. "executionStats" : {
  24. "executionSuccess" : true,
  25. "nReturned" : 553,
  26. "executionTimeMillis" : 20,
  27. "totalKeysExamined" : 0,
  28. "totalDocsExamined" : 25359,
  29. "executionStages" : {
  30. "stage" : "COLLSCAN",
  31. "filter" : {
  32. "address.street" : {
  33. "$eq" : "3 Avenue"
  34. }
  35. },
  36. "nReturned" : 553,
  37. "executionTimeMillisEstimate" : 20,
  38. "works" : 25361,
  39. "advanced" : 553,
  40. "needTime" : 24807,
  41. "needYield" : 0,
  42. "saveState" : 198,
  43. "restoreState" : 198,
  44. "isEOF" : 1,
  45. "invalidates" : 0,
  46. "direction" : "forward",
  47. "docsExamined" : 25359
  48. }
  49. },
  50. "serverInfo" : {
  51. "host" : "2c9af55923a4",
  52. "port" : 27017,
  53. "version" : "3.2.4",
  54. "gitVersion" : "e2ee9ffcf9f5a94fad76802e28cc978718bb7a30"
  55. },
  56. "ok" : 1
  57. }
  58. > db.restaurants.find({'address.street': '3 Avenue'}).explain('executionStats').executionStats.totalDocsExamined
  59. 25359
  60. > db.restaurants.ensureIndex({'address.street': 1})
  61. {
  62. "createdCollectionAutomatically" : false,
  63. "numIndexesBefore" : 1,
  64. "numIndexesAfter" : 2,
  65. "ok" : 1
  66. }
  67. > db.restaurants.find({'address.street': '3 Avenue'}).explain('executionStats').executionStats.totalDocsExamined
  68. 553
  69. > db.restaurants.dropIndex({'address.street': 1})
  70. { "nIndexesWas" : 2, "ok" : 1 }
  71. > db.restaurants.find({'address.street': '3 Avenue'}).explain('executionStats').executionStats.totalDocsExamined
  72. 25359

Aggregation Framework

$project Modifier

  1. > db.users.find({}).pretty()
  2. {
  3. "_id" : 1,
  4. "first_name" : "Mark",
  5. "last_name" : "Doe",
  6. "username" : "admin",
  7. "email" : "admin@clivern.com",
  8. "job_title" : "CEO",
  9. "created_at" : ISODate("2016-04-24T17:52:24.040Z"),
  10. "updated_at" : ISODate("2016-04-24T17:52:24.040Z"),
  11. "tags" : [
  12. "php",
  13. "javascript",
  14. "css"
  15. ]
  16. }
  17. {
  18. "_id" : 2,
  19. "first_name" : "Selena",
  20. "last_name" : "Gomez",
  21. "username" : "selena",
  22. "email" : "selena@clivern.com",
  23. "job_title" : "Backend Developer",
  24. "created_at" : ISODate("2016-04-24T17:52:24.040Z"),
  25. "updated_at" : ISODate("2016-04-24T17:52:24.040Z"),
  26. "tags" : [
  27. "jquery",
  28. "javascript",
  29. "python"
  30. ]
  31. }
  32. {
  33. "_id" : 3,
  34. "first_name" : "Helen",
  35. "last_name" : "Otal",
  36. "username" : "helen",
  37. "email" : "helen@clivern.com",
  38. "job_title" : "Web Designer",
  39. "created_at" : ISODate("2016-04-24T17:52:24.040Z"),
  40. "updated_at" : ISODate("2016-04-24T17:52:24.040Z"),
  41. "tags" : [
  42. "angular",
  43. "javascript",
  44. "haproxy"
  45. ]
  46. }
  47. {
  48. "_id" : 4,
  49. "first_name" : "Joe",
  50. "last_name" : "Mar",
  51. "username" : "joe",
  52. "email" : "joe@clivern.com",
  53. "job_title" : "Support Agent",
  54. "created_at" : ISODate("2016-04-24T17:52:24.040Z"),
  55. "updated_at" : ISODate("2016-04-24T17:52:24.040Z"),
  56. "tags" : [
  57. "angular3",
  58. "vuejs",
  59. "haproxy"
  60. ]
  61. }
  62. > db.users.aggregate({'$project': {'username': 1}})
  63. { "_id" : 1, "username" : "admin" }
  64. { "_id" : 2, "username" : "selena" }
  65. { "_id" : 3, "username" : "helen" }
  66. { "_id" : 4, "username" : "joe" }
  67. > db.users.aggregate({'$project': {'username': 1, '_id': 0}})
  68. { "username" : "admin" }
  69. { "username" : "selena" }
  70. { "username" : "helen" }
  71. { "username" : "joe" }
  72. > db.users.aggregate({'$project': {'username': '$_id', '_id': 0}})
  73. { "username" : 1 }
  74. { "username" : 2 }
  75. { "username" : 3 }
  76. { "username" : 4 }
  77. > db.users.aggregate({'$project': {'id': '$username', '_id': 0}})
  78. { "id" : "admin" }
  79. { "id" : "selena" }
  80. { "id" : "helen" }
  81. { "id" : "joe" }
  82. > db.users.aggregate({'$project': {'field_name': '$username', '_id': 0}})
  83. { "field_name" : "admin" }
  84. { "field_name" : "selena" }
  85. { "field_name" : "helen" }
  86. { "field_name" : "joe" }

$match Modifier

  1. > db.users.aggregate({'$project': {'id': '$username', '_id': 0}}, {'$match': {'id': 'admin'}})
  2. { "id" : "admin" }
  3. > db.users.aggregate({'$project': {'id': '$_id', '_id': 0}}, {'$match': {'id': {'$gt': 1}}})
  4. { "id" : 2 }
  5. { "id" : 3 }
  6. { "id" : 4 }
  7. > db.users.aggregate({'$project': {'id': '$_id', '_id': 0}}, {'$match': {'id': {'$gte': 1}}})
  8. { "id" : 1 }
  9. { "id" : 2 }
  10. { "id" : 3 }
  11. { "id" : 4 }
  12. > db.users.aggregate({'$project': {'id': '$_id', '_id': 0}}, {'$match': {'id': {'$lte': 1}}})
  13. { "id" : 1 }
  14. > db.users.aggregate({'$project': {'id': '$_id', '_id': 0}}, {'$match': {'id': {'$lt': 2}}})
  15. { "id" : 1 }
  16. > db.users.aggregate({'$project': {'id': '$_id', '_id': 0}}, {'$match': {'id': {'$in': [1, 2, 4]}}})
  17. { "id" : 1 }
  18. { "id" : 2 }
  19. { "id" : 4 }

$add, $subtract, $multiply, $divide and $mod Modifier

  1. > db.results.find({}).pretty()
  2. {
  3. "_id" : 1,
  4. "name" : "mark",
  5. "math_result" : 60,
  6. "physics_result" : 23,
  7. "cs_result" : 78
  8. }
  9. {
  10. "_id" : 2,
  11. "name" : "helen",
  12. "math_result" : 50,
  13. "physics_result" : 48,
  14. "cs_result" : 74
  15. }
  16. {
  17. "_id" : 3,
  18. "name" : "timber",
  19. "math_result" : 65,
  20. "physics_result" : 14,
  21. "cs_result" : 56
  22. }
  23. {
  24. "_id" : 4,
  25. "name" : "demi",
  26. "math_result" : 87,
  27. "physics_result" : 78,
  28. "cs_result" : 14
  29. }
  30. {
  31. "_id" : 5,
  32. "name" : "jenny",
  33. "math_result" : 76,
  34. "physics_result" : 45,
  35. "cs_result" : 78
  36. }
  37. > db.results.aggregate({'$project': {'total': {'$add': ['$math_result', '$cs_result']}}})
  38. { "_id" : 1, "total" : 138 }
  39. { "_id" : 2, "total" : 124 }
  40. { "_id" : 3, "total" : 121 }
  41. { "_id" : 4, "total" : 101 }
  42. { "_id" : 5, "total" : 154 }
  43. > db.results.aggregate({'$project': {'total': {'$add': ['$math_result', '$physics_result' , '$cs_result']}}})
  44. { "_id" : 1, "total" : 161 }
  45. { "_id" : 2, "total" : 172 }
  46. { "_id" : 3, "total" : 135 }
  47. { "_id" : 4, "total" : 179 }
  48. { "_id" : 5, "total" : 199 }
  49. > db.results.aggregate({'$project': {'total': {'$subtract': [{'$add': ['$math_result', '$physics_result' , '$cs_result']}, 100]}}})
  50. { "_id" : 1, "total" : 61 }
  51. { "_id" : 2, "total" : 72 }
  52. { "_id" : 3, "total" : 35 }
  53. { "_id" : 4, "total" : 79 }
  54. { "_id" : 5, "total" : 99 }
  55. > db.results.aggregate({'$project': {'total': {'$multiply': [{'$add': ['$math_result', '$physics_result' , '$cs_result']}, 100]}}})
  56. { "_id" : 1, "total" : 16100 }
  57. { "_id" : 2, "total" : 17200 }
  58. { "_id" : 3, "total" : 13500 }
  59. { "_id" : 4, "total" : 17900 }
  60. { "_id" : 5, "total" : 19900 }
  61. > db.results.aggregate({'$project': {'total': {'$divide': [{'$add': ['$math_result', '$physics_result' , '$cs_result']}, 100]}}})
  62. { "_id" : 1, "total" : 1.61 }
  63. { "_id" : 2, "total" : 1.72 }
  64. { "_id" : 3, "total" : 1.35 }
  65. { "_id" : 4, "total" : 1.79 }
  66. { "_id" : 5, "total" : 1.99 }
  67. > db.results.aggregate({'$project': {'total': {'$mod': [{'$add': ['$math_result', '$physics_result' , '$cs_result']}, 100]}}})
  68. { "_id" : 1, "total" : 61 }
  69. { "_id" : 2, "total" : 72 }
  70. { "_id" : 3, "total" : 35 }
  71. { "_id" : 4, "total" : 79 }
  72. { "_id" : 5, "total" : 99 }

$substr, $concat, $toLower and $toUpper Modifier

  1. > db.tuts.find({}).pretty()
  2. {
  3. "_id" : ObjectId("571e06a776dcd773abde06c7"),
  4. "title" : "Hello World",
  5. "author" : "Mark",
  6. "created_at" : ISODate("2016-04-25T11:59:35.742Z")
  7. }
  8. {
  9. "_id" : ObjectId("571e06a776dcd773abde06c8"),
  10. "title" : "Post Title",
  11. "author" : "Helen",
  12. "created_at" : ISODate("2016-04-25T11:59:35.742Z")
  13. }
  14. {
  15. "_id" : ObjectId("571e06a776dcd773abde06c9"),
  16. "title" : "WordPress Article",
  17. "author" : "Jenny",
  18. "created_at" : ISODate("2016-04-25T11:59:35.742Z")
  19. }
  20. > db.tuts.aggregate({'$project': {'title': {'$substr': ['$title', 0, 1]}}})
  21. { "_id" : ObjectId("571e06a776dcd773abde06c7"), "title" : "H" }
  22. { "_id" : ObjectId("571e06a776dcd773abde06c8"), "title" : "P" }
  23. { "_id" : ObjectId("571e06a776dcd773abde06c9"), "title" : "W" }
  24. > db.tuts.aggregate({'$project': {'title': {'$concat': ['title: ', '$title']}}})
  25. { "_id" : ObjectId("571e06a776dcd773abde06c7"), "title" : "title: Hello World" }
  26. { "_id" : ObjectId("571e06a776dcd773abde06c8"), "title" : "title: Post Title" }
  27. { "_id" : ObjectId("571e06a776dcd773abde06c9"), "title" : "title: WordPress Article" }
  28. > db.tuts.aggregate({'$project': {'title': {'$toLower': '$title'}}})
  29. { "_id" : ObjectId("571e06a776dcd773abde06c7"), "title" : "hello world" }
  30. { "_id" : ObjectId("571e06a776dcd773abde06c8"), "title" : "post title" }
  31. { "_id" : ObjectId("571e06a776dcd773abde06c9"), "title" : "wordpress article" }
  32. > db.tuts.aggregate({'$project': {'title': {'$toUpper': '$title'}}})
  33. { "_id" : ObjectId("571e06a776dcd773abde06c7"), "title" : "HELLO WORLD" }
  34. { "_id" : ObjectId("571e06a776dcd773abde06c8"), "title" : "POST TITLE" }
  35. { "_id" : ObjectId("571e06a776dcd773abde06c9"), "title" : "WORDPRESS ARTICLE" }

$year, $month, $week, $hour, $minute, $second, $dayOfMonth, $dayOfWeek and $dayOfYear Modifier

  1. > db.tuts.aggregate({'$project': {'created_in_year': {'$year': '$created_at'}}})
  2. { "_id" : ObjectId("571e06a776dcd773abde06c7"), "created_in_year" : 2016 }
  3. { "_id" : ObjectId("571e06a776dcd773abde06c8"), "created_in_year" : 2016 }
  4. { "_id" : ObjectId("571e06a776dcd773abde06c9"), "created_in_year" : 2016 }
  5. > db.tuts.aggregate({'$project': {'created_in_month': {'$month': '$created_at'}}})
  6. { "_id" : ObjectId("571e06a776dcd773abde06c7"), "created_in_month" : 4 }
  7. { "_id" : ObjectId("571e06a776dcd773abde06c8"), "created_in_month" : 4 }
  8. { "_id" : ObjectId("571e06a776dcd773abde06c9"), "created_in_month" : 4 }
  9. > db.tuts.aggregate({'$project': {'created_in_week': {'$week': '$created_at'}}})
  10. { "_id" : ObjectId("571e06a776dcd773abde06c7"), "created_in_week" : 17 }
  11. { "_id" : ObjectId("571e06a776dcd773abde06c8"), "created_in_week" : 17 }
  12. { "_id" : ObjectId("571e06a776dcd773abde06c9"), "created_in_week" : 17 }
  13. > db.tuts.aggregate({'$project': {'created_in_hour': {'$hour': '$created_at'}}})
  14. { "_id" : ObjectId("571e06a776dcd773abde06c7"), "created_in_hour" : 11 }
  15. { "_id" : ObjectId("571e06a776dcd773abde06c8"), "created_in_hour" : 11 }
  16. { "_id" : ObjectId("571e06a776dcd773abde06c9"), "created_in_hour" : 11 }
  17. > db.tuts.aggregate({'$project': {'created_in_minute': {'$minute': '$created_at'}}})
  18. { "_id" : ObjectId("571e06a776dcd773abde06c7"), "created_in_minute" : 59 }
  19. { "_id" : ObjectId("571e06a776dcd773abde06c8"), "created_in_minute" : 59 }
  20. { "_id" : ObjectId("571e06a776dcd773abde06c9"), "created_in_minute" : 59 }
  21. > db.tuts.aggregate({'$project': {'created_in_second': {'$second': '$created_at'}}})
  22. { "_id" : ObjectId("571e06a776dcd773abde06c7"), "created_in_second" : 35 }
  23. { "_id" : ObjectId("571e06a776dcd773abde06c8"), "created_in_second" : 35 }
  24. { "_id" : ObjectId("571e06a776dcd773abde06c9"), "created_in_second" : 35 }
  25. > db.tuts.aggregate({'$project': {'created_in_day_of_month': {'$dayOfMonth': '$created_at'}}})
  26. { "_id" : ObjectId("571e06a776dcd773abde06c7"), "created_in_day_of_month" : 25 }
  27. { "_id" : ObjectId("571e06a776dcd773abde06c8"), "created_in_day_of_month" : 25 }
  28. { "_id" : ObjectId("571e06a776dcd773abde06c9"), "created_in_day_of_month" : 25 }
  29. > db.tuts.aggregate({'$project': {'created_in_day_of_week': {'$dayOfWeek': '$created_at'}}})
  30. { "_id" : ObjectId("571e06a776dcd773abde06c7"), "created_in_day_of_week" : 2 }
  31. { "_id" : ObjectId("571e06a776dcd773abde06c8"), "created_in_day_of_week" : 2 }
  32. { "_id" : ObjectId("571e06a776dcd773abde06c9"), "created_in_day_of_week" : 2 }
  33. > db.tuts.aggregate({'$project': {'created_in_day_of_year': {'$dayOfYear': '$created_at'}}})
  34. { "_id" : ObjectId("571e06a776dcd773abde06c7"), "created_in_day_of_year" : 116 }
  35. { "_id" : ObjectId("571e06a776dcd773abde06c8"), "created_in_day_of_year" : 116 }
  36. { "_id" : ObjectId("571e06a776dcd773abde06c9"), "created_in_day_of_year" : 116 }

$cmp, $strcasecmp, $eq, $ne, $gt, $gte, $lt, $lte, $and, $or, $not, $cond and $ifNull Modifier

  1. > db.tuts.aggregate({'$project': {'title': {'$cmp': ['Hello World', '$title']}}})
  2. { "_id" : ObjectId("571e06a776dcd773abde06c7"), "title" : 0 }
  3. { "_id" : ObjectId("571e06a776dcd773abde06c8"), "title" : -1 }
  4. { "_id" : ObjectId("571e06a776dcd773abde06c9"), "title" : -1 }
  5. > db.tuts.aggregate({'$project': {'title': {'$cmp': ['hello World', '$title']}}})
  6. { "_id" : ObjectId("571e06a776dcd773abde06c7"), "title" : 1 }
  7. { "_id" : ObjectId("571e06a776dcd773abde06c8"), "title" : 1 }
  8. { "_id" : ObjectId("571e06a776dcd773abde06c9"), "title" : 1 }
  9. > db.tuts.aggregate({'$project': {'title': {'$strcasecmp': ['hello World', '$title']}}})
  10. { "_id" : ObjectId("571e06a776dcd773abde06c7"), "title" : 0 }
  11. { "_id" : ObjectId("571e06a776dcd773abde06c8"), "title" : -1 }
  12. { "_id" : ObjectId("571e06a776dcd773abde06c9"), "title" : -1 }
  13. > db.tuts.aggregate({'$project': {'title': {'$eq': ['Hello World', '$title']}}})
  14. { "_id" : ObjectId("571e06a776dcd773abde06c7"), "title" : true }
  15. { "_id" : ObjectId("571e06a776dcd773abde06c8"), "title" : false }
  16. { "_id" : ObjectId("571e06a776dcd773abde06c9"), "title" : false }
  17. > db.tuts.aggregate({'$project': {'title': {'$ne': ['Hello World', '$title']}}})
  18. { "_id" : ObjectId("571e06a776dcd773abde06c7"), "title" : false }
  19. { "_id" : ObjectId("571e06a776dcd773abde06c8"), "title" : true }
  20. { "_id" : ObjectId("571e06a776dcd773abde06c9"), "title" : true }
  21. > db.tuts.aggregate({'$project': {'title': {'$gt': ['Hello World', '$title']}}})
  22. { "_id" : ObjectId("571e06a776dcd773abde06c7"), "title" : false }
  23. { "_id" : ObjectId("571e06a776dcd773abde06c8"), "title" : false }
  24. { "_id" : ObjectId("571e06a776dcd773abde06c9"), "title" : false }
  25. > db.tuts.aggregate({'$project': {'title': {'$gte': ['Hello World', '$title']}}})
  26. { "_id" : ObjectId("571e06a776dcd773abde06c7"), "title" : true }
  27. { "_id" : ObjectId("571e06a776dcd773abde06c8"), "title" : false }
  28. { "_id" : ObjectId("571e06a776dcd773abde06c9"), "title" : false }
  29. > db.tuts.aggregate({'$project': {'title': {'$lt': ['Hello World', '$title']}}})
  30. { "_id" : ObjectId("571e06a776dcd773abde06c7"), "title" : false }
  31. { "_id" : ObjectId("571e06a776dcd773abde06c8"), "title" : true }
  32. { "_id" : ObjectId("571e06a776dcd773abde06c9"), "title" : true }
  33. > db.tuts.aggregate({'$project': {'title': {'$lte': ['Hello World', '$title']}}})
  34. { "_id" : ObjectId("571e06a776dcd773abde06c7"), "title" : true }
  35. { "_id" : ObjectId("571e06a776dcd773abde06c8"), "title" : true }
  36. { "_id" : ObjectId("571e06a776dcd773abde06c9"), "title" : true }
  37. > db.tuts.aggregate({'$project': {'title': {'$and': [0, '$title']}}})
  38. { "_id" : ObjectId("571e06a776dcd773abde06c7"), "title" : false }
  39. { "_id" : ObjectId("571e06a776dcd773abde06c8"), "title" : false }
  40. { "_id" : ObjectId("571e06a776dcd773abde06c9"), "title" : false }
  41. > db.tuts.aggregate({'$project': {'title': {'$or': [0, '$title']}}})
  42. { "_id" : ObjectId("571e06a776dcd773abde06c7"), "title" : true }
  43. { "_id" : ObjectId("571e06a776dcd773abde06c8"), "title" : true }
  44. { "_id" : ObjectId("571e06a776dcd773abde06c9"), "title" : true }
  45. > db.tuts.aggregate({'$project': {'title': {'$not': '$title'}}})
  46. { "_id" : ObjectId("571e06a776dcd773abde06c7"), "title" : false }
  47. { "_id" : ObjectId("571e06a776dcd773abde06c8"), "title" : false }
  48. { "_id" : ObjectId("571e06a776dcd773abde06c9"), "title" : false }
  49. > db.tuts.aggregate({'$project': {'title': {'$cond': ['$title', 'not empty', 'empty']}}})
  50. { "_id" : ObjectId("571e06a776dcd773abde06c7"), "title" : "not empty" }
  51. { "_id" : ObjectId("571e06a776dcd773abde06c8"), "title" : "not empty" }
  52. { "_id" : ObjectId("571e06a776dcd773abde06c9"), "title" : "not empty" }
  53. > db.tuts.aggregate({'$project': {'title': {'$ifNull': ['$title', 'null value']}}})
  54. { "_id" : ObjectId("571e06a776dcd773abde06c7"), "title" : "Hello World" }
  55. { "_id" : ObjectId("571e06a776dcd773abde06c8"), "title" : "Post Title" }
  56. { "_id" : ObjectId("571e06a776dcd773abde06c9"), "title" : "WordPress Article" }

$group Modifier

  1. > db.tuts.find({}).pretty()
  2. {
  3. "_id" : ObjectId("571e06a776dcd773abde06c7"),
  4. "title" : "Hello World",
  5. "author" : "Mark",
  6. "created_at" : ISODate("2016-04-25T11:59:35.742Z"),
  7. "votes" : 0
  8. }
  9. {
  10. "_id" : ObjectId("571e06a776dcd773abde06c8"),
  11. "title" : "Post Title",
  12. "author" : "Helen",
  13. "created_at" : ISODate("2016-04-25T11:59:35.742Z"),
  14. "votes" : 0
  15. }
  16. {
  17. "_id" : ObjectId("571e06a776dcd773abde06c9"),
  18. "title" : "WordPress Article",
  19. "author" : "Jenny",
  20. "created_at" : ISODate("2016-04-25T11:59:35.742Z"),
  21. "votes" : 0
  22. }
  23. > db.tuts.aggregate({'$project': {'title': '$title', 'author': '$author'}}, {'$group': {'_id': {'title': '$title', 'author': '$author'} }})
  24. { "_id" : { "title" : "WordPress Article", "author" : "Jenny" } }
  25. { "_id" : { "title" : "Post Title", "author" : "Helen" } }
  26. { "_id" : { "title" : "Hello World", "author" : "Mark" } }
  27. > db.tuts.aggregate({'$project': {'title': '$title', 'author': '$author'}}, {'$group': {'_id': {'title': '$title', 'author': '$author', 'author_name': '$author'} }})
  28. { "_id" : { "title" : "WordPress Article", "author" : "Jenny", "author_name" : "Jenny" } }
  29. { "_id" : { "title" : "Post Title", "author" : "Helen", "author_name" : "Helen" } }
  30. { "_id" : { "title" : "Hello World", "author" : "Mark", "author_name" : "Mark" } }
  31. > db.tuts.find({}).pretty()
  32. {
  33. "_id" : ObjectId("571e06a776dcd773abde06c7"),
  34. "title" : "Hello World",
  35. "author" : "Mark",
  36. "created_at" : ISODate("2016-04-25T11:59:35.742Z"),
  37. "votes" : 0
  38. }
  39. {
  40. "_id" : ObjectId("571e06a776dcd773abde06c8"),
  41. "title" : "Post Title",
  42. "author" : "Helen",
  43. "created_at" : ISODate("2016-04-25T11:59:35.742Z"),
  44. "votes" : 0
  45. }
  46. {
  47. "_id" : ObjectId("571e06a776dcd773abde06c9"),
  48. "title" : "WordPress Article",
  49. "author" : "Jenny",
  50. "created_at" : ISODate("2016-04-25T11:59:35.742Z"),
  51. "votes" : 0
  52. }
  53. > db.tuts.aggregate({'$project': {'title': '$title', 'author': '$author'}}, {'$group': {'_id': {'title': '$title', 'author': '$author'} }})
  54. { "_id" : { "title" : "WordPress Article", "author" : "Jenny" } }
  55. { "_id" : { "title" : "Post Title", "author" : "Helen" } }
  56. { "_id" : { "title" : "Hello World", "author" : "Mark" } }
  57. > db.tuts.aggregate({'$project': {'title': '$title', 'author': '$author'}}, {'$group': {'_id': {'title': '$title', 'author': '$author', 'author_name': '$author'} }})
  58. { "_id" : { "title" : "WordPress Article", "author" : "Jenny", "author_name" : "Jenny" } }
  59. { "_id" : { "title" : "Post Title", "author" : "Helen", "author_name" : "Helen" } }
  60. { "_id" : { "title" : "Hello World", "author" : "Mark", "author_name" : "Mark" } }
  61. > db.tuts.aggregate({'$project': {'title': '$title', 'author': '$author'}}, {'$group': {'_id': '$title'}})
  62. { "_id" : "WordPress Article" }
  63. { "_id" : "Post Title" }
  64. { "_id" : "Hello World" }
  65. > db.tuts.aggregate({'$project': {'title': '$title', 'author': '$author'}}, {'$group': {'_id': '$title'}})
  66. { "_id" : "WordPress Article" }
  67. { "_id" : "Post Title" }
  68. { "_id" : "Hello World" }
  69. > db.tuts.aggregate({'$project': {'title': '$title', 'author': '$author'}}, {'$group': {'_id': '$title', 'num': {'$sum': 1}}})
  70. { "_id" : "WordPress Article", "num" : 1 }
  71. { "_id" : "Post Title", "num" : 1 }
  72. { "_id" : "Hello World", "num" : 1 }
  73. > db.tuts.find({})
  74. { "_id" : ObjectId("571e06a776dcd773abde06c7"), "title" : "Hello World", "author" : "Mark", "created_at" : ISODate("2016-04-25T11:59:35.742Z"), "votes" : 0 }
  75. { "_id" : ObjectId("571e06a776dcd773abde06c8"), "title" : "Post Title", "author" : "Helen", "created_at" : ISODate("2016-04-25T11:59:35.742Z"), "votes" : 0 }
  76. { "_id" : ObjectId("571e06a776dcd773abde06c9"), "title" : "WordPress Article", "author" : "Jenny", "created_at" : ISODate("2016-04-25T11:59:35.742Z"), "votes" : 0 }
  77. > db.tuts.insert({"title" : "Hello World", "author" : "Mark", "created_at" : new Date(), "votes" : 0 })
  78. WriteResult({ "nInserted" : 1 })
  79. > db.tuts.aggregate({'$project': {'title': '$title', 'author': '$author'}}, {'$group': {'_id': '$title', 'num': {'$sum': 1}}})
  80. { "_id" : "WordPress Article", "num" : 1 }
  81. { "_id" : "Post Title", "num" : 1 }
  82. { "_id" : "Hello World", "num" : 2 }
  83. > db.tuts.find({}).pretty()
  84. {
  85. "_id" : ObjectId("571e06a776dcd773abde06c7"),
  86. "title" : "Hello World",
  87. "author" : "Mark",
  88. "created_at" : ISODate("2016-04-25T11:59:35.742Z"),
  89. "votes" : 0
  90. }
  91. {
  92. "_id" : ObjectId("571e06a776dcd773abde06c8"),
  93. "title" : "Post Title",
  94. "author" : "Helen",
  95. "created_at" : ISODate("2016-04-25T11:59:35.742Z"),
  96. "votes" : 0
  97. }
  98. {
  99. "_id" : ObjectId("571e06a776dcd773abde06c9"),
  100. "title" : "WordPress Article",
  101. "author" : "Jenny",
  102. "created_at" : ISODate("2016-04-25T11:59:35.742Z"),
  103. "votes" : 0
  104. }
  105. {
  106. "_id" : ObjectId("571e776916393222690de28e"),
  107. "title" : "Hello World",
  108. "author" : "Mark",
  109. "created_at" : ISODate("2016-04-25T20:00:41.442Z"),
  110. "votes" : 0
  111. }
  112. > db.tuts.aggregate({'$project': {'title': '$title', 'author': '$author'}}, {'$group': {'_id': '$title', 'num': {'$sum': 1}}})
  113. { "_id" : "WordPress Article", "num" : 1 }
  114. { "_id" : "Post Title", "num" : 1 }
  115. { "_id" : "Hello World", "num" : 2 }
  116. > db.tuts.aggregate({'$project': {'title': '$title', 'author': '$author'}}, {'$group': {'_id': '$title', 'num': {'$sum': 0}}})
  117. { "_id" : "WordPress Article", "num" : 0 }
  118. { "_id" : "Post Title", "num" : 0 }
  119. { "_id" : "Hello World", "num" : 0 }
  120. > db.tuts.update({'title': 'Hello World'}, {'$inc': {'votes': 50}})
  121. WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
  122. > db.tuts.update({'title': 'Post Title'}, {'$inc': {'votes': 40}})
  123. WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
  124. > db.tuts.update({'title': 'WordPress Article'}, {'$inc': {'votes': 80}})
  125. WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
  126. > db.tuts.find({}).pretty()
  127. {
  128. "_id" : ObjectId("571e06a776dcd773abde06c7"),
  129. "title" : "Hello World",
  130. "author" : "Mark",
  131. "created_at" : ISODate("2016-04-25T11:59:35.742Z"),
  132. "votes" : 50
  133. }
  134. {
  135. "_id" : ObjectId("571e06a776dcd773abde06c8"),
  136. "title" : "Post Title",
  137. "author" : "Helen",
  138. "created_at" : ISODate("2016-04-25T11:59:35.742Z"),
  139. "votes" : 40
  140. }
  141. {
  142. "_id" : ObjectId("571e06a776dcd773abde06c9"),
  143. "title" : "WordPress Article",
  144. "author" : "Jenny",
  145. "created_at" : ISODate("2016-04-25T11:59:35.742Z"),
  146. "votes" : 80
  147. }
  148. {
  149. "_id" : ObjectId("571e776916393222690de28e"),
  150. "title" : "Hello World",
  151. "author" : "Mark",
  152. "created_at" : ISODate("2016-04-25T20:00:41.442Z"),
  153. "votes" : 0
  154. }
  155. > db.tuts.aggregate({'$project': {'title': '$title', 'author': '$author'}}, {'$group': {'_id': '$title', 'max_votes': {'$max': '$votes'}}})
  156. { "_id" : "WordPress Article", "max_votes" : null }
  157. { "_id" : "Post Title", "max_votes" : null }
  158. { "_id" : "Hello World", "max_votes" : null }
  159. > db.tuts.aggregate({'$project': {'title': '$title', 'author': '$author', 'votes': '$votes'}}, {'$group': {'_id': '$title', 'max_votes': {'$max': '$votes'}}})
  160. { "_id" : "WordPress Article", "max_votes" : 80 }
  161. { "_id" : "Post Title", "max_votes" : 40 }
  162. { "_id" : "Hello World", "max_votes" : 50 }
  163. > db.tuts.aggregate({'$project': {'title': '$title', 'author': '$author', 'votes': '$votes'}}, {'$group': {'_id': '$title', 'max_votes': {'$min': '$votes'}}})
  164. { "_id" : "WordPress Article", "max_votes" : 80 }
  165. { "_id" : "Post Title", "max_votes" : 40 }
  166. { "_id" : "Hello World", "max_votes" : 0 }
  167. > db.tuts.aggregate({'$project': {'title': '$title', 'author': '$author', 'votes': '$votes'}}, {'$group': {'_id': '$title', 'min_votes': {'$min': '$votes'}}})
  168. { "_id" : "WordPress Article", "min_votes" : 80 }
  169. { "_id" : "Post Title", "min_votes" : 40 }
  170. { "_id" : "Hello World", "min_votes" : 0 }
  171. > db.tuts.aggregate({'$project': {'title': '$title', 'author': '$author', 'votes': '$votes'}}, {'$group': {'_id': '$title', 'max_votes': {'$max': '$votes'}}})
  172. { "_id" : "WordPress Article", "max_votes" : 80 }
  173. { "_id" : "Post Title", "max_votes" : 40 }
  174. { "_id" : "Hello World", "max_votes" : 50 }
  175. > db.tuts.aggregate({'$project': {'title': '$title', 'author': '$author', 'votes': '$votes'}}, {'$group': {'_id': '$title', 'min_votes': {'$min': '$votes'}}})
  176. { "_id" : "WordPress Article", "min_votes" : 80 }
  177. { "_id" : "Post Title", "min_votes" : 40 }
  178. { "_id" : "Hello World", "min_votes" : 0 }
  179. > db.tuts.aggregate({'$project': {'title': '$title', 'author': '$author', 'votes': '$votes'}}, {'$group': {'_id': '$title', 'first': {'$first': '$votes'}}})
  180. { "_id" : "WordPress Article", "first" : 80 }
  181. { "_id" : "Post Title", "first" : 40 }
  182. { "_id" : "Hello World", "first" : 50 }
  183. > db.tuts.aggregate({'$project': {'title': '$title', 'author': '$author', 'votes': '$votes'}}, {'$group': {'_id': '$title', 'last': {'$last': '$votes'}}})
  184. { "_id" : "WordPress Article", "last" : 80 }
  185. { "_id" : "Post Title", "last" : 40 }
  186. { "_id" : "Hello World", "last" : 0 }
  187. > db.tuts.aggregate({'$match': {'title': 'WordPress Article'}}, {'$project': {'comments': '$comments'}}, {'$unwind': '$comments'})
  188. { "_id" : ObjectId("571e06a776dcd773abde06c9"), "comments" : { "content" : "bla bla1" } }
  189. { "_id" : ObjectId("571e06a776dcd773abde06c9"), "comments" : { "content" : "bla bla2" } }
  190. { "_id" : ObjectId("571e06a776dcd773abde06c9"), "comments" : { "content" : "bla bla3" } }
  191. { "_id" : ObjectId("571e06a776dcd773abde06c9"), "comments" : { "content" : "bla bla4" } }
  192. > db.tuts.aggregate({'$match': {'title': 'WordPress Article'}}, {'$project': {'comments': '$comments'}}, {'$unwind': '$comments'}, {'$skip': 1})
  193. { "_id" : ObjectId("571e06a776dcd773abde06c9"), "comments" : { "content" : "bla bla2" } }
  194. { "_id" : ObjectId("571e06a776dcd773abde06c9"), "comments" : { "content" : "bla bla3" } }
  195. { "_id" : ObjectId("571e06a776dcd773abde06c9"), "comments" : { "content" : "bla bla4" } }
  196. > db.tuts.aggregate({'$match': {'title': 'WordPress Article'}}, {'$project': {'comments': '$comments'}}, {'$unwind': '$comments'}, {'$skip': 1}, {'$limit': 2})
  197. { "_id" : ObjectId("571e06a776dcd773abde06c9"), "comments" : { "content" : "bla bla2" } }
  198. { "_id" : ObjectId("571e06a776dcd773abde06c9"), "comments" : { "content" : "bla bla3" } }
  199. > db.tuts.aggregate({'$match': {'title': 'WordPress Article'}}, {'$project': {'comments': '$comments'}}, {'$unwind': '$comments'}, {'$skip': 1}, {'$limit': 2}, {'$sort':{'comments.content': 1}})
  200. { "_id" : ObjectId("571e06a776dcd773abde06c9"), "comments" : { "content" : "bla bla2" } }
  201. { "_id" : ObjectId("571e06a776dcd773abde06c9"), "comments" : { "content" : "bla bla3" } }
  202. > db.tuts.aggregate({'$match': {'title': 'WordPress Article'}}, {'$project': {'comments': '$comments'}}, {'$unwind': '$comments'}, {'$skip': 1}, {'$limit': 2}, {'$sort':{'comments.content': -1}})
  203. { "_id" : ObjectId("571e06a776dcd773abde06c9"), "comments" : { "content" : "bla bla3" } }
  204. { "_id" : ObjectId("571e06a776dcd773abde06c9"), "comments" : { "content" : "bla bla2" } }

MapReduce

  1. db.orders.insert([{'cust_id': 'A123', 'amount': 500, 'status': 'A'},{'cust_id': 'A124', 'amount': 600, 'status': 'B'},{'cust_id': 'A125', 'amount': 700, 'status': 'C'}])
  2. db.orders.mapReduce(function(){ emit(this.cust_id, this.amount); }, function(key, values){ return Array.sum(values); },{ 'query': {'status':'A'}, 'out': 'order_totals' })