- 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)
2K+ Views
The $unwind in MongoDB deconstructs an array field from the input documents to output a document for each element.$group is used to group input documents by the specified _id expression and for each distinct grouping, outputs a document.$project is used to pass along the documents with the requested fields to the next stage in the pipeline.Let us create a collection with documents −> db.demo238.insertOne( ... { ... ... "EmailId" : "John@gmail.com", ... "details" : [ ... { ... "Name" : "Bob", ... ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
242 Views
Set the field you don’t want to include as 0. This would display rest of the values while using find(). Let us first create a collection with documents −> db.demo237.insertOne({ ... _id:101, ... Product: { ... description1: {id:1001 }, ... description2: {Name:"Product-1" }, ... description3: {Price:550 } ... } ...} ...); { "acknowledged" : true, "insertedId" : 101 }Display all documents from a collection with the help of find() method −> db.demo237.find().pretty();This will produce the following output −{ "_id" : 101, "Product" : { "description1" : ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
175 Views
To sort, use $sort in MongoDB. Let us create a collection with documents −> db.demo236.insertOne({"details":{"Name":"Chris", "Age":21}}); { "acknowledged" : true, "insertedId" : ObjectId("5e419015f4cebbeaebec514c") } > db.demo236.insertOne({"details":{"Name":"David", "Age":23}}); { "acknowledged" : true, "insertedId" : ObjectId("5e41901cf4cebbeaebec514d") } > db.demo236.insertOne({"details":{"Name":"Bob", "Age":24}}); { "acknowledged" : true, "insertedId" : ObjectId("5e419023f4cebbeaebec514e") }Display all documents from a collection with the help of find() method −> db.demo236.find();This will produce the following output −{ "_id" : ObjectId("5e419015f4cebbeaebec514c"), "details" : { "Name" : "Chris", "Age" : 21 } } { "_id" : ObjectId("5e41901cf4cebbeaebec514d"), "details" : { "Name" : "David", "Age" : 23 } } ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
132 Views
To get inner value of JSON data, use find() along with dot(.) notation. Let us create a collection with documents −> db.demo235.insertOne( ... { ... "id":101, ... "details":[ ... { ... "Name":"Chris Brown", ... "Age":21 ... }, ... { ... "Name":"David Miller", ... "Age":24 ... } ... ], ... "otherdetails":[ ... ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
466 Views
To check for existence, use $exists. Let us create a collection with documents − > db.demo234.insertOne({"FirstName":"Chris", "LastName":"Brown", "Age":24}); { "acknowledged" : true, "insertedId" : ObjectId("5e418a50f4cebbeaebec5148") } > db.demo234.insertOne({"FirstName":"David", "LastName":"Miller"}); { "acknowledged" : true, "insertedId" : ObjectId("5e418a5ff4cebbeaebec5149") } > db.demo234.insertOne({"FirstName":"John", "LastName":"Smith", Age:34}); { "acknowledged" : true, "insertedId" : ObjectId("5e418a70f4cebbeaebec514a") }Display all documents from a collection with the help of find() method −> db.demo234.find();This will produce the following output −{ "_id" : ObjectId("5e418a50f4cebbeaebec5148"), "FirstName" : "Chris", "LastName" : "Brown", "Age" : 24 } { "_id" : ObjectId("5e418a5ff4cebbeaebec5149"), "FirstName" : "David", "LastName" : "Miller" } { "_id" : ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
359 Views
To limit the amount of characters returned from a field, use $substr in MongoDB. Let us create a collection with documents −> db.demo233.insertOne({"Paragraph":"My Name is John Smith.I am learning MongoDB database"}); { "acknowledged" : true, "insertedId" : ObjectId("5e41877df4cebbeaebec5146") } > db.demo233.insertOne({"Paragraph":"David Miller is a good student and learning Spring and Hibernate Framework."}); { "acknowledged" : true, "insertedId" : ObjectId("5e4187d7f4cebbeaebec5147") }Display all documents from a collection with the help of find() method −> db.demo233.find().pretty();This will produce the following output −{ "_id" : ObjectId("5e41877df4cebbeaebec5146"), "Paragraph" : "My Name is John Smith.I am learning MongoDB database" } ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
110 Views
Let us first create a collection with documents −> db.demo232.insertOne({_id:101, Name:"Chris"}); { "acknowledged" : true, "insertedId" : 101 } > db.demo232.insertOne({_id:102, Name:"Bob"}); { "acknowledged" : true, "insertedId" : 102 } > db.demo232.insertOne({_id:103, Name:"Bob"}); { "acknowledged" : true, "insertedId" : 103 } > db.demo232.insertOne({_id:104, Name:"David"}); { "acknowledged" : true, "insertedId" : 104 } > db.demo232.insertOne({_id:105, Name:"Chris"}); { "acknowledged" : true, "insertedId" : 105 }Display all documents from a collection with the help of find() method −> db.demo232.find();This will produce the following output −{ "_id" : 101, "Name" : "Chris" } { "_id" : 102, "Name" : "Bob" } { "_id" : ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
748 Views
To sum the array size fields, use $sum along with $size. Let us create a collection with documents −> db.demo231.insertOne({"Subjects":["MongoDB", "MySQL", "SQL Server"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e3fc73ff4cebbeaebec5143") } > db.demo231.insertOne({"Subjects":["Java", "C", "C++"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e3fc757f4cebbeaebec5144") } > db.demo231.insertOne({"Subjects":["Python", "Spring"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e3fc762f4cebbeaebec5145") }Display all documents from a collection with the help of find() method −> db.demo231.find();This will produce the following output −{ "_id" : ObjectId("5e3fc73ff4cebbeaebec5143"), "Subjects" : [ "MongoDB", "MySQL", "SQL Server" ] } { "_id" : ObjectId("5e3fc757f4cebbeaebec5144"), "Subjects" : [ "Java", "C", ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
283 Views
The findOne() in MongoDB returns only a single document. Let us create a collection with documents −> db.demo230.insertOne({"FirstName":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5e3fc4d2f4cebbeaebec513e") } > db.demo230.insertOne({"FirstName":"Bob"}); { "acknowledged" : true, "insertedId" : ObjectId("5e3fc4d5f4cebbeaebec513f") } > db.demo230.insertOne({"FirstName":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5e3fc4dbf4cebbeaebec5140") } > db.demo230.insertOne({"FirstName":"David"}); { "acknowledged" : true, "insertedId" : ObjectId("5e3fc4dff4cebbeaebec5141") } > db.demo230.insertOne({"FirstName":"David"}); { "acknowledged" : true, "insertedId" : ObjectId("5e3fc4e1f4cebbeaebec5142") }Display all documents from a collection with the help of find() method −> db.demo230.find();This will produce the following output −{ "_id" : ObjectId("5e3fc4d2f4cebbeaebec513e"), "FirstName" : ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
132 Views
For sparse index, use sparse:true. Following is the query to create an index −> db.demo229.ensureIndex({"ClientName":1}, {unique: true}); { "createdCollectionAutomatically" : true, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 }Following is the query to display indexes −> db.demo229.getIndexes();This will produce the following output −[ { "v" : 2, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "test.demo229" }, { "v" : 2, "unique" ... Read More