0% found this document useful (0 votes)
21 views2 pages

MongoDB_Quering

Uploaded by

Bhakti Joshi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views2 pages

MongoDB_Quering

Uploaded by

Bhakti Joshi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

MongoDB

 Database Name – BBIT


o Collection Name – student

 show dbs - show all existing databases

 use BBIT - switch to any database

 insertOne() - Insert one document

db.students.insertOne({ "sid": 101, "name": "Jinal", "age": 20, "subjects": ["Math", "Science", "English"] })

 insertMany() – Insert multiple document

db.student.insertMany( [ { "sid": 102, "name": "Meet", "age": 26, "subjects": ["Math", "Physics"] },

{ "sid": 103, "name": "Jeel", "age": 20, "subjects": ["Chemistry", "Biology"] },

{ "sid": 104, "name": "Jeel", "age": 22, "subjects": ["History", "Geography"] } ] )

 find() – Search document in document

find( filter, projection )


- filter for specify condition
- projection for include or exclude field - { field1: 1, field2: 0, ... } // 1 for include, 2 for
exclude

db.students.find()

db.students.find({ "name": "Jeel" })

db.students.find({ "name": "Jeel" }, { "age" : { $gt: 20 } }) - AND operation

db.student.find({ $or: [{ "name": "Jeel" }, { "name": "Jinal" }] }) - OR operation

or

db.student.find({ name: { $in: ["Math", "Science"] } } ) - OR operation

db.student.find().limit(2) - return only 2 documents

db.student.find().skip(2) - skip 2 documents and return rest documents

 updateOne() - modify a single document

db.student.updateOne(
{ name: "Meet" }, // Filter criteria
{ $set: { age: 19 } } ) // Update operation

 updateMany() – modify multiple document

db.student.updateMany(
{ name: "Jeel" }, // Filter criteria ( more than one Jeel )
{ $set: { age: 25 } } ) // Update operation

db.student.updateMany( {}, { $inc: {‘age’: 1}} ) //update all document , age by increment of 1 year

 deleteOne() – delete document

db.student.deleteOne( {name: "Meet" } )

 deleteMany() – delete multiple document

db.student.deleteMany( {name: "Jeel" } )

db.student.deleteMany( {} ) //delete all document in student collection

You might also like