Mongo Queries2
Mongo Queries2
Example
Following example inserts three different documents into the empDetails collection
using the insertMany() method.
> db.empDetails.insertMany(
[
{
First_Name: "Radhika",
Last_Name: "Sharma",
Date_Of_Birth: "1995-09-26",
e_mail: "[email protected]",
phone: "9000012345"
},
{
First_Name: "Rachel",
Last_Name: "Christopher",
Date_Of_Birth: "1990-02-16",
e_mail: "[email protected]",
phone: "9000054321"
},
{
First_Name: "Fathima",
Last_Name: "Sheik",
Date_Of_Birth: "1990-02-16",
e_mail: "[email protected]",
phone: "9000054321"
}
]
)
{
"acknowledged" : true,
"insertedIds" : [
ObjectId("5dd631f270fb13eec3963bed"),
ObjectId("5dd631f270fb13eec3963bee"),
ObjectId("5dd631f270fb13eec3963bef")
]
}
>
>db.COLLECTION_NAME.find()
>db.COLLECTION_NAME.find().pretty()
Syntax
>db.COLLECTIONNAME.findOne()
Example
Following example retrieves the document with title MongoDB Overview.
> db.myc–––––ol.findOne({title: "MongoDB Overview"})
{
"_id" : ObjectId("5dd6542170fb13eec3963bf0"),
"title" : "MongoDB Overview",
"description" : "MongoDB is no SQL database",
"by" : "tutorials point",
"url" : "http://www.tutorialspoint.com",
"tags" : [
"mongodb",
"database",
"NoSQL"
],
"likes" : 100
}
To query the document on the basis of some condition, you can use following
operations.
AND in MongoDB
Syntax
To query documents based on the AND condition, you need to use $and keyword.
Following is the basic syntax of AND −
>db.mycol.find({ $and: [ {<key1>:<value1>}, { <key2>:<value2>} ] })
Example
Following example will show all the tutorials written by 'tutorials point' and whose title is
'MongoDB Overview'.
> db.mycol.find({$and:[{"by":"tutorials point"},{"title": "MongoDB
Overview"}]}).pretty()
{
"_id" : ObjectId("5dd4e2cc0821d3b44607534c"),
"title" : "MongoDB Overview",
"description" : "MongoDB is no SQL database",
"by" : "tutorials point",
"url" : "http://www.tutorialspoint.com",
"tags" : [
"mongodb",
"database",
"NoSQL"
],
"likes" : 100
}
>
For the above given example, equivalent where clause will be ' where by = 'tutorials
point' AND title = 'MongoDB Overview' '. You can pass any number of key, value
pairs in find clause.
OR in MongoDB
Syntax
To query documents based on the OR condition, you need to use $or keyword.
Following is the basic syntax of OR −
>db.mycol.find(
{
$or: [
{key1: value1}, {key2:value2}
]
}
).pretty()
Example
Following example will show all the tutorials written by 'tutorials point' or whose title is
'MongoDB Overview'.
>db.mycol.find({$or:[{"by":"tutorials point"},{"title": "MongoDB
Overview"}]}).pretty()
{
"_id": ObjectId(7df78ad8902c),
"title": "MongoDB Overview",
"description": "MongoDB is no sql database",
"by": "tutorials point",
"url": "http://www.tutorialspoint.com",
"tags": ["mongodb", "database", "NoSQL"],
"likes": "100"
}
>
NOR in MongoDB
Syntax
To query documents based on the NOT condition, you need to use $not keyword.
Following is the basic syntax of NOT −
>db.COLLECTION_NAME.find(
{
$not: [
{key1: value1}, {key2:value2}
]
}
)
The basic syntax of MongoDB save() method is shown below −
>db.COLLECTION_NAME.save({_id:ObjectId(),NEW_DATA})
Example
Following example will replace the document with the _id '5983548781331adf45ec5'.
>db.mycol.save(
{
"_id" : ObjectId("507f191e810c19729de860ea"),
"title":"Tutorials Point New Topic",
"by":"Tutorials Point"
}
)
WriteResult({
"nMatched" : 0,
"nUpserted" : 1,
"nModified" : 0,
"_id" : ObjectId("507f191e810c19729de860ea")
})
>db.mycol.find()
{ "_id" : ObjectId("507f191e810c19729de860e6"), "title":"Tutorials
Point New Topic",
"by":"Tutorials Point"}
{ "_id" : ObjectId("507f191e810c19729de860e6"), "title":"NoSQL
Overview"}
{ "_id" : ObjectId("507f191e810c19729de860e6"), "title":"Tutorials
Point Overview"}
>
MongoDB findOneAndUpdate() method
The findOneAndUpdate() method updates the values in the existing document.
Syntax
The basic syntax of findOneAndUpdate() method is as follows −
>db.COLLECTION_NAME.findOneAndUpdate(SELECTIOIN_CRITERIA,
UPDATED_DATA)
Example
Assume we have created a collection named empDetails and inserted three
documents in it as shown below −
> db.empDetails.insertMany(
[
{
First_Name: "Radhika",
Last_Name: "Sharma",
Age: "26",
e_mail: "[email protected]",
phone: "9000012345"
},
{
First_Name: "Rachel",
Last_Name: "Christopher",
Age: "27",
e_mail: "[email protected]",
phone: "9000054321"
},
{
First_Name: "Fathima",
Last_Name: "Sheik",
Age: "24",
e_mail: "[email protected]",
phone: "9000054321"
}
]
)
Following example updates the age and email values of the document with name
'Radhika'.
> db.empDetails.findOneAndUpdate(
{First_Name: 'Radhika'},
{ $set: { Age: '30',e_mail: '[email protected]'}}
)
{
"_id" : ObjectId("5dd6636870fb13eec3963bf5"),
"First_Name" : "Radhika",
"Last_Name" : "Sharma",
"Age" : "30",
"e_mail" : "[email protected]",
"phone" : "9000012345"
}
Syntax
The basic syntax of updateOne() method is as follows −
>db.COLLECTION_NAME.updateOne(<filter>, <update>)
Example
> db.empDetails.updateOne(
{First_Name: 'Radhika'},
{ $set: { Age: '30',e_mail: '[email protected]'}}
)
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 0 }
>
Syntax
The basic syntax of updateMany() method is as follows −
>db.COLLECTION_NAME.update(<filter>, <update>)
Example
> db.empDetails.updateMany(
{Age:{ $gt: "25" }},
{ $set: { Age: '00'}}
)
{ "acknowledged" : true, "matchedCount" : 2, "modifiedCount" : 2 }
You can see the updated values if you retrieve the contents of the document using the
find method as shown below −
> db.empDetails.find()
{ "_id" : ObjectId("5dd6636870fb13eec3963bf5"), "First_Name" :
"Radhika", "Last_Name" : "Sharma", "Age" : "00", "e_mail" :
"[email protected]", "phone" : "9000012345" }
{ "_id" : ObjectId("5dd6636870fb13eec3963bf6"), "First_Name" :
"Rachel", "Last_Name" : "Christopher", "Age" : "00", "e_mail" :
"[email protected]", "phone" : "9000054321" }
{ "_id" : ObjectId("5dd6636870fb13eec3963bf7"), "First_Name" :
"Fathima", "Last_Name" : "Sheik", "Age" : "24", "e_mail" :
"[email protected]", "phone" : "9000054321" }
>
Syntax
The basic syntax of sort() method is as follows −
>db.COLLECTION_NAME.find().sort({KEY:1})
Example
Consider the collection myycol has the following data.
{_id : ObjectId("507f191e810c19729de860e1"), title: "MongoDB
Overview"}
{_id : ObjectId("507f191e810c19729de860e2"), title: "NoSQL
Overview"}
{_id : ObjectId("507f191e810c19729de860e3"), title: "Tutorials
Point Overview"}
Following example will display the documents sorted by title in the descending order.
>db.mycol.find({},{"title":1,_id:0}).sort({"title":-1})
{"title":"Tutorials Point Overview"}
{"title":"NoSQL Overview"}
{"title":"MongoDB Overview"}
>
Please note, if you don't specify the sorting preference, then sort() method will display
the documents in ascending order.
use book
switched to db book
WriteResult({ "nInserted" : 1 })
> db.book.find()
{ "_id" : ObjectId("6150a0f9f945783949c3ffb3"), "bid" : 2, "bname" : "DBMS priciples", "Author" :
"korth", "edition" : 4 }
WriteResult({ "nInserted" : 1 })
WriteResult({ "nInserted" : 1 })
> db.book.remove({'bid'=5})
@(shell):1:21
> db.book.remove({"bid"="5"})
@(shell):1:21
> db.book.remove({"bid"=5})
@(shell):1:21
WriteResult({ "nRemoved" : 2 })
WriteResult({ "nInserted" : 1 })
> db.book.find()
> db.book.find().pretty();
"_id" : ObjectId("6150a1b2f945783949c3ffb5"),
"bid" : 5,
"Author" : "Navathe",
"edition" : 4
"_id" : ObjectId("6150a40ef945783949c3ffb6"),
"bid" : 2,
"Author" : "korth",
"edition" : 4
> db.book.find({edition:4}).pretty()
"_id" : ObjectId("6150a1b2f945783949c3ffb5"),
"bid" : 5,
"Author" : "Navathe",
"edition" : 4
"_id" : ObjectId("6150a40ef945783949c3ffb6"),
"bid" : 2,
"Author" : "korth",
"edition" : 4
"_id" : ObjectId("6150a40ef945783949c3ffb6"),
"bid" : 2,
"Author" : "korth",
"edition" : 4
> db.book.findOne()
"_id" : ObjectId("6150a1b2f945783949c3ffb5"),
"bid" : 5,
"Author" : "Navathe",
"edition" : 4
"acknowledged" : true,
"insertedId" : ObjectId("6150a617f945783949c3ffb7")
"acknowledged" : true,
"insertedIds" : [
ObjectId("6150a811f945783949c3ffb8"),
ObjectId("6150a811f945783949c3ffb9")
Update command:
Ascending order:
Descending order
>
db.book.find().limit(2).pretty()
"_id" : ObjectId("6150a1b2f945783949c3ffb5"),
"bid" : 5,
"Author" : "Navathe",
"edition" : 4
"_id" : ObjectId("6150a40ef945783949c3ffb6"),
"bid" : 2,
"Author" : "korth",
"edition" : 4
}
db.book.find({edition: {$gt:3}}).pretty()
"_id" : ObjectId("6150a1b2f945783949c3ffb5"),
"bid" : 5,
"Author" : "Navathe",
"edition" : 4
"_id" : ObjectId("6150a40ef945783949c3ffb6"),
"bid" : 2,
"Author" : "korth",
"edition" : 4
"_id" : ObjectId("6150a617f945783949c3ffb7"),
"bid" : 8,
"bname" : "SE",
"Author" : "Pressman",
"edition" : 5
}
{
"_id" : ObjectId("6150a811f945783949c3ffb8"),
"bid" : 9,
"bname" : "os",
"Author" : "Tanenbaum",
"edition" : 5
And operator
"_id" : ObjectId("6150a40ef945783949c3ffb6"),
"bid" : 2,
"Author" : "korth",
"edition" : 4
OR operator
db.book.find({$or: [{"Author":"korth"},{"Author":"Pressman"}]}).pretty()
"_id" : ObjectId("6150a40ef945783949c3ffb6"),
"bid" : 2,
"Author" : "korth",
"edition" : 4
}
{
"_id" : ObjectId("6150a617f945783949c3ffb7"),
"bid" : 8,
"bname" : "SE",
"Author" : "Pressman",
"edition" : 5
"_id" : ObjectId("6150a1b2f945783949c3ffb5"),
"bid" : 5,
"Author" : "Navathe",
"edition" : 4
"_id" : ObjectId("6150a40ef945783949c3ffb6"),
"bid" : 2,
"Author" : "korth",
"edition" : 4
{
"_id" : ObjectId("6150a617f945783949c3ffb7"),
"bid" : 8,
"bname" : "SE",
"Author" : "Pressman",
"edition" : 5
"_id" : ObjectId("6150a811f945783949c3ffb8"),
"bid" : 9,
"bname" : "os",
"Author" : "Tanenbaum",
"edition" : 5
Remove all
db.student.remove({})
WriteResult({ "nRemoved" : 0 })