1. show databases
  2. # implicitly create db
  3. use xxx_db
  4. db.createCollection('users')
  5. # INSERT
  6. db.xxxc.insertOne({x: 1})
  7. db.xxxc.insertMany([{}, {}])
  8. # QUERY
  9. # search in xxx_collection
  10. # with status=='Success' and only return name filed
  11. # implecitly create collectio and each document's _id filed
  12. db.collection.find({'status': 'Success'}, {'name': 1, 'age': 0})
  13. db.collection.find({'status': {'$in': ['A', 'B']}, 'name': 'B'}) # operator
  14. db.collection.find({'com.size': {'$lt': 15}})
  15. # for array field
  16. db.collection.find({'tags': "red"}) # red in tags
  17. db.collection.find({'tags': ['red', 'black']}) # ['red', 'black'] == tags
  18. db.collection.find({'tags': {'$all': ['red', 'black']}}) # ['red', 'black'] in tags
  19. db.collection.find({'tags': {'$lt': 5}}) # with element in tags < 5
  20. db.collection.find({'tags': {'$lt': 5, '$gt': 20}}) # OR with element in tags < 5 or > 20
  21. db.inventory.find( { 'tags': { '$elemMatch': { '$gt': 22, '$lt': 30 } } } ) # AND multi version of the above
  22. db.inventory.find( { "tags.1": { '$gt': 25 } } ) # the second element in tags > 25
  23. db.inventory.find( { "tags": { '$size': 3 } } ) # len(tags) == 3
  24. # project field
  25. db.inventory.find( { status: "A" }, { item: 1, status: 1, _id: 0 } )
  26. db.inventory.find({stateus, 'A'}, { item: 1, status: 1, size: { uom: 1 } })
  27. # for instock array, return the last one
  28. db.inventory.find( { status: "A" }, { item: 1, status: 1, instock: { $slice: -1 } } )
  29. cursor = db.collection.find()
  30. for item in cursor:
  31. print(item)
  32. # UPDATE
  33. db.xxxc.updateOne()
  34. db.xxxc.updateMany()
  35. db.xxxc.replaceOne()
  36. # use update operations
  37. # $addFields $set $project $unset $replaceRoot $replaceWith
  38. db.students.updateOne( { _id: 3 }, [ { $set: { "test3": 98, modified: "$$NOW"} } ] )
  39. db.students2.updateMany( {},
  40. [
  41. { $replaceRoot: { newRoot:
  42. { $mergeObjects: [ { quiz1: 0, quiz2: 0, test1: 0, test2: 0 }, "$$ROOT" ] }
  43. } },
  44. { $set: { modified: "$$NOW"} }
  45. ]
  46. )
  47. db.collection.update() when used with the upsert: true option.
  48. db.collection.updateOne() when used with the upsert: true option.
  49. db.collection.updateMany() when used with the upsert: true option.
  50. db.collection.findAndModify() when used with the upsert: true option.
  51. db.collection.findOneAndUpdate() when used with the upsert: true option.
  52. db.collection.findOneAndReplace() when used with the upsert: true option.
  53. db.collection.save().
  54. db.collection.bulkWrite()
  55. db.collection.deleteOne()
  56. db.collection.deleteMany()
  57. db.collection.remove()
  58. db.collection.findOneAndDelete()
  59. db.collection.findAndModify().
  60. db.collection.findAndModify() provides a sort option. The option allows for the deletion of the first document sorted by the specified order.
  61. db.collection.bulkWrite().

views

Capped Collections