- 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
![Mahesh Parahar](https://www.tutorialspoint.com/assets/profiles/127878/profile/60_2277600-1655382864.jpg)
701 Views
MySQL is a relational database. MongoDB is NoSQL Database.Following are the important differences between MySQL and MongoDB.Sr. No.KeyMySQLMongoDB1Owned/Developed ByMySQL is owned by Oracle.MongoDB is developed by MongoDB Inc.2Data StorageMySql stores data in tabular format as records in table.MongoDB stores records as documents.3LanguageSQL, Structured Query Language is used to query database.Dynamic Schema. Predefined structure is defined for incoming data.4Design ObjectivesNo efficient replication and Sharding available.High availability, Scalability, Replication and Sharding are inbuilt.5TermsMongoDB uses Collection, Document, Field, Embedded Document, Linking etc.MySQL uses Table, Row, Column, Joins etc.6Data StorageMySQL stores data in forms of records in table.MongoDB stores data in form of JSON ... Read More
![Nishtha Thakur](https://www.tutorialspoint.com/assets/profiles/13598/profile/60_104893-1512719058.jpg)
904 Views
To count number of elements in an array, use the aggregate framework. Let us first create a collection with documents −>db.countNumberOfElementsDemo.insertOne({"UserMessage":["Hi", "Hello", "Bye", "Awesome"]}); { "acknowledged" : true, "insertedId" : ObjectId("5cef8ec2ef71edecf6a1f6a1") }Display all documents from a collection with the help of find() method −> db.countNumberOfElementsDemo.find().pretty();This will produce the following output −{ "_id" : ObjectId("5cef8ec2ef71edecf6a1f6a1"), "UserMessage" : [ "Hi", "Hello", "Bye", "Awesome" ] }Following is the query to count number of elements in an array −> db.countNumberOfElementsDemo.aggregate({$project: { NumberOfElements: { $size:"$UserMessage" }}})This will produce ... Read More
![Smita Kapse](https://www.tutorialspoint.com/assets/profiles/13597/profile/60_103706-1512718957.jpg)
123 Views
Let us first create a collection with documents −> db.queryMongoValueDemo.insertOne( { _id:101, "ScoreDetails":[{Score:80}, {Score:45}, {Score:25}, {Score:70}] } ); { "acknowledged" : true, "insertedId" : 101 } > db.queryMongoValueDemo.insertOne( { _id:102, "ScoreDetails":[{Score:80}, {Score:24}, {Score:34}] } ); { "acknowledged" : true, "insertedId" : 102 } > db.queryMongoValueDemo.insertOne( { _id:103, "ScoreDetails":[{Score:99}, {Score:95}] } ); { "acknowledged" : true, "insertedId" : 103 }Display all documents from a collection with the help of find() method −> db.queryMongoValueDemo.find().pretty();This will produce the ... Read More
![Anvi Jain](https://www.tutorialspoint.com/assets/profiles/13591/profile/60_98631-1512716973.jpg)
133 Views
To get distinct first word from a string, you can use distinct(). Let us first create a collection with documents −> db.distinctFirstWordDemo.insertOne( { "_id": 100, "StudentName":"John", "StudentFeature": "John is a good player", "Subject":"MongoDB" } ); { "acknowledged" : true, "insertedId" : 100 } > db.distinctFirstWordDemo.insertOne( { "_id": 101, "StudentName":"Carol", "StudentFeature": "Carol is not a good player", "Subject":"MongoDB" } ); { "acknowledged" : true, "insertedId" : 101 }Display all documents from a collection ... Read More
![Nishtha Thakur](https://www.tutorialspoint.com/assets/profiles/13598/profile/60_104893-1512719058.jpg)
236 Views
To query all items, use find(). Let us first create a collection with documents −> db.queryAllItemsDemo.insertOne({"StudentDetails":{"StudentName":"John", "StudentSubject":["MongoDB", "MySQL"], "StudentSubjectPrice":[4000, 6000]}, "OtherDetails":{"UserAge":29, "UserCountryName":"US"}}); { "acknowledged" : true, "insertedId" : ObjectId("5cef74ecef71edecf6a1f69f") }Display all documents from a collection with the help of find() method −> db.queryAllItemsDemo.find().pretty();This will produce the following output −{ "_id" : ObjectId("5cef74ecef71edecf6a1f69f"), "StudentDetails" : { "StudentName" : "John", "StudentSubject" : [ "MongoDB", "MySQL" ], "StudentSubjectPrice" : [ 4000, ... Read More
![Smita Kapse](https://www.tutorialspoint.com/assets/profiles/13597/profile/60_103706-1512718957.jpg)
144 Views
Use $addToSet operator to ensures that there are no duplicate items added to the set. Let us first create a collection with documents −> db.getDistinctDemo.insertOne({"Values":[100, 200]}); { "acknowledged" : true, "insertedId" : ObjectId("5cef69f9ef71edecf6a1f69d") } > db.getDistinctDemo.insertOne({"Values":[300, 100]}); { "acknowledged" : true, "insertedId" : ObjectId("5cef6a07ef71edecf6a1f69e") }Display all documents from a collection with the help of find() method −> db.getDistinctDemo.find().pretty();This will produce the following output −{ "_id" : ObjectId("5cef69f9ef71edecf6a1f69d"), "Values" : [ 100, 200 ] } { "_id" : ObjectId("5cef6a07ef71edecf6a1f69e"), "Values" : [ 300, ... Read More
![Smita Kapse](https://www.tutorialspoint.com/assets/profiles/13597/profile/60_103706-1512718957.jpg)
480 Views
To extract the particular element in MongoDB, you can use $elemMatch operator. Let us first create a collection with documents −> db.particularElementDemo.insertOne( { "GroupId" :"Group-1", "UserDetails" : [ { "UserName" : "John", "UserOtherDetails" : [ { "UserEmailId" : "John123@gmail.com", "UserFriendName" : [ ... Read More
![Nishtha Thakur](https://www.tutorialspoint.com/assets/profiles/13598/profile/60_104893-1512719058.jpg)
2K+ Views
To build query dynamically, you need to write some script. Let us first create a collection with documents −> db.dynamicQueryDemo.insertOne({"Name":"John", "Subject":["MongoDB", "MySQL"]}); { "acknowledged" : true, "insertedId" : ObjectId("5cef5c5def71edecf6a1f69a") } > db.dynamicQueryDemo.insertOne({"Name":"John", "Subject":["C", "C++"]}); { "acknowledged" : true, "insertedId" : ObjectId("5cef5c73ef71edecf6a1f69b") } > db.dynamicQueryDemo.insertOne({"Name":"John", "Subject":["MongoDB", "Java"]}); { "acknowledged" : true, "insertedId" : ObjectId("5cef5c8bef71edecf6a1f69c") }Display all documents from a collection with the help of find() method −> db.dynamicQueryDemo.find().pretty();This will produce the following output −{ "_id" : ObjectId("5cef5c5def71edecf6a1f69a"), "Name" : "John", "Subject" : [ ... Read More
![Smita Kapse](https://www.tutorialspoint.com/assets/profiles/13597/profile/60_103706-1512718957.jpg)
237 Views
To retrieve an embedded object as a document, use the aggregation $replaceRoot. Let us first create a collection with documents −> db.embeddedObjectDemo.insertOne( { _id: new ObjectId(), "UserDetails": { "UserName": "John", "UserAge": 24, "UserEmailId": "John22@gmail.com" } } ); { "acknowledged" : true, "insertedId" : ObjectId("5ced580fef71edecf6a1f693") } > db.embeddedObjectDemo.insertOne( { _id: new ObjectId(), "UserDetails": { "UserName": "Carol", "UserAge": 26, "UserEmailId": "Carol123@gmail.com" } } ); { "acknowledged" : true, "insertedId" : ObjectId("5ced5828ef71edecf6a1f694") }Following is the query to display all documents from a collection with the help of find() method −> db.embeddedObjectDemo.find().pretty();This will produce the ... Read More
![Anvi Jain](https://www.tutorialspoint.com/assets/profiles/13591/profile/60_98631-1512716973.jpg)
126 Views
To query array of nested string, you can use the dot(.) notation. Let us first create a collection with documents −> db.nestedStringDemo.insertOne( { "CustomerName": "John", "CustomerOtherDetails": [ { "Age":29, "CountryName": "US" }, { "CompanyName": "Amazon", "Salary": 150000, "ProjectName": ["Online Library Management System", "Pig Dice Game"] } ] } ); { "acknowledged" : true, "insertedId" : ObjectId("5cea4629ef71edecf6a1f690") } > db.nestedStringDemo.insertOne( { "CustomerName": "Chris", "CustomerOtherDetails": [ { "Age":27, "CountryName": "AUS" }, { "CompanyName": "Google", "Salary": 250000, "ProjectName": ["Chat Application", "Game ... Read More