- Trending Categories
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 1349 Articles for MongoDB
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
122 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, "insertedId" : ObjectId("5ea067144deddd72997713d9") } > db.demo658.insertOne({"FirstName":"Adam", "LastName":"Smith"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea0671c4deddd72997713da") } > db.demo658.insertOne({"FirstName":"John", "LastName":"Doe"}); 2020-04-22T21:17:46.072+0530 E QUERY [js] WriteError: E11000 duplicate key error collection: onlinecustomertracker.demo658 index: FirstName_1 dup key: { : "John" } : WriteError({ "index" : 0, "code" : 11000, ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
101 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" : ObjectId("5ea064b44deddd72997713d6") } > db.demo657.insertOne( ... { ... id: 1, ... Name: "John", ... DueDate: new ISODate("2020-04-22") ... } ...); { "acknowledged" : true, "insertedId" : ObjectId("5ea064b44deddd72997713d7") } > db.demo657.insertOne( ... { ... id: 1, ... Name: "Chris", ... DueDate: new ISODate("2020-04-22") ... ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
167 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" : ObjectId("5ea060264deddd72997713cf"), "Name" : "John" }Here is the query to push and slice in MongoDB−> db.demo656.update({Name:"John"}, {"$push":{"ListOfName": {"$each": ["John"], "$slice": -9}}}); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })Display all documents from a collection with the help of find() method −> db.demo656.find();This will produce the following output −{ ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
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 onlinecustomertracker −> use onlinecustomertracker switched to db onlinecustomertracker > db.dropDatabase(); { "dropped" : "onlinecustomertracker", "ok" : 1 }Following is the query to show all databases after deleting a database above −> show dbsThis will produce the following output −MyDB 0.000GB admin 0.000GB config 0.000GB local 0.000GB test 0.006GB
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
486 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" : true, "insertedId" : ObjectId("5ea050314deddd72997713ce") }Display all documents from a collection with the help of find() method −> db.demo655.find();This will produce the following output −{ "_id" : ObjectId("5ea050254deddd72997713cc"), "subject" : "MySQL" } { "_id" : ObjectId("5ea0502b4deddd72997713cd"), "subject" : "MongoDB" } { "_id" : ObjectId("5ea050314deddd72997713ce"), "subject" : "Java" }Following is ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
69 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" : ObjectId("5ea04b354deddd72997713c2") } > db.demo653.insertOne({subject:"C"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea04b384deddd72997713c3") } > db.demo653.insertOne({subject:"C++"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea04b3b4deddd72997713c4") }Display all documents from a collection with the help of find() method −> db.demo653.find();This will produce the following output −{ "_id" : ObjectId("5ea04b274deddd72997713c0"), "subject" : ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
148 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 ... }, { ... "ProductName" : "Product-2", ... "ProductQuantity" : 7, ... "ProductPrice" : 500 ... ... }] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e9f0730e3c3cd0dcff36a62") } > > db.demo652.insertOne({ ... value:5, ... ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
362 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") } > db.demo531.insertOne({CountryName:"UK", "Name":"David"});{ "acknowledged" : true, "insertedId" : ObjectId("5e8b2b6cef4dcbee04fbbbf3") } > db.demo531.insertOne({CountryName:"AUS", "Name":"chris"});{ "acknowledged" : true, "insertedId" : ObjectId("5e8b2b74ef4dcbee04fbbbf4") } > db.demo531.insertOne({CountryName:"US", "Name":"CHRIS"});{ "acknowledged" : true, "insertedId" : ObjectId("5e8b2badef4dcbee04fbbbf5") }Display all documents from a collection with the help of find() method −> db.demo531.find();This will ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
581 Views
If there are null values also, then implement ORDERBY using sort().Note − Since, starting in MongoDB v3.2, the $orderby operator deprecated in the mongo shell. Use cursor.sort() instead.Let us create a collection with documents −> db.demo530.insertOne({"Name":"Chris"});{ "acknowledged" : true, "insertedId" : ObjectId("5e8b2990ef4dcbee04fbbbec") } > db.demo530.insertOne({"Name":null});{ "acknowledged" : true, "insertedId" : ObjectId("5e8b2991ef4dcbee04fbbbed") } > db.demo530.insertOne({"Name":"David"});{ "acknowledged" : true, "insertedId" : ObjectId("5e8b2992ef4dcbee04fbbbee") } > db.demo530.insertOne({"Name":"Adam"});{ "acknowledged" : true, "insertedId" : ObjectId("5e8b2995ef4dcbee04fbbbef") } > db.demo530.insertOne({"Name":null});{ "acknowledged" : true, "insertedId" : ObjectId("5e8b2999ef4dcbee04fbbbf0") } > db.demo530.insertOne({"Name":"Carol"});{ "acknowledged" : true, "insertedId" : ObjectId("5e8b299eef4dcbee04fbbbf1") }Display ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
783 Views
To group by _id in MongoDB, use $group. Let us create a collection with documents −> db.demo529.insertOne({"Score":10});{ "acknowledged" : true, "insertedId" : ObjectId("5e8b1d5bef4dcbee04fbbbe4") } > db.demo529.insertOne({"Score":20});{ "acknowledged" : true, "insertedId" : ObjectId("5e8b1d5fef4dcbee04fbbbe5") } > db.demo529.insertOne({"Score":10});{ "acknowledged" : true, "insertedId" : ObjectId("5e8b1d61ef4dcbee04fbbbe6") } > db.demo529.insertOne({"Score":10});{ "acknowledged" : true, "insertedId" : ObjectId("5e8b1d62ef4dcbee04fbbbe7") }Display all documents from a collection with the help of find() method −> db.demo529.find();This will produce the following output −{ "_id" : ObjectId("5e8b1d5bef4dcbee04fbbbe4"), "Score" : 10 } { "_id" : ObjectId("5e8b1d5fef4dcbee04fbbbe5"), "Score" : 20 } { "_id" : ObjectId("5e8b1d61ef4dcbee04fbbbe6"), "Score" : 10 ... Read More