Found 1659 Articles for Big Data Analytics

MongoDB query to get minimum and maximum value from documents including some duplicate records

AmitDiwan
Updated on 01-Apr-2020 12:00:28

254 Views

For this, use aggregate() and $group. To get minimum and maximum value, use $min and $max.Let us create a collection with documents −> db.demo167.insertOne({"Score":60}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3693a79e4f06af551997d1") } > db.demo167.insertOne({"Score":80}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3693ab9e4f06af551997d2") } > db.demo167.insertOne({"Score":60}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3693ad9e4f06af551997d3") } > db.demo167.insertOne({"Score":90}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3693b09e4f06af551997d4") } > db.demo167.insertOne({"Score":89}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3693b69e4f06af551997d5") }Display all documents from a collection with the help of find() method −> db.demo167.find();This will produce the following output ... Read More

MongoDB query to return only specific fields (phone numbers) in the form of an array?

AmitDiwan
Updated on 01-Apr-2020 11:58:22

336 Views

Let us create a collection with documents −> db.demo166.insertOne({"details" : { "UserName" : "Chris", "UserAge":29, "PhoneDetails" : { "PhoneNumber" : "98646463533" } } }); {    "acknowledged" : true,    "insertedId" : ObjectId("5e368b159e4f06af551997cf") } > db.demo166.insertOne({"details" : { "UserName" : "David", "UserAge":21, "PhoneDetails" : { "PhoneNumber" : "87664534654" } } }); {    "acknowledged" : true,    "insertedId" : ObjectId("5e368b159e4f06af551997d0") }Display all documents from a collection with the help of find() method −> db.demo166.find();This will produce the following output −{ "_id" : ObjectId("5e368b159e4f06af551997cf"), "details" : { "UserName" : "Chris", "UserAge" : 29, "PhoneDetails" : { "PhoneNumber" : "98646463533" } } ... Read More

Remove only a single document in MongoDB

AmitDiwan
Updated on 01-Apr-2020 11:56:50

77 Views

To remove only a single document in MongoDB, use remove(). Let us create a collection with documents −> db.demo165.insertOne({"ClientId":101, "ClientName":"Chris", "ClientAge":34}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e36895c9e4f06af551997cc") } > db.demo165.insertOne({"ClientId":102, "ClientName":"Bob", "ClientAge":32}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3689659e4f06af551997cd") } > db.demo165.insertOne({"ClientId":103, "ClientName":"David", "ClientAge":35}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e36896d9e4f06af551997ce") }Display all documents from a collection with the help of find() method −> db.demo165.find();This will produce the following output −{ "_id" : ObjectId("5e36895c9e4f06af551997cc"), "ClientId" : 101, "ClientName" : "Chris", "ClientAge" : 34 } { "_id" : ObjectId("5e3689659e4f06af551997cd"), "ClientId" : 102, "ClientName" : ... Read More

How to use MongoDB Aggregate to sort?

AmitDiwan
Updated on 01-Apr-2020 11:54:50

143 Views

Use aggregate() and within that to sort, use $sort in MongoDB. Let us create a collection with documents −> db.demo164.insertOne({"StudentAge":24}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e36883d9e4f06af551997c8") } > db.demo164.insertOne({"StudentAge":25}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3688409e4f06af551997c9") } > db.demo164.insertOne({"StudentAge":22}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3688429e4f06af551997ca") } > db.demo164.insertOne({"StudentAge":21}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3688439e4f06af551997cb") }Display all documents from a collection with the help of find() method −> db.demo164.find();This will produce the following output −{ "_id" : ObjectId("5e36883d9e4f06af551997c8"), "StudentAge" : 24 } { "_id" : ObjectId("5e3688409e4f06af551997c9"), "StudentAge" : 25 } ... Read More

MongoDB query with $all in array

AmitDiwan
Updated on 01-Apr-2020 11:52:49

300 Views

