...

Database Managment - Mongo DB

Back to Course

Lesson Description


Lession - #1412 MongoDB Query Document


MongoDB - Query Document

The find(>
Method To inquiry information from MongoDB assortment, you want to utilize MongoDB's find(>
strategy. Linguistic structure The essential punctuation of find(>
strategy is as per the following −

>db.COLLECTION_NAME.find(>

find(>
technique will show every one of the records in a non-organized manner.
Example: Expect we have made an assortment named mycol as −

> use sampleDB
changed to db sampleDB
> db.createCollection("mycol">
{ "Alright" : 1 } >
Furthermore, embedded 3 archives in it utilizing the addition(>
technique as displayed beneath −

> db.mycol.insert([
	{
		title: "MongoDB Overview",
		description: "MongoDB is no SQL database",
		by: "tutorials point",
		url: "http://www.tutorialspoint.com",
		tags: ["mongodb", "database", "NoSQL"],
		likes: 100
	},
	{
		title: "NoSQL Database",
		description: "NoSQL database doesn't have tables",
		by: "tutorials point",
		url: "http://www.tutorialspoint.com",
		tags: ["mongodb", "database", "NoSQL"],
		likes: 20,
		comments: [
			{
				user:"user1",
				message: "My first comment",
				dateCreated: new Date(2013,11,10,2,35>
, like: 0 } ] } ]>

Following method retrieves all the documents in the collection −

> db.mycol.find(>
{ "_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 } { "_id" : ObjectId("5dd4e2cc0821d3b44607534d">
, "title" : "NoSQL Database", "description" : "NoSQL database doesn't have tables", "by" : "tutorials point", "url" : "http://www.tutorialspoint.com", "tags" : [ "mongodb", "database", "NoSQL" ], "likes" : 20, "comments" : [ { "user" : "user1", "message" : "My first comment", "dateCreated" : ISODate("2013-12-09T21:05:00Z">
, "like" : 0 } ] } >

The pretty(>
Method

To show the outcomes in an arranged manner, you can utilize pretty(>
technique.

>db.COLLECTION_NAME.find(>
.pretty(>

Example Following model recovers every one of the reports from the assortment named mycol and organizes them in a simple to-understand design.

> db.mycol.find(>
.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 } { "_id" : ObjectId("5dd4e2cc0821d3b44607534d">
, "title" : "NoSQL Database", "description" : "NoSQL database doesn't have tables", "by" : "tutorials point", "url" : "http://www.tutorialspoint.com", "tags" : [ "mongodb", "database", "NoSQL" ], "likes" : 20, "comments" : [ { "user" : "user1", "message" : "My first comment", "dateCreated" : ISODate("2013-12-09T21:05:00Z">
, "like" : 0 } ] }

The findOne(>
technique

Aside from the find(>
technique, there is findOne(>
strategy, that profits just a single record
Syntax: >db.COLLECTIONNAME.findOne(>

Example:

Following model recovers the report with title MongoDB Overview.
> db.mycol.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 }

AND in MongoDB

To query records in light of the AND condition, you really want to utilize $and catchphrase. Following is the fundamental grammar of AND −

>db.mycol.find({ $and: [ {:}, { :} ] }>

Example Following model will show every one of the instructional exercises composed by 'instructional exercises 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 model, identical where condition will be ' where by = 'instructional exercises point' AND title = 'MongoDB Overview' '. You can pass quite a few key, esteem matches in track down statement.

OR in MongoDB

Syntax: To question records in view of the OR condition, you really want to utilize $or watchword. Following is the essential punctuation of OR −

>db.mycol.find(
   {
      $or: [
         {key1: value1}, {key2:value2}
      ]
   }
>
.pretty(>

Example Following example will show every one of the instructional exercises composed by 'instructional exercises 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" } >

Using AND and OR Together

Example: The accompanying model will show the archives that have likes more prominent than 10 and whose title is either 'MongoDB Overview' or by is 'instructional exercises point'. Comparable SQL where statement is 'where likes>10 AND (by = 'instructional exercises point' OR title = 'MongoDB Overview'>
'

>db.mycol.find({"likes": {$gt:10}, $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 inquiry records in view of the NOT condition, you want to utilize $not catchphrase. Following is the essential language structure of NOT

>db.COLLECTION_NAME.find(
	{
		$not: [
			{key1: value1}, {key2:value2}
		]
	}
>

Example Assume we have embedded 3 records in the assortment empDetails as displayed shown below

db.empDetails.insertMany(
	[
		{
			First_Name: "Radhika",
			Last_Name: "Sharma",
			Age: "26",
			e_mail: "radhika_sharma.123@gmail.com",
			phone: "9000012345"
		},
		{
			First_Name: "Rachel",
			Last_Name: "Christopher",
			Age: "27",
			e_mail: "Rachel_Christopher.123@gmail.com",
			phone: "9000054321"
		},
		{
			First_Name: "Fathima",
			Last_Name: "Sheik",
			Age: "24",
			e_mail: "Fathima_Sheik.123@gmail.com",
			phone: "9000054321"
		}
	]
>

Following example will recover the document(s>
whose first name isn't "Radhika" and last name isn't "Christopher"

> db.empDetails.find(
	{
		$nor:[
			40
			{"First_Name": "Radhika"},
			{"Last_Name": "Christopher"}
		]
	}
>
.pretty(>
{ "_id" : ObjectId("5dd631f270fb13eec3963bef">
, "First_Name" : "Fathima", "Last_Name" : "Sheik", "Age" : "24", "e_mail" : "Fathima_Sheik.123@gmail.com", "phone" : "9000054321" }

NOT in MongoDB

Syntax: To query archives in view of the NOT condition, you really want to utilize $not catchphrase following is the essential sentence structure of NOT

>db.COLLECTION_NAME.find(
	{
		$NOT: [
			{key1: value1}, {key2:value2}
		]
	}
>
.pretty(>

Example: Following model will recover the document(s>
whose age isn't more prominent than 25

> db.empDetails.find( { "Age": { $not: { $gt: "25" } } } >
{ "_id" : ObjectId("5dd6636870fb13eec3963bf7">
, "First_Name" : "Fathima", "Last_Name" : "Sheik", "Age" : "24", "e_mail" : "Fathima_Sheik.123@gmail.com", "phone" : "9000054321" }