- 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 1659 Articles for Big Data Analytics
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
206 Views
To insert and limit the total records while inserting, use capped:true and set the size and max values.Let us create a collection with documents wherein we have set capped:true and size to 4 −> db.createCollection("demo297", {capped:true, size:4, max:4}); { "ok" : 1 } > db.demo297.insertOne({"Name":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5e4d54385d93261e4bc9ea43") } > db.demo297.insertOne({"Name":"Bob"}); { "acknowledged" : true, "insertedId" : ObjectId("5e4d543e5d93261e4bc9ea44") } > db.demo297.insertOne({"Name":"Mike"}); { "acknowledged" : true, "insertedId" : ObjectId("5e4d543e5d93261e4bc9ea45") } > db.demo297.insertOne({"Name":"Sam"}); { "acknowledged" : true, "insertedId" : ObjectId("5e4d543f5d93261e4bc9ea46") } > db.demo297.insertOne({"Name":"John"}); { "acknowledged" : true, "insertedId" : ObjectId("5e4d54405d93261e4bc9ea47") ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
73 Views
To query inside array and check for existence to get the count, use $exists. Let us create a collection with documents −> db.demo296.insertOne( ... { ... "id":101, ... "Name":"Chris", ... "details":[ ... { ... SubjectId:[101, 103], ... "SubjectName":["MySQL", "MongoDB"] ... }, ... { ... SubjectId:[102, 104], ... "SubjectName":["Java", "C"] ... } ... ] ... ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
103 Views
Query object’s field array value using arrayFieldName along with value. Let us create a collection with documents −> db.demo295.insertOne({"status":["Active", "Inactive"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e4d4ea65d93261e4bc9ea39") } > db.demo295.insertOne({"status":["Yes", "No"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e4d4eb15d93261e4bc9ea3a") }Display all documents from a collection with the help of find() method −> db.demo295.find().pretty();This will produce the following output −{ "_id" : ObjectId("5e4d4ea65d93261e4bc9ea39"), "status" : [ "Active", "Inactive" ] } { "_id" : ObjectId("5e4d4eb15d93261e4bc9ea3a"), "status" : [ "Yes", "No" ] ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
265 Views
To query or return a MongoDB collection, use getCollection(). Let us create a collection with documents −> db.demo294.insertOne({"EmployeeId":101, "EmployeeName":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5e4d4a1a5d93261e4bc9ea36") } > db.demo294.insertOne({"EmployeeId":102, "EmployeeName":"Bob"}); { "acknowledged" : true, "insertedId" : ObjectId("5e4d4a255d93261e4bc9ea37") } > db.demo294.insertOne({"EmployeeId":103, "EmployeeName":"David"}); { "acknowledged" : true, "insertedId" : ObjectId("5e4d4a335d93261e4bc9ea38") }Display all documents from a collection with the help of find() method −> db.demo294.find();This will produce the following output −{ "_id" : ObjectId("5e4d4a1a5d93261e4bc9ea36"), "EmployeeId" : 101, "EmployeeName" : "Chris" } { "_id" : ObjectId("5e4d4a255d93261e4bc9ea37"), "EmployeeId" : 102, "EmployeeName" : "Bob" } { "_id" : ObjectId("5e4d4a335d93261e4bc9ea38"), "EmployeeId" ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
544 Views
Let us first create a collection with documents −> db.demo293.insertOne({FirstName:"Chris", LastName:"Brown", Age:24}); { "acknowledged" : true, "insertedId" : ObjectId("5e4d45075d93261e4bc9ea32") } > db.demo293.insertOne({FirstName:"David", LastName:"Miller", Age:23}); { "acknowledged" : true, "insertedId" : ObjectId("5e4d45265d93261e4bc9ea33") } > db.demo293.insertOne({FirstName:"John", LastName:"Smith", Age:24}); { "acknowledged" : true, "insertedId" : ObjectId("5e4d45385d93261e4bc9ea34") } > db.demo293.insertOne({FirstName:"Adam", LastName:"Doe", Age:21}); { "acknowledged" : true, "insertedId" : ObjectId("5e4d46cf5d93261e4bc9ea35") }Display all documents from a collection with the help of find() method −> db.demo293.find();This will produce the following output −{ "_id" : ObjectId("5e4d45075d93261e4bc9ea32"), "FirstName" : "Chris", "LastName" : "Brown", "Age" : 24 } { "_id" : ObjectId("5e4d45265d93261e4bc9ea33"), ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
95 Views
For this, check using $where in MongoDB. Let us create a collection with documents −> db.demo292.insertOne({FirstName:"Chris", LastName:"Brown", ... "Friend":{FirstName:"David", "LastName":"Miller"} ... } ...); { "acknowledged" : true, "insertedId" : ObjectId("5e4c10aa5d93261e4bc9ea30") } > db.demo292.insertOne({FirstName:"John", LastName:"Doe", ... "Friend":{FirstName:"Mike", "LastName":"Doe"} ...} ...); { "acknowledged" : true, "insertedId" : ObjectId("5e4c10dc5d93261e4bc9ea31") }Display all documents from a collection with the help of find() method −> db.demo292.find();This will produce the following output −{ "_id" : ObjectId("5e4c10aa5d93261e4bc9ea30"), "FirstName" : "Chris", "LastName" : "Brown", "Friend" : { "FirstName" : "David", "LastName" : "Miller" } } { "_id" : ObjectId("5e4c10dc5d93261e4bc9ea31"), "FirstName" : "John", ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
505 Views
To get the sum of two columns, use $add. Let us create a collection with documents −> db.demo291.insertOne({"Value1":10, "Value2":50}); { "acknowledged" : true, "insertedId" : ObjectId("5e4c0e1e5d93261e4bc9ea2f") }Display all documents from a collection with the help of find() method −> db.demo291.find();This will produce the following output −{ "_id" : ObjectId("5e4c0e1e5d93261e4bc9ea2f"), "Value1" : 10, "Value2" : 50 }Following is the query to get the sum of two columns and save it to another column “Value3” −> db.demo291.aggregate( ... { "$project" : { ... 'Value1' : '$Value1', ... 'Value2' : '$Value2', ... 'Value3' : ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
674 Views
For exact match, you can use $exists that checks for a match. Let us create a collection with documents −> db.demo290.insertOne({"ListOfName":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5e4c0c9e5d93261e4bc9ea2d") } > db.demo290.insertOne({"ListOfName":["Chris", "David"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e4c0cb05d93261e4bc9ea2e") }Display all documents from a collection with the help of find() method −> db.demo290.find();This will produce the following output −{ "_id" : ObjectId("5e4c0c9e5d93261e4bc9ea2d"), "ListOfName" : "Chris" } { "_id" : ObjectId("5e4c0cb05d93261e4bc9ea2e"), "ListOfName" : [ "Chris", "David" ] }Here is the query for exact match of a value −> db.demo290.find({$and: [{'ListOfName.0': {$exists: false}}, {"ListOfName": 'Chris'}]});This will produce the ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
211 Views
To make an array of multiple arrays, use $unwind in MongoDB aggregate. Let us create a collection with documents −> db.demo289.insertOne({"Section":["A", "B", "E"], "Name":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5e4c06fcf49383b52759cbc3") } > db.demo289.insertOne({"Section":["C", "D", "B"], "Name":"David"}); { "acknowledged" : true, "insertedId" : ObjectId("5e4c070af49383b52759cbc4") }Display all documents from a collection with the help of find() method −> db.demo289.find().pretty();This will produce the following output −{ "_id" : ObjectId("5e4c06fcf49383b52759cbc3"), "Section" : [ "A", "B", "E" ], "Name" : "Chris" } { "_id" : ObjectId("5e4c070af49383b52759cbc4"), "Section" : [ "C", "D", "B" ], "Name" ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
228 Views
Let us create a collection with documents −>db.demo288.insertOne({"Name":"Chris", details:[{"CountryName":"US", Marks:78}, {"CountryName":"UK", Marks:68}]}); { "acknowledged" : true, "insertedId" : ObjectId("5e4c0393f49383b52759cbbe") }Display all documents from a collection with the help of find() method −> db.demo288.find();This will produce the following output −{ "_id" : ObjectId("5e4c0393f49383b52759cbbe"), "Name" : "Chris", "details" : [ { "CountryName" : "US", "Marks" : 78 }, { "CountryName" : "UK", "Marks" : 68 } ] }Following is the query to implement $push in embedded document array −> db.demo288.update( {Name: "Chris"}, {$push:{ "details":{"CountryName" : "AUS", "Marks" : 98} }}, {upsert:true}); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : ... Read More