Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.10 KB | None | 0 0
  1. > intorductioon
  2. mongod is a database server , this should be up and running
  3. mongod --port = PORT will be running mongo server on perticular server
  4. mongo is a mongoDB shell , this is used to do all mongo operation , thought we haave DRIVERS for all kind of platform
  5.  
  6. Shell command are almost similar as JAVAscript driver for nodejs
  7.  
  8. show dbs > show databases
  9. use DataBase_name > create a database
  10. db.collectionName.insertOne({JSON_Document}) > create a collection and inserting a document into it
  11.  
  12. > CRUD operations
  13. Create : insertOne(data,options) , insertMany(data,options)
  14. Read : find(filter,options), findOne(filter,options)
  15. Update : updateOne(filter,data,options) , updateMany(filter,data,options), replaceOne(filter,data,options)
  16. Delete : deleteOne(filter,options), deleteMany(filter,options)
  17.  
  18. UpdateMany({},{$set : {marker:"delete"}}) this will set field in all dcuments as FILTER is {}
  19. DelateMany({}) This will delete all data
  20.  
  21. Above two will only work with Many() operations
  22.  
  23. >Finding
  24. options can contain matching key value pair
  25. Note :*
  26. [
  27. {
  28. "departureAirport": "MUC",
  29. "arrivalAirport": "SFO",
  30. "aircraft": "Airbus A380",
  31. "distance": 12000,
  32. "intercontinental": true
  33. },
  34. {
  35. "departureAirport": "LHR",
  36. "arrivalAirport": "TXL",
  37. "aircraft": "Airbus A320",
  38. "distance": 950,
  39. "intercontinental": false
  40. }
  41. ]
  42.  
  43. db.flightData.find({distance: {$gt:1000}})
  44. ** $gt is greater than symbol , similarly we have $lt //// in update we had $set
  45.  
  46. > Update
  47. in update we saw we had updateOne(), updateMany(),ReplaceOne() BUT we have update()
  48. this do not take $set key , it takes entire object and replace existing contentt with new object
  49.  
  50. > Find
  51. when we write a find query it return a cursor which is a iterator on data list
  52. when we type "it" it send new set of data
  53. to convert data recieved from find we need function as follows
  54. db.passengers.find().toArray() >>> this will return entire array
  55.  
  56. forEach()
  57. db.passengers.forEach((data)=>printjson(data))
  58.  
  59. *note here find is returning a cursor , nwo the inbuild method forEach is accessing cursor to get data one by one
  60. so it is very efficient , it do not access entire data in one shot , it iterate over a cursor
  61.  
  62.  
  63. >PROJECTION
  64. when we query data from database we get unnecessory data along with required data like some extra fileds
  65. and we filter out those extra fields in out application In almost all programming language
  66. here comes the concept of projection , this removes unneessory data at mongodb server
  67.  
  68. we get data by using find()
  69. now we add projection to find by adding an object as a second parameter
  70. db.passenges.find({},{name:1,_id:0}) // this will remove id as well , this is a explicit call to remove _id
  71. with help of projection we can save network bandwidth
  72.  
  73. > Embeded documents
  74. documents within document , there is a limit of nesting document till 100 levels and maximum size of a document can be 16 MB
  75.  
  76. >Array in documents
  77. an array can be part of a documents , this arraya can contain a document , a number , a string
  78.  
  79. =================================================================================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement