Found 1659 Articles for Big Data Analytics

Add a boolean field true to returned objects, when a specified value is in array. For NULLor other values, set false.

AmitDiwan
Updated on 14-May-2020 06:54:08

178 Views

For this, use $ifNull. It evaluates an expression and returns the value of the expression if the expression evaluates to a non-null value. Let us first create a collection with documents −> db.demo542.insertOne({"ListOfName":["Chris", "David"]});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8cabc6ef4dcbee04fbbc17") } > db.demo542.insertOne({"ListOfName":null});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8cabc8ef4dcbee04fbbc18") } > db.demo542.insertOne({"ListOfName":["David"]});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8cabd3ef4dcbee04fbbc19") } > db.demo542.insertOne({"Name":"John"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8cabdaef4dcbee04fbbc1a") }Display all documents from a collection with the help of find() method −> db.demo542.find();This will produce the following output −{ "_id" : ObjectId("5e8cabc6ef4dcbee04fbbc17"), "ListOfName" : ... Read More

How to remove element in a MongoDB array?

AmitDiwan
Updated on 14-May-2020 06:48:47

16K+ Views

To remove an element, update, and use $pull in MongoDB. The $pull operator removes from an existing array all instances of a value or values that match a specified condition.Let us first create a collection with documents −db.demo541.insertOne({"software":{"services":["gmail", "facebook", "yahoo"]}});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8ca845ef4dcbee04fbbc11") } > db.demo541.insertOne({"software":{"services":["whatsapp", "twitter"]}});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8ca85cef4dcbee04fbbc12") }Display all documents from a collection with the help of find() method −> db.demo541.find();This will produce the following output −{ "_id" : ObjectId("5e8ca845ef4dcbee04fbbc11"), "software" : { "services" : [ "gmail", "facebook", "yahoo" ] } } { "_id" : ObjectId("5e8ca85cef4dcbee04fbbc12"), "software" ... Read More

Select documents grouped by field in MongoDB?

AmitDiwan
Updated on 14-May-2020 06:46:37

406 Views

To select documents grouped by field in MongoDB, use $group along with $project. Let us first create a collection with documents −> db.demo540.insertOne({id:1, "Name":"Chris", "CountryName":"US"});{    "acknowledged" : true, "insertedId" : ObjectId("5e8ca368ef4dcbee04fbbc0e") } > db.demo540.insertOne({id:1, "Name":"Chris", "CountryName":"UK"});{    "acknowledged" : true, "insertedId" : ObjectId("5e8ca36bef4dcbee04fbbc0f") } > db.demo540.insertOne({id:1, "Name":"Chris", "CountryName":"AUS"});{    "acknowledged" : true, "insertedId" : ObjectId("5e8ca370ef4dcbee04fbbc10") }Display all documents from a collection with the help of find() method −> db.demo540.find();This will produce the following output −{ "_id" : ObjectId("5e8ca368ef4dcbee04fbbc0e"), "id" : 1, "Name" : "Chris", "CountryName" : "US" } { "_id" : ObjectId("5e8ca36bef4dcbee04fbbc0f"), "id" : 1, "Name" : "Chris", "CountryName" ... Read More

MongoDB query to remove subdocument from document?

AmitDiwan
Updated on 14-May-2020 06:42:13

720 Views

To remove subdocument from a document, use $pull along with update(). Let us first create a collection with documents −> db.demo538.insertOne( ... { ...    id:101, ...    "details": ...    { ...       anotherDetails: ...       [ ...          { ...             "Name":"Chris", ...             Age:21 ...          }, ...          { ...             "Name":"David", ...             Age:23 ...          }, ...   ... Read More

How to fire find query on sub-documents in MongoDB?

AmitDiwan
Updated on 14-May-2020 06:40:08

129 Views

For sub-documents, use the dot notation. Let us first create a collection with documents −> db.demo537.insertOne({"details":{"SubjectName":"MongoDB"}});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8c8a10ef4dcbee04fbbc05") } > db.demo537.insertOne({"details":{"SubjectName":"MySQL"}});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8c8a4bef4dcbee04fbbc06") } > db.demo537.insertOne({"details":{"SubjectName":"Java"}});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8c8a51ef4dcbee04fbbc07") }Display all documents from a collection with the help of find() method −> db.demo537.find();This will produce the following output −{ "_id" : ObjectId("5e8c8a10ef4dcbee04fbbc05"), "details" : { "SubjectName" : "MongoDB" } } { "_id" : ObjectId("5e8c8a4bef4dcbee04fbbc06"), "details" : { "SubjectName" : "MySQL" } } { "_id" : ObjectId("5e8c8a51ef4dcbee04fbbc07"), "details" : { "SubjectName" : "Java" } ... Read More

Group with multiple fields and get the count of duplicate field values grouped together inMongoDB

AmitDiwan
Updated on 14-May-2020 06:36:58

809 Views

For this, use MongoDB aggregate and within that, use $cond. The $cond evaluates a boolean expression to return one of the two specified return expressions.Let us first create a collection with documents −> db.demo536.insertOne({"Name1":"Chris", "Name2":"David"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8c843eef4dcbee04fbbc01") } > db.demo536.insertOne({"Name1":"David", "Name2":"Chris"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8c843fef4dcbee04fbbc02") } > db.demo536.insertOne({"Name1":"Bob", "Name2":"Sam"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8c843fef4dcbee04fbbc03") } > db.demo536.insertOne({"Name1":"Chris", "Name2":"David"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8c843fef4dcbee04fbbc04") }Display all documents from a collection with the help of find() method −> db.demo536.find();This will produce the following output −{ "_id" ... Read More

How do I delete array value from a document in MongoDB?

AmitDiwan
Updated on 14-May-2020 06:33:19

388 Views

To delete array values, use $pull in MongoDB. The $pull operator removes from an existing array all instances of a value or values that match a specified condition.Let us first create a collection with documents −> db.demo535.insertOne( ... { ... ...    "studentId" : "101", ...    "studentName" : "Chris", ...    "ListOfMailIds" : [ ...       "Chris@gmail.com", ...       "Chris@yahoo.com" ...    ] ... ... } ... ) {    "acknowledged" : true,    "insertedId" : ObjectId("5e8c82bfef4dcbee04fbbc00") }Display all documents from a collection with the help of find() method −> db.demo535.find();This will produce the following ... Read More

“Structured” grouping query in MongoDB to display result with a new field displaying the count

AmitDiwan
Updated on 14-May-2020 06:29:48

86 Views

For this, use $group in MongoDB IN aggregate(). The $group groups input documents by the specified _id expression and for each distinct grouping, outputs a document. 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 collection with the help of find() method −> db.demo534.find();This will produce the following output −{ "_id" : 10, "ProductId" : 100, "ProductName" : "Product-1" } { ... Read More

Unable to implement $addToSet in MongoDB to fetch values of a single field?

AmitDiwan
Updated on 14-May-2020 06:25:20

208 Views

The $addToSet operator adds value to an array unless the value is already present, in which case $addToSet does nothing to that array.Let us create a collection with documents −> db.demo533.insertOne({"ProjectName":"Online Hospital Management"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8b4cfaef4dcbee04fbbbfc") } > db.demo533.insertOne({"ProjectName":"Online Library Management"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8b4d02ef4dcbee04fbbbfd") } > db.demo533.insertOne({"ProjectName":"Online Hospital Management"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8b4d04ef4dcbee04fbbbfe") } > db.demo533.insertOne({"ProjectName":"Online Customer Tracker"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8b4d0def4dcbee04fbbbff") }Display all documents from a collection with the help of find() method −> db.demo533.find();This will produce the following output −{ ... Read More

MongoDB query to fetch only the “Name” field based on roles?

AmitDiwan
Updated on 14-May-2020 06:22:55

212 Views

For this, use aggregate(). Here, we have considered 3 roles − Admin, Guest, and User. Let us create a collection with documents −> db.demo532.insertOne({"Name":"Chris", "Type":"Admin"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8b4a9def4dcbee04fbbbf9") } > db.demo532.insertOne({"Name":"David", "Type":"Guest"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8b4aa3ef4dcbee04fbbbfa") } > db.demo532.insertOne({"Name":"Bob", "Type":"User"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8b4ab0ef4dcbee04fbbbfb") }Display all documents from a collection with the help of find() method −> db.demo532.find();This will produce the following output −{ "_id" : ObjectId("5e8b4a9def4dcbee04fbbbf9"), "Name" : "Chris", "Type" : "Admin" } { "_id" : ObjectId("5e8b4aa3ef4dcbee04fbbbfa"), "Name" : "David", "Type" : "Guest" } { "_id" ... Read More

Advertisements