Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- > intorductioon
- mongod is a database server , this should be up and running
- mongod --port = PORT will be running mongo server on perticular server
- mongo is a mongoDB shell , this is used to do all mongo operation , thought we haave DRIVERS for all kind of platform
- Shell command are almost similar as JAVAscript driver for nodejs
- show dbs > show databases
- use DataBase_name > create a database
- db.collectionName.insertOne({JSON_Document}) > create a collection and inserting a document into it
- > CRUD operations
- Create : insertOne(data,options) , insertMany(data,options)
- Read : find(filter,options), findOne(filter,options)
- Update : updateOne(filter,data,options) , updateMany(filter,data,options), replaceOne(filter,data,options)
- Delete : deleteOne(filter,options), deleteMany(filter,options)
- UpdateMany({},{$set : {marker:"delete"}}) this will set field in all dcuments as FILTER is {}
- DelateMany({}) This will delete all data
- Above two will only work with Many() operations
- >Finding
- options can contain matching key value pair
- Note :*
- [
- {
- "departureAirport": "MUC",
- "arrivalAirport": "SFO",
- "aircraft": "Airbus A380",
- "distance": 12000,
- "intercontinental": true
- },
- {
- "departureAirport": "LHR",
- "arrivalAirport": "TXL",
- "aircraft": "Airbus A320",
- "distance": 950,
- "intercontinental": false
- }
- ]
- db.flightData.find({distance: {$gt:1000}})
- ** $gt is greater than symbol , similarly we have $lt //// in update we had $set
- > Update
- in update we saw we had updateOne(), updateMany(),ReplaceOne() BUT we have update()
- this do not take $set key , it takes entire object and replace existing contentt with new object
- > Find
- when we write a find query it return a cursor which is a iterator on data list
- when we type "it" it send new set of data
- to convert data recieved from find we need function as follows
- db.passengers.find().toArray() >>> this will return entire array
- forEach()
- db.passengers.forEach((data)=>printjson(data))
- *note here find is returning a cursor , nwo the inbuild method forEach is accessing cursor to get data one by one
- so it is very efficient , it do not access entire data in one shot , it iterate over a cursor
- >PROJECTION
- when we query data from database we get unnecessory data along with required data like some extra fileds
- and we filter out those extra fields in out application In almost all programming language
- here comes the concept of projection , this removes unneessory data at mongodb server
- we get data by using find()
- now we add projection to find by adding an object as a second parameter
- db.passenges.find({},{name:1,_id:0}) // this will remove id as well , this is a explicit call to remove _id
- with help of projection we can save network bandwidth
- > Embeded documents
- documents within document , there is a limit of nesting document till 100 levels and maximum size of a document can be 16 MB
- >Array in documents
- an array can be part of a documents , this arraya can contain a document , a number , a string
- =================================================================================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement