Found 1659 Articles for Big Data Analytics

How to get unique values from MongoDB collection?

AmitDiwan
Updated on 14-May-2020 09:41:17

16K+ Views

To get unique values and ignore duplicates, use distinct() in MongoDB. The distinct() finds the distinct values for a specified field across a single collection and returns the results in an array.Let us create a collection with documents −> db.demo704.insertOne({"LanguageCode":"hi"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea6ee18551299a9f98c93bd") } > db.demo704.insertOne({"LanguageCode":"en"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea6ee1e551299a9f98c93be") } > db.demo704.insertOne({"LanguageCode":"hi"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea6ee20551299a9f98c93bf") } > db.demo704.insertOne({"LanguageCode":"eo"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea6ee2c551299a9f98c93c0") } > db.demo704.insertOne({"LanguageCode":"eu"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea6ee2f551299a9f98c93c1") } > db.demo704.insertOne({"LanguageCode":"fo"}); ... Read More

MongoDB query to count the number of array items in documents and display in a new field

AmitDiwan
Updated on 14-May-2020 09:39:38

100 Views

To count the number of array items in a document, use $size in MongoDB. Let us create a collection with documents −> db.demo703.insertOne({"ListOfSubject":["MySQL", "MongoDB"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea6ebaf551299a9f98c93b4") } > db.demo703.insertOne({"ListOfSubject":["Java"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea6ebb5551299a9f98c93b5") } > db.demo703.insertOne({"ListOfSubject":["C", "C++", "Python"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea6ebbf551299a9f98c93b6") }Display all documents from a collection with the help of find() method −> db.demo703.find();This will produce the following output −{ "_id" : ObjectId("5ea6ebaf551299a9f98c93b4"), "ListOfSubject" : [ "MySQL", "MongoDB" ] } { "_id" : ObjectId("5ea6ebb5551299a9f98c93b5"), "ListOfSubject" : [ "Java" ] } { ... Read More

Create index in a MongoDB collection?

AmitDiwan
Updated on 14-May-2020 09:39:12

143 Views

To create index, use createIndex() in MongoDB. Let us create a collection with documents −> db.demo702.createIndex({"details.id":1}); {    "createdCollectionAutomatically" : true,    "numIndexesBefore" : 1,    "numIndexesAfter" : 2,    "ok" : 1 } > db.demo702.insertOne({ ...    "details" : [ ...       { ...          id:101, ...          studentInfo:{ ...             "StudentName" : "Chris", ...             "StudentAge" : 23, ...          } ...       }, ...    { ... ...       id: 102, ...       studentInfo:{ ...          "StudentName" : "Robert", ...          "StudentAge" : 20, ...       } ...    } ... ] ... } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea6ea3b551299a9f98c93b3") }Display all documents from a collection with the help of find() method −> db.demo702.find().pretty();This will produce the following output −{    "_id" : ObjectId("5ea6ea3b551299a9f98c93b3"),    "details" : [       {          "id" : 101,          "studentInfo" : {             "StudentName" : "Chris",             "StudentAge" : 23          }       },       {          "id" : 102,          "studentInfo" : {             "StudentName" : "Robert",             "StudentAge" : 20          }       }    ] }

MongoDB query to match documents with array values greater than a specific value

AmitDiwan
Updated on 14-May-2020 09:37:15

741 Views

You can use $elemMatch. The $elemMatch operator matches documents that contain an array field with at least one element that matches all the specified query criteria.Let us create a collection with documents −> db.demo701.insertOne({"ListOfValues":[100, 200, 300]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea6e8cf551299a9f98c93b0") } > db.demo701.insertOne({"ListOfValues":[500, 700, 1000]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea6e8d8551299a9f98c93b1") } > db.demo701.insertOne({"ListOfValues":[300, 350, 450]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea6e8e1551299a9f98c93b2") }Display all documents from a collection with the help of find() method −> db.demo701.find();This will produce the following output −{ "_id" : ObjectId("5ea6e8cf551299a9f98c93b0"), "ListOfValues" : [ 100, ... Read More

MongoDB query to display documents with a specific name irrespective of case

AmitDiwan
Updated on 14-May-2020 09:36:49

99 Views

For this, use $regex in MongoDB. We will search for document field value with name “David”, irrespective of case. Let us create a collection with documents −> db.demo700.insertOne( { details: [ { Name:"david" }]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea6e6b1551299a9f98c93ac") } > db.demo700.insertOne( { details: [ { Name:"Chris" }]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea6e6b9551299a9f98c93ad") } > db.demo700.insertOne( { details: [ { Name:"DAVID" }]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea6e6bf551299a9f98c93ae") } > db.demo700.insertOne( { details: [ { Name:"David" }]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea6e6c4551299a9f98c93af") }Display all documents ... Read More

Multiple atomic updates using MongoDB?

AmitDiwan
Updated on 14-May-2020 09:32:01

124 Views

For multiple atomic updates, use update() along with $set. Let us create a collection with documents −> db.demo699.insertOne({Name:"Chris Brown"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea6e370551299a9f98c93a7") } > db.demo699.insertOne({Name:"David Miller"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea6e37a551299a9f98c93a8") } > db.demo699.insertOne({Name:"Chris Brown"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea6e381551299a9f98c93a9") } > db.demo699.insertOne({Name:"John Doe"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea6e38a551299a9f98c93aa") }Display all documents from a collection with the help of find() method −> db.demo699.find();This will produce the following output −{ "_id" : ObjectId("5ea6e370551299a9f98c93a7"), "Name" : "Chris Brown" } { "_id" : ObjectId("5ea6e37a551299a9f98c93a8"), "Name" ... Read More

How do I get email-id from a MongoDB document and display with print()

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

362 Views

For this, use forEach() along with print() to display the email-id values. Let us create a collection with documents −> db.demo690.insertOne({"UserName":"John", "UserEmailId":"John@gmail.com"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea6db31551299a9f98c939c") } > db.demo690.insertOne({"UserName":"Bob", "UserEmailId":"Bob@gmail.com"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea6db3c551299a9f98c939d") } > db.demo690.insertOne({"UserName":"David", "UserEmailId":"David@gmail.com"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea6db47551299a9f98c939e") }Display all documents from a collection with the help of find() method −> db.demo690.find();This will produce the following output −{ "_id" : ObjectId("5ea6db31551299a9f98c939c"), "UserName" : "John", "UserEmailId" : "John@gmail.com" } { "_id" : ObjectId("5ea6db3c551299a9f98c939d"), "UserName" : "Bob", "UserEmailId" : "Bob@gmail.com" } { "_id" ... Read More

Increment only a single value in MongoDB document?

AmitDiwan
Updated on 14-May-2020 09:27:54

95 Views

To update only a single value and increment it in MongoDB, use $inc along with update(). Let us create a collection with documents −> db.demo698.insertOne({Score:78}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea6d8a4551299a9f98c9398") } > db.demo698.insertOne({Score:56}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea6d8a7551299a9f98c9399") } > db.demo698.insertOne({Score:65}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea6d8aa551299a9f98c939a") } > db.demo698.insertOne({Score:88}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea6d8b0551299a9f98c939b") }Display all documents from a collection with the help of find() method −> db.demo698.find();This will produce the following output −{ "_id" : ObjectId("5ea6d8a4551299a9f98c9398"), "Score" : 78 } { "_id" : ... Read More

Get number of records in MongoDB?

AmitDiwan
Updated on 14-May-2020 09:26:07

280 Views

To get number of records, use count() in MongoDB. Let us create a collection with documents −> db.demo697.insertOne({Name:"Chris", Age:21}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea6d7d1551299a9f98c9395") } > db.demo697.insertOne({Name:"Bob", Age:23}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea6d7d8551299a9f98c9396") } > db.demo697.insertOne({Name:"David", Age:24}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea6d7dd551299a9f98c9397") }Display all documents from a collection with the help of find() method −> db.demo697.find();This will produce the following output −{ "_id" : ObjectId("5ea6d7d1551299a9f98c9395"), "Name" : "Chris", "Age" : 21 } { "_id" : ObjectId("5ea6d7d8551299a9f98c9396"), "Name" : "Bob", "Age" : 23 } { "_id" : ObjectId("5ea6d7dd551299a9f98c9397"), "Name" ... Read More

Build (escape) regexp in MongoDB?

AmitDiwan
Updated on 14-May-2020 09:25:04

251 Views

For this, use find() along with //i. Let us create a collection with documents −> db.demo696.insertOne({Message:"/Good/"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea6d664551299a9f98c9391") } > db.demo696.insertOne({Message:"(good)"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea6d67a551299a9f98c9392") } > db.demo696.insertOne({Message:"/Bye/"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea6d68b551299a9f98c9393") } > db.demo696.insertOne({Message:"(GOOD)"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea6d693551299a9f98c9394") }Display all documents from a collection with the help of find() method −> db.demo696.find();This will produce the following output −{ "_id" : ObjectId("5ea6d664551299a9f98c9391"), "Message" : "/Good/" } { "_id" : ObjectId("5ea6d67a551299a9f98c9392"), "Message" : "(good)" } { "_id" : ObjectId("5ea6d68b551299a9f98c9393"), ... Read More

Advertisements