WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

MongoDB - Query Document


MongoDB - Query Document how to question report from MongoDB series.

The locate() Method

To query facts from MongoDB collection, you want to use MongoDB's discover() approach.

Syntax

There are following primary syntax of discover() method
>db.COLLECTION_NAME.Discover()
locate() approach will show all of the documents in a non-structured manner.

The pretty() Method

To display the consequences in a formatted way, you can use quite() technique.

Syntax

>db.Mycol.Locate().Quite()

Example

>db.mycol.locate().quite()

   "_id": ObjectId(7df78ad8902c),
   "name": "MongoDB Overview", 
   "description": "MongoDB is no sq. Database",
   "by means of": "welookups",
   "url": "http://www.Welookups.Com",
   "tags": ["mongodb", "database", "NoSQL"],
   "likes": "one hundred"

>

findOne() method, that returns handiest one record.

RDBMS Where Clause Equivalents in MongoDB

To query the file on the idea of some situation, you may use following operations.
Operation Syntax Example RDBMS Equivalent
Equality <key>:<value> db.Mycol.Find("through":"welookups").Pretty() where via = 'welookups'
Less Than <key>:$lt:<cost> db.Mycol.Discover("likes":$lt:50).Quite() wherein likes < 50
Less Than Equals <key>:$lte:<price> db.Mycol.Discover("likes":$lte:50).Quite() where likes <= 50
Greater Than <key>:$gt:<price> db.Mycol.Find("likes":$gt:50).Quite() wherein likes > 50
Greater Than Equals <key>:$gte:<cost> db.Mycol.Find("likes":$gte:50).Pretty() where likes >= 50
Not Equals <key>:$ne:<value> db.Mycol.Find("likes":$ne:50).Quite() wherein likes != 50

OR in MongoDB

Syntax

To query files based totally on the OR condition, you want to use $or keyword. Following is the fundamental syntax of OR
>db.mycol.locate(
   
      $or: [
         key1: value1, key2:value2
      ]
   
).quite()

Example

There are Following instance will display all of the tutorials written through 'welookups' or whose name is 'MongoDB Overview'.
>db.mycol.find($or:["via":"welookups","name": "MongoDB Overview"]).quite()

   "_id": ObjectId(7df78ad8902c),
   "name": "MongoDB Overview", 
   "description": "MongoDB is no sq. Database",
   "with the aid of": "welookups",
   "url": "http://www.Welookups.Com",
   "tags": ["mongodb", "database", "NoSQL"],
   "likes": "one hundred"

>

AND in MongoDB

Syntax

In the find() approach, if you bypass a couple of keys via isolating them by ',' then MongoDB treats it as AND condition. Following is the basic syntax of AND
>db.Mycol.Locate(
   
      $and: [
         key1: value1, key2:value2
      ]
   
).Pretty()

Example

There are Following instance will show all the tutorials written with the aid of 'welookups' and whose identify is 'MongoDB Overview'.
>db.mycol.find($and:["by using":"welookups","name": "MongoDB Overview"]).quite() 
   "_id": ObjectId(7df78ad8902c),
   "name": "MongoDB Overview", 
   "description": "MongoDB is no square database",
   "by way of": "welookups",
   "url": "http://www.Welookups.Com",
   "tags": ["mongodb", "database", "NoSQL"],
   "likes": "a hundred"
For the above given instance, equal wherein clause may be ' in which by means of = 'welookups' AND name = 'MongoDB Overview' '. You can skip any wide variety of key, price pairs in locate clause.



Welookups is optimized for learning.Copy right 2018 kumar aditya singh .
All Right Reserved and you agree to have read and accepted our term and condition.
All Rights Reserved.