Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
MongoDB Articles
Page 111 of 111
Difference between MySQL and MongoDB
MySQL is a relational database management system (RDBMS) that stores data in structured tables with rows and columns. MongoDB is a NoSQL document database that stores data as flexible JSON-like documents. Both are widely used but serve different use cases. MySQL MySQL is an open-source relational database owned by Oracle. It uses SQL (Structured Query Language) to query and manage data stored in predefined tables with fixed schemas. MySQL enforces data integrity through relationships, constraints, and joins. MongoDB MongoDB is an open-source NoSQL database developed by MongoDB Inc. It stores data as BSON (Binary JSON) documents ...
Read MoreCount the number of items in an array in MongoDB?
To count the number of items in an array in MongoDB, you can use the $size operator within the $project stage of an aggregation pipeline. The $size operator returns the number of elements in a specified array field. The syntax is − db.yourCollectionName.aggregate({ $project: { anyFieldName: { $size: "$yourArrayName" } } }).pretty(); Creating a Sample Collection To understand the above syntax, let us create a collection with documents. Each document contains a StudentMarks array with a different number of elements ? ...
Read MoreHow to select a single field in MongoDB?
You can select a single field in MongoDB using the projection parameter in the find() method. Projection lets you specify which fields to include or exclude in the query results. The syntax is − db.yourCollectionName.find( {"yourFieldName": yourValue}, {"yourSingleFieldName": 1, _id: 0} ); In the above syntax, "yourSingleFieldName": 1 means include this field in the result, and _id: 0 means exclude the _id field which MongoDB includes by default. Creating a Sample Collection To understand the above syntax, let us create a collection with documents ? db.singleFieldDemo.insertOne({"StudentName": "David", "StudentAge": 28}); ...
Read More"Structured" grouping query in MongoDB to display result with a new field displaying the count
For this, use $group in MongoDB with aggregate(). The $group stage groups input documents by the specified _id expression and for each distinct grouping, outputs a document. Structured grouping allows you to create complex grouping hierarchies and add calculated fields like counts to your results. Let us first create a collection with documents − > db.demo534.insertOne({_id:10, "ProductId":100, "ProductName":"Product-1"}); { "acknowledged" : true, "insertedId" : 10 } > db.demo534.insertOne({_id:11, "ProductId":100, "ProductName":"Product-2"}); { "acknowledged" : true, "insertedId" : 11 } > db.demo534.insertOne({_id:12, "ProductId":101, "ProductName":"Product-1"}); { "acknowledged" : true, "insertedId" : 12 } Display all documents from a ...
Read More"Toggle" query in MongoDB?
To implement a toggle query in MongoDB, you need to find the document first and then use the update operation to toggle the boolean field value. The toggle operation switches a boolean value from true to false or vice versa. Let us first create a collection with sample documents − > db.toggleDemo.insertOne({"CustomerName":"John Smith", "CustomerAge":28, "isMarried":true}); { "acknowledged" : true, "insertedId" : ObjectId("5cc7be138f9e6ff3eb0ce43b") } > db.toggleDemo.insertOne({"CustomerName":"David Miller", "CustomerAge":25, "isMarried":false}); { "acknowledged" : true, "insertedId" : ObjectId("5cc7be2e8f9e6ff3eb0ce43c") } Following is the query to display all ...
Read MoreFind document with array that contains a specific value in MongoDB
You can use find() method to find document with array that contains a specific value. The syntax is as follows:db.yourCollectionName.find({"yourArrayFieldName":"yourValue"}, .......N).pretty();To understand the above syntax, let us create a collection with documents. The query to create a collection with documents is as follows:>db.findSpecificValue.insertOne({"StudentId":1, "StudentName":"Larry", "FavouriteSubject":["C", "C++", "Java"]}); { "acknowledged" : true, "insertedId" : ObjectId("5c6e8996140577d89182b8d0") } >db.findSpecificValue.insertOne({"StudentId":2, "StudentName":"Larry", "FavouriteSubject":["MongoDB", "MySQL", "SQL Server"]}); { "acknowledged" : true, "insertedId" : ObjectId("5c6e89b1140577d89182b8d1") }Display all documents from a collection with the help of find() method. The query is as follows:> db.findSpecificValue.find().pretty();The following is the output:{ "_id" : ObjectId("5c6e8996140577d89182b8d0"), "StudentId" ...
Read More