The $natural returns the documents in natural order. To reverse the items, use $natural:-1. Let us create a collection with documents −> db.demo710.insertOne({id:101, Name:"Robert"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea83a855d33e20ed1097b7a") } > db.demo710.insertOne({id:102, Name:"Carol"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea83a8d5d33e20ed1097b7b") } > db.demo710.insertOne({id:103, Name:"Mike"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea83a935d33e20ed1097b7c") } > db.demo710.insertOne({id:104, Name:"Sam"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea83a9b5d33e20ed1097b7d") }Display all documents from a collection with the help of find() method −> db.demo710.find();This will produce the following output −{ "_id" : ObjectId("5ea83a855d33e20ed1097b7a"), "id" : 101, "Name" : ... Read More
To get the sorted list of marks, use $sort. Use $limit: 2 to display only two such documents with least marks. Let us create a collection with documents −> db.demo709.insertOne({Name:"John", "Marks":75}); { "acknowledged" : true, "insertedId" : ObjectId("5ea839005d33e20ed1097b76") } > db.demo709.insertOne({Name:"Chris", "Marks":45}); { "acknowledged" : true, "insertedId" : ObjectId("5ea839075d33e20ed1097b77") } > db.demo709.insertOne({Name:"David", "Marks":54}); { "acknowledged" : true, "insertedId" : ObjectId("5ea839125d33e20ed1097b78") } > db.demo709.insertOne({Name:"Bob", "Marks":69}); { "acknowledged" : true, "insertedId" : ObjectId("5ea839295d33e20ed1097b79") }Display all documents from a collection with the help of find() method −> db.demo709.find();This will produce the following output −{ "_id" ... Read More
To find MongoDB based on a condition, use find() and set the condition. Let us create a collection with documents −> db.demo708.insertOne({"Name":"John", Marks:54}); { "acknowledged" : true, "insertedId" : ObjectId("5ea702e4d346dcb074dc6f33") } > db.demo708.insertOne({"Name":"Chris", Marks:35}); { "acknowledged" : true, "insertedId" : ObjectId("5ea702e6d346dcb074dc6f34") } > db.demo708.insertOne({"Name":"David", Marks:45}); { "acknowledged" : true, "insertedId" : ObjectId("5ea702ebd346dcb074dc6f35") } > db.demo708.insertOne({"Name":"Bob", Marks:40}); { "acknowledged" : true, "insertedId" : ObjectId("5ea702fad346dcb074dc6f36") }Display all documents from a collection with the help of find() method −> db.demo708.find();This will produce the following output −{ "_id" : ObjectId("5ea702e4d346dcb074dc6f33"), "Name" : "John", "Marks" : 54 ... Read More
Let us create a collection with documents −> db.demo707.insertOne( ... { ... id:101, ... "serverInformation": ... [ ... { ... "IP":"192.56.34.3", ... "Status":"Active" ... }, ... { ... "IP":"192.56.36.4", ... "Status":"Inactive" ... } ... ] ... } ... ); { "acknowledged" : true, ... Read More
Following is the syntax showing document and subdocument −db.yourCollectionName.insertOne( { yourFiledName:yourValue, yourFieldName : [ { yourFiledName1, yourFiledName2, . . . N } ] } );Let us see an example create a collection with documents −> db.demo706.insertOne( ... { ... PortalName: "GameApplication", ... Read More
To query embedded documents in MongoDB, use aggregate(). Let us create a collection with documents −> db.demo705.insertOne( ... { ... _id:101, ... "Information": ... [ ... { ... "StudentName":"Chris", ... "StudentAge":21 ... }, ... { ... "StudentName":"David", ... "StudentAge":23 ... }, ... { ... ... Read More
To get unique values and ignore duplicates, use distinct() in MongoDB. The distinct() finds the distinct values for a specified field across a single collection and returns the results in an array.Let us create a collection with documents −> db.demo704.insertOne({"LanguageCode":"hi"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea6ee18551299a9f98c93bd") } > db.demo704.insertOne({"LanguageCode":"en"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea6ee1e551299a9f98c93be") } > db.demo704.insertOne({"LanguageCode":"hi"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea6ee20551299a9f98c93bf") } > db.demo704.insertOne({"LanguageCode":"eo"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea6ee2c551299a9f98c93c0") } > db.demo704.insertOne({"LanguageCode":"eu"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea6ee2f551299a9f98c93c1") } > db.demo704.insertOne({"LanguageCode":"fo"}); ... Read More
To count the number of array items in a document, use $size in MongoDB. Let us create a collection with documents −> db.demo703.insertOne({"ListOfSubject":["MySQL", "MongoDB"]}); { "acknowledged" : true, "insertedId" : ObjectId("5ea6ebaf551299a9f98c93b4") } > db.demo703.insertOne({"ListOfSubject":["Java"]}); { "acknowledged" : true, "insertedId" : ObjectId("5ea6ebb5551299a9f98c93b5") } > db.demo703.insertOne({"ListOfSubject":["C", "C++", "Python"]}); { "acknowledged" : true, "insertedId" : ObjectId("5ea6ebbf551299a9f98c93b6") }Display all documents from a collection with the help of find() method −> db.demo703.find();This will produce the following output −{ "_id" : ObjectId("5ea6ebaf551299a9f98c93b4"), "ListOfSubject" : [ "MySQL", "MongoDB" ] } { "_id" : ObjectId("5ea6ebb5551299a9f98c93b5"), "ListOfSubject" : [ "Java" ] } { ... Read More
To create index, use createIndex() in MongoDB. Let us create a collection with documents −> db.demo702.createIndex({"details.id":1}); { "createdCollectionAutomatically" : true, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 } > db.demo702.insertOne({ ... "details" : [ ... { ... id:101, ... studentInfo:{ ... "StudentName" : "Chris", ... "StudentAge" : 23, ... } ... }, ... { ... ... id: 102, ... studentInfo:{ ... "StudentName" : "Robert", ... "StudentAge" : 20, ... } ... } ... ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5ea6ea3b551299a9f98c93b3") }Display all documents from a collection with the help of find() method −> db.demo702.find().pretty();This will produce the following output −{ "_id" : ObjectId("5ea6ea3b551299a9f98c93b3"), "details" : [ { "id" : 101, "studentInfo" : { "StudentName" : "Chris", "StudentAge" : 23 } }, { "id" : 102, "studentInfo" : { "StudentName" : "Robert", "StudentAge" : 20 } } ] }
You can use $elemMatch. The $elemMatch operator matches documents that contain an array field with at least one element that matches all the specified query criteria.Let us create a collection with documents −> db.demo701.insertOne({"ListOfValues":[100, 200, 300]}); { "acknowledged" : true, "insertedId" : ObjectId("5ea6e8cf551299a9f98c93b0") } > db.demo701.insertOne({"ListOfValues":[500, 700, 1000]}); { "acknowledged" : true, "insertedId" : ObjectId("5ea6e8d8551299a9f98c93b1") } > db.demo701.insertOne({"ListOfValues":[300, 350, 450]}); { "acknowledged" : true, "insertedId" : ObjectId("5ea6e8e1551299a9f98c93b2") }Display all documents from a collection with the help of find() method −> db.demo701.find();This will produce the following output −{ "_id" : ObjectId("5ea6e8cf551299a9f98c93b0"), "ListOfValues" : [ 100, ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP