- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 1359 Articles for MongoDB
232 Views
To order a list, use sort(). Let us create a collection with documents −> db.demo581.insertOne({"Name":"Chris", "Score":56});{ "acknowledged" : true, "insertedId" : ObjectId("5e91cbbbfd2d90c177b5bcb6") } > db.demo581.insertOne({"Name":"Bob", "Score":240});{ "acknowledged" : true, "insertedId" : ObjectId("5e91cbbbfd2d90c177b5bcb7") } > db.demo581.insertOne({"Name":"David", "Score":150});{ "acknowledged" : true, "insertedId" : ObjectId("5e91cbbcfd2d90c177b5bcb8") }Display all documents from a collection with the help of find() method −> db.demo581.find();This will produce the following output −{ "_id" : ObjectId("5e91cbbbfd2d90c177b5bcb6"), "Name" : "Chris", "Score" : 56 } { "_id" : ObjectId("5e91cbbbfd2d90c177b5bcb7"), "Name" : "Bob", "Score" : 240 } { "_id" : ObjectId("5e91cbbcfd2d90c177b5bcb8"), "Name" : "David", "Score" : 150 }Following is the query ... Read More
317 Views
To calculate the sum of unique properties in different collection elements, use $cond along with $group. This gives the resultant price.Let us create a collection with documents −> db.demo580.insertOne( ... { ... "Name":"John", ... "Id1":"110", ... "Id2":"111", ... "Price":10.5 ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e918cebfd2d90c177b5bcae") } > > db.demo580.insertOne( ... { ... "Name":"John", ... "Id1":"111", ... "Id2":"", ... "Price":9.5 ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e918cecfd2d90c177b5bcaf") }Display all documents ... Read More
250 Views
To slice only one element of array, use $slice in MongoDB. Let us create a collection with documents −> db.demo579.insertOne( ... { ... "_id" : 101, ... "details" : { "FirstName" : "John" }, ... "Marks" : [ 56,78,90,34,45,74 ] ... } ... ); { "acknowledged" : true, "insertedId" : 101 }Display all documents from a collection with the help of find() method −> db.demo579.find().pretty();This will produce the following output −{ "_id" : 101, "details" : { "FirstName" : "John" }, "Marks" : [ 56, 78, 90, 34, 45, 74 ] }Following is the query to slice only one the element of the array −> db.demo579.find({},{Marks : {$slice : 1} ,"details":0,"_id":0})This will produce the following output −{ "Marks" : [ 56 ] }
161 Views
To sort MongoDB collection by Array value, use aggregate() along with $sort. Let us create a collection with documents −> db.demo577.insertOne( ... { ... ... "student": { ... "details": [ ... { ... Name:"Chris", ... Score:45 ... }, ... { ... Name:"Bob", ... Score:33 ... ... Read More
262 Views
Let us create a collection with documents −> db.demo576.insertOne({id:101, Name:"Chris", Marks:45}){ "acknowledged" : true, "insertedId" : ObjectId("5e916c3b581e9acd78b427fa") } > db.demo576.insertOne({id:101, Name:"John", Marks:55}){ "acknowledged" : true, "insertedId" : ObjectId("5e916c43581e9acd78b427fb") } > db.demo576.insertOne({id:101, Name:"John", Marks:65}){ "acknowledged" : true, "insertedId" : ObjectId("5e916c47581e9acd78b427fc") } > db.demo576.insertOne({id:102, Name:"David", Marks:37}){ "acknowledged" : true, "insertedId" : ObjectId("5e916c55581e9acd78b427fd") } > db.demo576.insertOne({id:102, Name:"David", Marks:75}){ "acknowledged" : true, "insertedId" : ObjectId("5e916c5e581e9acd78b427fe") }Display all documents from a collection with the help of find() method −> db.demo576.find();This will produce the following output −{ "_id" : ObjectId("5e916c3b581e9acd78b427fa"), "id" : 101, "Name" : "Chris", "Marks" : 45 } { ... Read More
456 Views
Let us create a collection with documents −> db.demo575.insertOne({id:101, Information:{Name:"Chris", Age:21}});{ "acknowledged" : true, "insertedId" : ObjectId("5e916a55581e9acd78b427f7") } > db.demo575.insertOne({id:102, Information:{Name:"David", Age:20}});{ "acknowledged" : true, "insertedId" : ObjectId("5e916a5f581e9acd78b427f8") } > db.demo575.insertOne({id:101, Information:{Name:"Bob", Age:23}});{ "acknowledged" : true, "insertedId" : ObjectId("5e916a67581e9acd78b427f9") }Display all documents from a collection with the help of find() method −> db.demo575.find();This will produce the following output −{ "_id" : ObjectId("5e916a55581e9acd78b427f7"), "id" : 101, "Information" : { "Name" : "Chris", "Age" : 21 } } { "_id" : ObjectId("5e916a5f581e9acd78b427f8"), "id" : 102, "Information" : { "Name" : "David", "Age" : 20 } } { "_id" ... Read More
421 Views
To match all the values in MongoDB, use $match along with $and in Aggregation. Let us create a collection with documents −> db.demo574.insertOne( ... { ... "details1": { ... "details2": { ... "dueDate": new ISODate("2020-01-10"), ... "Name": "Chris", ... ... "UserInformation": { ... "Name": "John", ... "Marks": 78 ... }, ... ... Read More
108 Views
For this, use $push along with update. Let us create a collection with documents −> db.demo573.insertOne( ... { ... '_id' :101, ... 'SearchInformation' : [ ... { ... 'Site' : 'Facebook.com', ... 'NumberOfHits' : 100 ... }, ... { ... 'Site' : 'Twitter.com', ... 'NumberOfHits' : 300 ... } ... ... Read More
2K+ Views
For case, insensitive search, use regex in find() method. Following is the syntax −db.demo572.find({"yourFieldName" : { '$regex':/^yourValue$/i}});To understand the above syntax, let us create a collection with documents −> db.demo572.insertOne({"CountryName":"US"});{ "acknowledged" : true, "insertedId" : ObjectId("5e915f0e581e9acd78b427f1") } > db.demo572.insertOne({"CountryName":"UK"});{ "acknowledged" : true, "insertedId" : ObjectId("5e915f17581e9acd78b427f2") } > db.demo572.insertOne({"CountryName":"Us"});{ "acknowledged" : true, "insertedId" : ObjectId("5e915f1b581e9acd78b427f3") } > db.demo572.insertOne({"CountryName":"AUS"});{ "acknowledged" : true, "insertedId" : ObjectId("5e915f20581e9acd78b427f4") } > db.demo572.insertOne({"CountryName":"us"});{ "acknowledged" : true, "insertedId" : ObjectId("5e915f25581e9acd78b427f5") }Display all documents from a collection with the help of find() method −> db.demo572.find();This will produce the following output −{ "_id" : ObjectId("5e915f0e581e9acd78b427f1"), ... Read More
131 Views
To find values above a specific value, the following is the syntax using $gte in MongoDB −db.yourCollectionName.find({yourFieldName:{$gte:yourValue}});Let us create a collection with documents −> db.demo571.insertOne({"Price":140});{ "acknowledged" : true, "insertedId" : ObjectId("5e909b3439cfeaaf0b97b587") } > db.demo571.insertOne({"Price":100});{ "acknowledged" : true, "insertedId" : ObjectId("5e909b3639cfeaaf0b97b588") } > db.demo571.insertOne({"Price":110});{ "acknowledged" : true, "insertedId" : ObjectId("5e909b3839cfeaaf0b97b589") } > db.demo571.insertOne({"Price":240});{ "acknowledged" : true, "insertedId" : ObjectId("5e909b3c39cfeaaf0b97b58a") }Display all documents from a collection with the help of find() method −> db.demo571.find();This will produce the following output −{ "_id" : ObjectId("5e909b3439cfeaaf0b97b587"), "Price" : 140 } { "_id" : ObjectId("5e909b3639cfeaaf0b97b588"), "Price" : 100 } { "_id" : ... Read More
To Continue Learning Please Login
Login with Google