
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 6705 Articles for Database

518 Views
To implement indexes correctly with $elemMatch, you need to use the concept of explain(). Let us first create a collection with documents −> db.workingOfIndexesDemo.createIndex({"Information.StudentDetails.StudentName":1}, { sparse : true, background : true } ); { "createdCollectionAutomatically" : true, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 } > db.workingOfIndexesDemo.insertOne({"Information":{"StudentDetails":{"StudentName":"Chris"}}}); { "acknowledged" : true, "insertedId" : ObjectId("5e06f94825ddae1f53b621f7") } > db.workingOfIndexesDemo.insertOne({"Information":{"StudentDetails":{"StudentName":"David"}}}); { "acknowledged" : true, "insertedId" : ObjectId("5e06f94f25ddae1f53b621f8") } > db.workingOfIndexesDemo.insertOne({"Information":{"StudentDetails":{"StudentName":"Mike"}}}); { "acknowledged" : true, "insertedId" : ObjectId("5e06f95325ddae1f53b621f9") }Following is the query to display all documents from a collection with the help ... Read More

243 Views
For this, you can use $cond along with $anyElementTrue. NULL values (absence of a field) would evaluate to FALSE. With that, an empty array also returns FALSE with $ anyElementTrue.Let us first create a collection with documents −> db.presenceDemo.insertOne({"StudentName":null}); { "acknowledged" : true, "insertedId" : ObjectId("5e06f70c25ddae1f53b621f3") } > db.presenceDemo.insertOne({"StudentName":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5e06f71425ddae1f53b621f4") } > db.presenceDemo.insertOne({"StudentName":null}); { "acknowledged" : true, "insertedId" : ObjectId("5e06f71825ddae1f53b621f5") } > db.presenceDemo.insertOne({"StudentName":"David"}); { "acknowledged" : true, "insertedId" : ObjectId("5e06f71e25ddae1f53b621f6") }Following is the query to display all documents from a collection with the help of find() ... Read More

114 Views
For this, use double underscore( __) in find(). Let us first create a collection with documents −> db.nestedDemo.insertOne({"Information":{"__StudentName":"John Smith"}}); { "acknowledged" : true, "insertedId" : ObjectId("5e06f39125ddae1f53b621f0") } > db.nestedDemo.insertOne({"Information":{"__StudentName":"John Doe"}}); { "acknowledged" : true, "insertedId" : ObjectId("5e06f39e25ddae1f53b621f1") } > db.nestedDemo.insertOne({"Information":{"__StudentName":"Chris Brown"}}); { "acknowledged" : true, "insertedId" : ObjectId("5e06f3a625ddae1f53b621f2") }Following is the query to display all documents from a collection with the help of find() method −> db.nestedDemo.find().pretty();This will produce the following output −{ "_id" : ObjectId("5e06f39125ddae1f53b621f0"), "Information" : { "__StudentName" : "John Smith" } } { "_id" ... Read More

219 Views
For this, you can use aggregate(). Let us first create a collection with documents −> db.countOccurrencesDemo.insertOne({"ListOfValues":[65, 87, 89, 65, 67, 87, 87, 87]}); { "acknowledged" : true, "insertedId" : ObjectId("5e06ef9325ddae1f53b621eb") } > db.countOccurrencesDemo.insertOne({"ListOfValues":[102, 65, 87, 65, 89, 65, 89, 65, 89, 65]}); { "acknowledged" : true, "insertedId" : ObjectId("5e06efaa25ddae1f53b621ec") }Following is the query to display all documents from a collection with the help of find() method −> db.countOccurrencesDemo.find();This will produce the following output −{ "_id" : ObjectId("5e06ef9325ddae1f53b621eb"), "ListOfValues" : [ 65, 87, 89, 65, 67, 87, 87, 87 ] } { "_id" : ObjectId("5e06efaa25ddae1f53b621ec"), "ListOfValues" : ... Read More

205 Views
To update only a single document in a collection. use updateOne(). Let us first create a collection with documents −> db.updateOneDemo.insertOne({"StudentId":1, "StudentFirstName":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5e06ed3725ddae1f53b621e8") } > db.updateOneDemo.insertOne({"StudentId":2, "StudentFirstName":"David"}); { "acknowledged" : true, "insertedId" : ObjectId("5e06ed3825ddae1f53b621e9") } > db.updateOneDemo.insertOne({"StudentId":1, "StudentFirstName":"Mike"}); { "acknowledged" : true, "insertedId" : ObjectId("5e06ed3825ddae1f53b621ea") }Following is the query to display all documents from a collection with the help of find() method −> db.updateOneDemo.find();This will produce the following output −{ "_id" : ObjectId("5e06ed3725ddae1f53b621e8"), "StudentId" : 1, "StudentFirstName" : "Chris" } { "_id" : ObjectId("5e06ed3825ddae1f53b621e9"), "StudentId" : 2, "StudentFirstName" ... Read More

1K+ Views
To fetch specific field values, use $in operator. The $in selects the documents where the value of a field equals any value in the specified array.Let us first create a collection with documents −> db.indexesDemo.createIndex({"StudentFirstName":1}); { "createdCollectionAutomatically" : true, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 } > db.indexesDemo.insertOne({"StudentFirstName":"John", "StudentLastName":"Smith"}); { "acknowledged" : true, "insertedId" : ObjectId("5e06de4d25ddae1f53b621dd") } > db.indexesDemo.insertOne({"StudentFirstName":"Chris", "StudentLastName":"Brown"}); { "acknowledged" : true, "insertedId" : ObjectId("5e06de5825ddae1f53b621de") } > db.indexesDemo.insertOne({"StudentFirstName":"John", "StudentLastName":"Doe"}); { "acknowledged" : true, "insertedId" : ObjectId("5e06de6725ddae1f53b621df") } > db.indexesDemo.insertOne({"StudentFirstName":"David", "StudentLastName":"Miller"}); { "acknowledged" : true, ... Read More

340 Views
To append an entry in MongoDB, use $push. Let us create a collection with documents −> db.demo253.insertOne( ... { ... _id: "101", ... isActive: false, ... details: [ ... { ... Name: "Chris", ... }, ... { ... CountryName:"US" ... } ... ] ... } ...); { "acknowledged" : true, "insertedId" : "101" }Display all documents from a collection with the help of find() ... Read More

684 Views
To sort array, use $sort. For projection, use $project in MongoBD aggregate(). Let us create a collection with documents −> db.demo252.insertOne( ... {"Values" : [ { "v1" : 20, "v2" :30 }, { "v1" : 20, "v2" : 20 }, { "v1" : 10, "v2" : 7 } ] } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e46c2761627c0c63e7dba78") }Display all documents from a collection with the help of find() method −> db.demo252.find();This will produce the following output −{ "_id" : ObjectId("5e46c2761627c0c63e7dba78"), "Values" : [ { "v1" : 20, "v2" : 30 }, { "v1" : 20, ... Read More

1K+ Views
For min/max in MongoDB, use $min and $max. Let us create a collection with documents −> db.demo251.insertOne({"Marks":78}); { "acknowledged" : true, "insertedId" : ObjectId("5e46c0001627c0c63e7dba74") } > db.demo251.insertOne({"Marks":87}); { "acknowledged" : true, "insertedId" : ObjectId("5e46c0031627c0c63e7dba75") } > db.demo251.insertOne({"Marks":56}); { "acknowledged" : true, "insertedId" : ObjectId("5e46c0061627c0c63e7dba76") } > db.demo251.insertOne({"Marks":76}); { "acknowledged" : true, "insertedId" : ObjectId("5e46c00c1627c0c63e7dba77") }Display all documents from a collection with the help of find() method −> db.demo251.find();This will produce the following output −{ "_id" : ObjectId("5e46c0001627c0c63e7dba74"), "Marks" : 78 } { "_id" : ObjectId("5e46c0031627c0c63e7dba75"), "Marks" : 87 } { "_id" : ... Read More

111 Views
For this, use bsonsize() in MongoDB. Let us create a collection with documents −> db.demo250.insertOne({"Name":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5e46bd501627c0c63e7dba70") } > db.demo250.insertOne({"Name":"Bob"}); { "acknowledged" : true, "insertedId" : ObjectId("5e46bd531627c0c63e7dba71") } > db.demo250.insertOne({"Name":"David"}); { "acknowledged" : true, "insertedId" : ObjectId("5e46bd561627c0c63e7dba72") } > db.demo250.insertOne({"Name":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5e46bd5b1627c0c63e7dba73") }Display all documents from a collection with the help of find() method −> db.demo250.find();This will produce the following output −{ "_id" : ObjectId("5e46bd501627c0c63e7dba70"), "Name" : "Chris" } { "_id" : ObjectId("5e46bd531627c0c63e7dba71"), "Name" : "Bob" } { "_id" : ObjectId("5e46bd561627c0c63e7dba72"), "Name" ... Read More