nosqlref.txt 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. docker run -it -v /home/raman/.mongodata:/data/db -p 27017:27017 --name mongodb -d mongo
  2. docker logs mongodb
  3. docker start mongodb
  4. docker exec -it mongodb bash
  5. mongo -host localhost -port 27017
  6. show startupWarnings
  7. exit
  8. apt-get install vim openssh-server openssh-client
  9. ssh-keygen -t rsa
  10. sudo service ssh status
  11. sudo service ssh start
  12. ssh user@<ipaddress>
  13. # current version of mongodb
  14. version()
  15. # list of commands
  16. db.help()
  17. # database name, number of collection and documents in the database
  18. db.stats()
  19. Note: In MongoDB, the default database is test.
  20. # database list
  21. show databases/dbs
  22. [to display database, insert atleast one document into it]
  23. # create new database if doesn't exist else return existing database
  24. use <database>
  25. # check currently selected database
  26. db
  27. # collection list
  28. show collections
  29. # documents list
  30. db.<collection>.find()
  31. db.<collection>.find().pretty()
  32. # insert
  33. db.<collection>.insert({key: 'value'})
  34. # drop database
  35. db.dropDatabase()
  36. [delete selected database]
  37. [if not selected any database, then it delete default 'test' database]
  38. # create collection
  39. db.createCollection(name, options)
  40. [name - name of the collection, options - a document which can be used to specify configurations for the collection]
  41. db.createCollection("Departments", {capped: true, size: 5242880, max: 5000})
  42. [this will create a collection named "Departments", with maximum size of 5 MB and maximum of 5000 documents]
  43. # collection list
  44. show collections
  45. # drop collection
  46. db.<collection>.drop()
  47. [drop() method will return true, if the selected collection is dropped successfully, otherwise it will return false]
  48. # CRUD operations
  49. structure of MongoDB document
  50. {
  51. field1: value1;
  52. field2: value2;
  53. .
  54. .
  55. .
  56. fieldN: valueN;
  57. }
  58. 1. INSERT documents
  59. db.<collection>.insert(document)
  60. [_id is 12 bytes hexadecimal number unique for every document in a collection]
  61. [_id: ObjectId (4 bytes timestamp, 3 bytes machineId, 2 bytes processId, 3 bytes incrementer)]
  62. db.<collection>.insertOne() # to insert one document
  63. db.<collection>.insertMany([]) # to insert multiple documents
  64. post = {title: "My blog post", content: "Here's is my blog post.", date: new Date()}
  65. db.<collection>.insert(post)
  66. 2. QUERY(Find)
  67. db.<collection>.find() # query all documents in a collection
  68. criteria = {name: "Raman"}
  69. db.<collection>.find(criteria) # query documents of a collection based on a criteria
  70. db.<collection>.find().pretty() # it display the results in a formatted way
  71. db.<collection>.findOne() # it returns only one document
  72. Projection:
  73. db.<collection>.find(query_document, projection_document)
  74. projection_doc = {"_id": 0, "Date": 1, "Time": 1, "Title": 1, "Speaker": 1}
  75. db.<collection>.find({}, projection_doc)
  76. 3. UPDATE
  77. db.<collection>.update(criteria, update)
  78. db.<collection>.update({name: 'MongoDB overview'}, {$set: {name: 'New MongoDB overview'}})
  79. db.<collection>.update({name: 'MongoDB overview'}, {$set: {name: 'New MongoDB overview'}}, {multi: true})
  80. 4. DELETE document
  81. db.<collection>.remove(deletion_criteria)
  82. Remove all documents: db.<collection>.remove({})
  83. # count No. of documents
  84. db.<collection>.countDocuments({}) # count all documents
  85. db.<collection>.countDocuments({Salary: 71000}) # count all documents with field Salary with value 71000
  86. # limit No. of documents display
  87. db.<collection>.find().limit(N) # display 1st N documents
  88. # skip No. of documents display
  89. db.<collection>.find().skip(N) # skip N documents and display
  90. # skip with limit documents display
  91. db.<collection>.find().skip(1).limit(2)
  92. [1 document is skipped and the display is limited to 2 documents]
  93. # sort documents
  94. db.<collection>.find().sort({Field: sorting_order})
  95. [sorting order 1 for ascending and -1 for descending]
  96. db.<collection>.find().sort({"Salary": 1})
  97. db.<collection>.find().sort({"Salary": -1})
  98. # comparison operators
  99. db.<collection>.find({Salary: {$eq: 71000}}) # equals
  100. db.<collection>.find({Salary: {$ne: 71000}}) # not equals
  101. db.<collection>.find({Salary: {$lt: 75000}}) # less than
  102. db.<collection>.find({Salary: {$lte: 75000}}) # less than equals
  103. db.<collection>.find({Salary: {$gt: 75000}}) # greater than
  104. db.<collection>.find({Salary: {$gte: 75000}}) # greater than equals
  105. db.<collection>.find({Salary: {$in: [62500, 71000]}}) # values in an array
  106. db.<collection>.find({Salary: {$nin: [62500, 71000]}}) # values not in an array
  107. # Notes
  108. For multi-machine replication:
  109. 1. Benefit: Data is always highly available
  110. 2. If master fails any of the secondary will act as master (on the basis of election)
  111. robo 3t 1.4
  112. bindIP: localhost, hostname, ipaddress
  113. ps -ef | grep mongo*
  114. sudo kill -9 3455
  115. fork = create process