BDA - MongoDB
BDA - MongoDB
4) Creating a collection :
createCollection() method - db.createCollection(“students”)
insert method
- db.students.insertOne(*object*)
- creates a collection ‘students’ if it doesn’t exist and inserts the object.
5) Inserting documents :
Inserting one document – db.students.insertOne({*object*})
Inserting multiple documents – db.students.insertMany( [{*object*}, {*object*},{ }…] )
6) Displaying records :
Syntax - db.students.find({*query*}, {*projection*})
- Query parameter :
Used to filter data
- Projection parameter :
Used to describe the fields to be included/returned.
If left empty, all fields are included.
Use ‘true’ to include a field and ‘false’ to exclude a field.
Displaying all records – db.students.find()
Displaying one record :
- db.students.findOne( { }, { } )
- If left empty, it returns the first document.
7) Updating documents :
Syntax – db.students.updateOne({*filter*}, {*update*}) OR
db.students.updateMany({ }, { })
- Filter parameter :
Used to describe/filter out the documents to be updated.
- Update parameter :
Used to define the updation.
Updating one document :
- db.students.updateOne( { }, { } )
- updates the first document that is found matching the query.
Updating multiple documents :
- db.students.updateMany( { }, { } )
Updation methods :
- $set - to set a value to a field
- $unset – to remove a field
- $inc – to increment a field
- $rename – to rename a field
Updating the cgpa of john to be 9
1) $group :
- This stage groups documents by the unique _id parameter.
- Syntax – db.students.aggregate( [ {$group { _id : “ “ } } ] )
- NOTE : this _id is different from the unique ObjectId provided to each object.
4) $project :
- This stage passes only the specified fields onto the next stage.
- Similar to the projection parameter used with the find() method.
Returning only the name, age and cgpa of all students
5) $count :
- This stage counts the number of documents passed on by the previous stage.