In MongoDB, $all is used to select the documents where the value of a field is an array that contains all the specified elementsLet us create a collection with documents −> db.demo163.insertOne( ...    { ...       "ClientDetails": [{ ...          "ClientName": "Chris" ... ...       }, { ...          "ClientName": "David" ... ...       } ...    ] ... ... } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3686d49e4f06af551997c5") } > db.demo163.insertOne( ...    { ...       "ClientDetails": [{ ...     ... Read More

Updating a MongoDB document and adding new keys only in the first document?

AmitDiwan
Updated on 01-Apr-2020 11:49:47

761 Views

This can be easily achieved using MongoDB update(). Let us create a collection with documents −> db.demo162.insertOne({"StudentName":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3684359e4f06af551997c2") } > db.demo162.insertOne({"StudentName":"Bob"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3684389e4f06af551997c3") } > db.demo162.insertOne({"StudentName":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e36843c9e4f06af551997c4") }Display all documents from a collection with the help of find() method −> db.demo162.find();This will produce the following output −{ "_id" : ObjectId("5e3684359e4f06af551997c2"), "StudentName" : "Chris", "StudentAge" : 23 } { "_id" : ObjectId("5e3684389e4f06af551997c3"), "StudentName" : "Bob" } { "_id" : ObjectId("5e36843c9e4f06af551997c4"), "StudentName" : "David" }Here is the query to ... Read More

Search for multiple documents in MongoDB?

AmitDiwan
Updated on 01-Apr-2020 11:46:48

156 Views

To search for multiple documents in MongoDB, use $in. Let us create a collection with documents −> db.demo161.insertOne({"ClientId":101, "ClientName":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3577cafdf09dd6d0853a09") } > db.demo161.insertOne({"ClientId":102, "ClientName":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3577d0fdf09dd6d0853a0a") } > db.demo161.insertOne({"ClientId":103, "ClientName":"David", "ClientAge":35}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3577dffdf09dd6d0853a0b") } > db.demo161.insertOne({"ClientId":104, "ClientName":"Carol", "ClientAge":31}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3577eefdf09dd6d0853a0c") }Display all documents from a collection with the help of find() method −> db.demo161.find();This will produce the following output −{ "_id" : ObjectId("5e3577cafdf09dd6d0853a09"), "ClientId" : 101, "ClientName" : "Chris" } { ... Read More

GroupBy Date in MongoDB to count duplicate date records

AmitDiwan
Updated on 01-Apr-2020 11:44:06

195 Views

To count duplicate date records in MongoDB, use aggregate() and $group. Let us create a collection with documents −> db.demo160.insertOne({"DueDate":new ISODate()}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e357525fdf09dd6d0853a04") } > db.demo160.insertOne({"DueDate":new ISODate("2019-01-11")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e357532fdf09dd6d0853a05") } > db.demo160.insertOne({"DueDate":new ISODate()}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e357534fdf09dd6d0853a06") } > db.demo160.insertOne({"DueDate":new ISODate("2019-01-11")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e357538fdf09dd6d0853a07") } > db.demo160.insertOne({"DueDate":new ISODate("2020-04-10")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e357542fdf09dd6d0853a08") }Display all documents from a collection with the help of find() method −> db.demo160.find();This will produce the following ... Read More

Calculate average value in MongoDB

AmitDiwan
Updated on 01-Apr-2020 11:40:59

383 Views

To calculate average value in MongoDB, use aggregate() along with $avg. Let us create a collection with documents −> db.demo159.insertOne({"Score":50}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3557b2fdf09dd6d0853a01") } > db.demo159.insertOne({"Score":70}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3557b6fdf09dd6d0853a02") } > db.demo159.insertOne({"Score":60}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3557c4fdf09dd6d0853a03") }Display all documents from a collection with the help of find() method −> db.demo159.find();This will produce the following output −{ "_id" : ObjectId("5e3557b2fdf09dd6d0853a01"), "Score" : 50 } { "_id" : ObjectId("5e3557b6fdf09dd6d0853a02"), "Score" : 70 } { "_id" : ObjectId("5e3557c4fdf09dd6d0853a03"), "Score" : 60 }Following is the query to ... Read More

Get execution stats in MongoDB for a collection

AmitDiwan
Updated on 01-Apr-2020 11:39:19

300 Views

To get stats, use explain() in MongoDB. Let us create a collection with documents −> db.demo157.insertOne({"Status":"Active"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e354fdffdf09dd6d08539fc") } > db.demo157.insertOne({"Status":"InActive"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e354fe3fdf09dd6d08539fd") }Display all documents from a collection with the help of find() method −> db.demo157.find();This will produce the following output −{ "_id" : ObjectId("5e354fdffdf09dd6d08539fc"), "Status" : "Active" } { "_id" : ObjectId("5e354fe3fdf09dd6d08539fd"), "Status" : "InActive" }Following is how to implement explain() in MongoDB −> db.demo157.find({Status: { $in: ['Active', 'InActive'] }}).explain("executionStats");This will produce the following output −{    "queryPlanner" : {       "plannerVersion" ... Read More

Advertisements