Found 1659 Articles for Big Data Analytics

How can I aggregate collection and group by field count in MongoDB?

AmitDiwan
Updated on 12-May-2020 06:43:28

574 Views

In MongoDB aggregate(), use $group and aggregate collection. Let us create a collection with documents −> db.demo616.insertOne({"details":{"Name":"Chris", "Age":21}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e99bfac65492f6c60d00283") } > db.demo616.insertOne({"details":{"Name":"Chris", "Age":22}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e99bfb065492f6c60d00284") } > db.demo616.insertOne({"details":{"Name":"Bob", "Age":23}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e99bfb865492f6c60d00285") } > db.demo616.insertOne({"details":{"Name":"Sam", "Age":21}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e99bfbd65492f6c60d00286") } > db.demo616.insertOne({"details":{"Name":"Chris", "Age":24}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e99bfc165492f6c60d00287") }Display all documents from a collection with the help of find() method −> db.demo616.find();This will produce the following output −{ "_id" ... Read More

How to use $type in MongoDB?

AmitDiwan
Updated on 12-May-2020 06:41:09

83 Views

The $type selects documents where the value of the field is an instance of the specified BSON type. Let us create a collection with documents −> db.demo615.insert({"Value":100}); WriteResult({ "nInserted" : 1 }) > db.demo615.insert({"Value":"100"}); WriteResult({ "nInserted" : 1 }) > db.demo615.insert({"Value":"300"}); WriteResult({ "nInserted" : 1 }) > db.demo615.insert({"Value":300}); WriteResult({ "nInserted" : 1 })Display all documents from a collection with the help of find() method −> db.demo615.find();This will produce the following output −{ "_id" : ObjectId("5e99bb3465492f6c60d0027f"), "Value" : 100 } { "_id" : ObjectId("5e99bb3865492f6c60d00280"), "Value" : "100" } { "_id" : ObjectId("5e99bb3c65492f6c60d00281"), "Value" : "300" } { "_id" : ObjectId("5e99bb4265492f6c60d00282"), "Value" ... Read More

What is ({$natural: 1}) in MongoDB?

AmitDiwan
Updated on 12-May-2020 06:40:05

723 Views

The ({$natural − 1}) works like LIFO(LAST IN FIRST OUT), that means last inserted document will be shown first.Let us create a collection with documents −> db.demo614.insertOne({"CountryName":"US"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e988cddf6b89257f5584d8e") } > db.demo614.insertOne({"CountryName":"UK"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e988ce0f6b89257f5584d8f") } > db.demo614.insertOne({"CountryName":"AUS"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e988ce3f6b89257f5584d90") } > db.demo614.insertOne({"CountryName":"IND"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e988cebf6b89257f5584d91") }Display all documents from a collection with the help of find() method −> db.demo614.find();This will produce the following output −{ "_id" : ObjectId("5e988cddf6b89257f5584d8e"), "CountryName" : "US" } { ... Read More

Implementing String Comparison in MongoDB?

AmitDiwan
Updated on 11-May-2020 10:05:23

573 Views

To implement string comparison in MongoDB, use $strcasecmp. It performs case-insensitive comparison of two strings. It returns −1 if first string is “greater than” the second string.0 if the two strings are equal.-1 if the first string is “less than” the second string.Let us create a collection with documents −> db.demo490.insertOne({"Name1":"John", "Name2":"john"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8496ccb0f3fa88e22790bb") } > db.demo490.insertOne({"Name1":"David", "Name2":"Bob"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8496d9b0f3fa88e22790bc") } > db.demo490.insertOne({"Name1":"Carol", "Name2":"Carol"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8496e5b0f3fa88e22790bd") }Display all documents from a collection with the help of find() method −> db.demo490.find();This will produce ... Read More

MongoDB query to update array object in index N?

AmitDiwan
Updated on 11-May-2020 10:04:05

219 Views

Use update() in MongoDB to update array object. The usage of dot notation is also required. Let us create a collection with documents −> db.demo489.insertOne( ... { ... ... ...    details : [{ ...       id : 101, ...       "Info1" : { ...          "StudentName" : "Chris" ...       }, ...       "Info2" : { ...          "TeacherName" : "David" ...       } ...    }, ...    { ...       id : 102, ...       "Info1" : ... Read More

How to delete partial data in MongoDB?

AmitDiwan
Updated on 11-May-2020 10:01:57

198 Views

Set the value to be deleted in a variable To delete particle data, use remove(). Let us create a collection with documents −> db.demo488.insertOne({"Name":"Chris"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8351e0b0f3fa88e22790b2") } > db.demo488.insertOne({"Name":"David"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8351e8b0f3fa88e22790b3") } > db.demo488.insertOne({"Name":"Bob"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8351ebb0f3fa88e22790b4") } > db.demo488.insertOne({"Name":"Mike"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8351eeb0f3fa88e22790b5") } > db.demo488.insertOne({"Name":"Sam"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e835202b0f3fa88e22790b6") } > db.demo488.insertOne({"Name":"John"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e835207b0f3fa88e22790b7") } > db.demo488.insertOne({"Name":"Robert"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e83520cb0f3fa88e22790b8") }Display all ... Read More

Implement a query similar to MySQL Union with MongoDB?

AmitDiwan
Updated on 11-May-2020 10:01:25

476 Views

For a similar query to UNION two collections, use JOIN in MongoDB along with aggregate(). Let us create a collection with documents −> db.demo486.insertOne({_id:1, "Amount":30, "No":4}); { "acknowledged" : true, "insertedId" : 1 } > db.demo486.insertOne({_id:2, "Amount":40, "No":2}); { "acknowledged" : true, "insertedId" : 2 } > db.demo486.insertOne({_id:3, "Amount":60, "No":6}); { "acknowledged" : true, "insertedId" : 3 }Display all documents from a collection with the help of find() method −> db.demo486.find();This will produce the following output −{ "_id" : 1, "Amount" : 30, "No" : 4 } { "_id" : 2, "Amount" : 40, "No" : 2 } { "_id" ... Read More

How to check empty field in a MongoDB collection?

AmitDiwan
Updated on 11-May-2020 09:58:35

3K+ Views

To check empty field in a MongoDB collection, use $exists along with $eq operator. Let us create a collection with documents −> db.demo485.insertOne({"FirstName":"Chris", "LastName":""});{    "acknowledged" : true,    "insertedId" : ObjectId("5e82e9f6b0f3fa88e22790a8") } > db.demo485.insertOne({"FirstName":"David", "LastName":"Miller"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e82e9fdb0f3fa88e22790a9") } > db.demo485.insertOne({"FirstName":"Chris", "LastName":"Brown"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e82ea03b0f3fa88e22790aa") } > db.demo485.insertOne({"FirstName":"Robert", "LastName":""});{    "acknowledged" : true,    "insertedId" : ObjectId("5e82ea0fb0f3fa88e22790ab") }Display all documents from a collection with the help of find() method −> db.demo485.find();This will produce the following output −{ "_id" : ObjectId("5e82e9f6b0f3fa88e22790a8"), "FirstName" : "Chris", "LastName" : "" } { "_id" ... Read More

Search array of objects in a MongoDB collection?

AmitDiwan
Updated on 11-May-2020 09:57:38

333 Views

To search array of objects, use MongoDB find().The find() method selects documents in a collection or view and returns a cursor to the selected documents..Let us create a collection with documents −> db.demo484.insertOne( ... { 'id' : 1, 'details' : [ { 'Name1' : 'Chris' }, { 'Name2' : 'David' }, { 'Name3' : 'Bob' } ] } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e82e3a4b0f3fa88e22790a1") } > db.demo484.insertOne( ... { 'id' : 1, 'details' : [ { 'Name1' : 'Chris' }, { 'Name2' : 'Carol' }, { 'Name3' : 'Bob' } ] } ... ); { ... Read More

Set variable value in MongoDB save() method

AmitDiwan
Updated on 11-May-2020 09:56:07

931 Views

Use db.yourCollectionName.save(yourVariableName) to set variable value, wherein “yourVariableName” is your variable.Let us see an example and create a variable −> var Info={"Name":"David", ... "CountryName":"US", ... "ProjectDetails":[{"ClientName":"David", "ProjectName":"Online Banking System"}]}Following is the query to set the variable value in the save() to save the value in the collection −> db.demo483.save(Info); WriteResult({ "nInserted" : 1 })Display all documents from a collection with the help of find() method −> db.demo483.find().pretty();This will produce the following output −{    "_id" : ObjectId("5e82e0d6b0f3fa88e22790a0"),    "Name" : "David",    "CountryName" : "US",    "ProjectDetails" : [       {          "ClientName" : "David", ... Read More

Advertisements