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
AmitDiwan has Published 10740 Articles
AmitDiwan
468 Views
For faster search, create index. For this, use createIndex(). Let us create a collection with documents −> db.demo661.createIndex({ListOfName:1}); { "createdCollectionAutomatically" : true, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 } > db.demo661.insertOne({_id:1, ListOfName:["John", "Robert", "David"]}); { "acknowledged" : true, "insertedId" : 1 } > ... Read More
AmitDiwan
417 Views
You can use $group. Let us create a collection with documents −> db.demo659.insertOne({Name:"Chris", CountryName:"US", "Marks":50}); { "acknowledged" : true, "insertedId" : ObjectId("5ea1a50724113ea5458c7cf9") } > db.demo659.insertOne({Name:"David", CountryName:"US", "Marks":60}); { "acknowledged" : true, "insertedId" : ObjectId("5ea1a50724113ea5458c7cfa") } > db.demo659.insertOne({Name:"Mike", CountryName:"US", "Marks":55}); { "acknowledged" : true, "insertedId" ... Read More
AmitDiwan
227 Views
For unique index, set unique − true while creating an index. Let us create a collection with documents −> db.demo658.createIndex({FirstName:1}, {unique:true, sparse:true}); { "createdCollectionAutomatically" : true, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 } > > db.demo658.insertOne({"FirstName":"John", "LastName":"Smith"}); { "acknowledged" : true, ... Read More
AmitDiwan
214 Views
To group by dates, use $group in MongoDB aggregate. Let us create a collection with documents −> db.demo657.insertOne({ ... id: 1, ... Name: "Chris", ... DueDate: new ISODate("2020-04-22") ... } ... ); { "acknowledged" : true, "insertedId" : ... Read More
AmitDiwan
264 Views
To push and slice in MongoDB, use $push and $slice. Let us create a collection with documents −> db.demo656.insertOne({Name:"John"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea060264deddd72997713cf") }Display all documents from a collection with the help of find() method −> db.demo656.find();This will produce the following output −{ "_id" : ... Read More
AmitDiwan
2K+ Views
To clear, use dropDatabase. Following is the syntax −use yourDatabaseName; db.dropDatabase();To clear a MongoDB database, first show all the databases −> show dbsThis will produce the following output −MyDB 0.000GB admin 0.000GB config 0.000GB local 0.000GB onlinecustomertracker 0.000GB test 0.006GBNow, let us delete the database ... Read More
AmitDiwan
673 Views
The findOne() returns one document that satisfies the specified query criteria on the collection. Let us create a collection with documents −> db.demo655.insertOne({subject:"MySQL"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea050254deddd72997713cc") } > db.demo655.insertOne({subject:"MongoDB"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea0502b4deddd72997713cd") } > db.demo655.insertOne({subject:"Java"}); { "acknowledged" ... Read More
AmitDiwan
119 Views
Yes, using $in is faster. Let us see an example and create a collection with documents −> db.demo653.insertOne({subject:"MySQL"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea04b274deddd72997713c0") } > db.demo653.insertOne({subject:"MongoDB"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea04b304deddd72997713c1") } > db.demo653.insertOne({subject:"Java"}); { "acknowledged" : true, "insertedId" : ... Read More
AmitDiwan
244 Views
Use $sort in MongoDB aggregation. Let us create a collection with documents −> db.demo652.insertOne({ ... value:10, ... "details" : [{ ... "ProductName" : "Product-1", ... "ProductQuantity" : 8, ... "ProductPrice" : 500 ... }, { ... ... Read More
AmitDiwan
482 Views
To avoid performance issues in MongoDB, use the concept of index. Let us create a collection with documents −> db.demo531.createIndex({"CountryName":"text", "Name":"text"});{ "createdCollectionAutomatically" : true, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 } > db.demo531.insertOne({CountryName:"US", "Name":"Chris"});{ "acknowledged" : true, "insertedId" : ObjectId("5e8b2b60ef4dcbee04fbbbf2") } ... Read More