Found 1659 Articles for Big Data Analytics

Update elements inside an array in MongoDB?

AmitDiwan
Updated on 13-May-2020 05:34:05

1K+ Views

To update elements inside an array, use $set in MongoDB. Let us create a collection with documents −> db.demo494.insertOne( ... { ... ...    "CollegeDetails" : [ ...       { ...          "CollegeName" : "MIT", ...          "Fees" : 80000 ...       }, ...       { ...          "CollegeName" : "SU", ...          "Fees" : 90000 ...       } ...    ] ... } ... ) {    "acknowledged" : true,    "insertedId" : ObjectId("5e84a5c1b0f3fa88e22790c9") }Display all documents from a ... Read More

Count unique items in array-based fields across all MongoDB documents?

AmitDiwan
Updated on 13-May-2020 05:29:37

674 Views

To count unique items in array-based fields, use $group along with aggregate(). Let us create a collection with documents −> db.demo493.insertOne({"SubjectName":["MySQL", "MongoDB", "Java"]});{    "acknowledged" : true,    "insertedId" : ObjectId("5e849f97b0f3fa88e22790c4") } > db.demo493.insertOne({"SubjectName":["C++", "MongoDB", "C"]});{    "acknowledged" : true,    "insertedId" : ObjectId("5e849fa4b0f3fa88e22790c5") } > db.demo493.insertOne({"SubjectName":["MySQL", "MongoDB", "C"]});{    "acknowledged" : true,    "insertedId" : ObjectId("5e849fb2b0f3fa88e22790c6") }Display all documents from a collection with the help of find() method −> db.demo493.find();This will produce the following output −{ "_id" : ObjectId("5e849f97b0f3fa88e22790c4"), "SubjectName" : [ "MySQL", "MongoDB", "Java" ] } { "_id" : ObjectId("5e849fa4b0f3fa88e22790c5"), "SubjectName" : [ "C++", "MongoDB", "C" ] } ... Read More

Make nested queries in MongoDB 4 to fetch a specific document

AmitDiwan
Updated on 13-May-2020 05:27:50

358 Views

For nested queries, let us first create a collection with documents −> db.demo492.insertOne({ ...    "ProductDetails" : ...    { ...       "StockDetails" : [ ...          { "ProductName" : "Product-1" }, ...          {"ProductName" : "Product-2"}, ...          { "ProductName" : "Product-3"} ... ...       ] ... ...    } ... }); {    "acknowledged" : true,    "insertedId" : ObjectId("5e849db8b0f3fa88e22790c2") } > > > > db.demo492.insertOne({ ...    "ProductDetails" : ...    { ...       "StockDetails" : [ ...         ... Read More

How to match date with MongoDB $match?

AmitDiwan
Updated on 13-May-2020 05:25:24

3K+ Views

To match date, use $match along with aggregate(). Let us create a collection with documents −> db.demo491.insertOne({"ShippingDate":new ISODate("2020-01-10")});{    "acknowledged" : true,    "insertedId" : ObjectId("5e849a09b0f3fa88e22790be") } > db.demo491.insertOne({"ShippingDate":new ISODate("2020-02-21")});{    "acknowledged" : true,    "insertedId" : ObjectId("5e849a0eb0f3fa88e22790bf") } > db.demo491.insertOne({"ShippingDate":new ISODate("2020-03-23")});{    "acknowledged" : true,    "insertedId" : ObjectId("5e849a1db0f3fa88e22790c0") } > db.demo491.insertOne({"ShippingDate":new ISODate()});{    "acknowledged" : true,    "insertedId" : ObjectId("5e849a28b0f3fa88e22790c1") }Display all documents from a collection with the help of find() method −> db.demo491.find();This will produce the following output −{ "_id" : ObjectId("5e849a09b0f3fa88e22790be"), "ShippingDate" : ISODate("2020-01- 10T00:00:00Z") } { "_id" : ObjectId("5e849a0eb0f3fa88e22790bf"), "ShippingDate" : ISODate("2020-02- 21T00:00:00Z") } { ... Read More

MongoDB query to fetch random value using Map Reduce concept.

AmitDiwan
Updated on 12-May-2020 09:02:03

221 Views

For random value with Map Reduce, use mapReduce() concept along with Math.random(). Let us create a collection with documents −> db.demo651.insertOne({Value:10}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9f0330e3c3cd0dcff36a57") } > db.demo651.insertOne({Value:20}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9f0332e3c3cd0dcff36a58") } > db.demo651.insertOne({Value:30}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9f0335e3c3cd0dcff36a59") } > db.demo651.insertOne({Value:40}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9f0337e3c3cd0dcff36a5a") } > db.demo651.insertOne({Value:50}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9f0339e3c3cd0dcff36a5b") } > db.demo651.insertOne({Value:60}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9f033be3c3cd0dcff36a5c") } > db.demo651.insertOne({Value:70}); {    "acknowledged" : true,    "insertedId" ... Read More

Querying only the field name and display only the id in MongoDB?

AmitDiwan
Updated on 12-May-2020 08:57:02

746 Views

To query only the field name, set fieldName to 0 i.e. the fieldName to hide. Let us create a collection with documents −> db.demo650.insertOne({_id:101, details:{Name:"Chris", Age:21}}); { "acknowledged" : true, "insertedId" : 101 } > db.demo650.insertOne({_id:102, details:{Name:"Bob", Age:22}}); { "acknowledged" : true, "insertedId" : 102 } > db.demo650.insertOne({_id:103, details:{Name:"Sam", Age:20}}); { "acknowledged" : true, "insertedId" : 103 } > db.demo650.insertOne({_id:104, details:{Name:"Robert", Age:24}}); { "acknowledged" : true, "insertedId" : 104 }Display all documents from a collection with the help of find() method −> db.demo650.find();This will produce the following output −{ "_id" : 101, "details" : { "Name" : "Chris", "Age" : ... Read More

MongoDB aggregation group and remove duplicate array values?

AmitDiwan
Updated on 12-May-2020 08:55:19

2K+ Views

Use MongoDB aggregate for this and within that, use $group. Let us create a collection with documents −> db.demo649.insertOne( ...    { "_id" : 101, "Names" : [ "John", "Bob", "Bob", "Robert" ], "CountryName" : "US" } ... ); { "acknowledged" : true, "insertedId" : 101 } > > db.demo649.insertOne({ "_id" :102, "Names" : [ "John", "Robert" ], "CountryName" : "UK"}); { "acknowledged" : true, "insertedId" : 102 }Display all documents from a collection with the help of find() method −> db.demo649.find();This will produce the following output −{ "_id" : 101, "Names" : [ "John", "Bob", "Bob", "Robert" ], "CountryName" ... Read More

Create and display the newly created database in MongoDB?

AmitDiwan
Updated on 12-May-2020 08:53:29

83 Views

To create a new database, you need to use the USE command as in the below syntax −use yourDatabaseName;To show all databases, you need to use show command. The syntax is as follows −show dbs;Let us implement the above syntax in order to create database −> use onlinecustomertracker; switched to db onlinecustomertrackerIn order to display the newly created database, you need to create collection. Following is the query −> db.example.insert({Value:10}); WriteResult({ "nInserted" : 1 })Display all documents from a collection with the help of find() method −> db.example.find();This will produce the following output −{ "_id" : ObjectId("5e9dafade3c3cd0dcff36a4f"), "Value" : 10 ... Read More

Perform nested document value search in MongoDB?

AmitDiwan
Updated on 12-May-2020 08:50:43

92 Views

For searching value, use $match in MongoDB. Let us create a collection with documents −> db.demo648.insertOne( ...    { ...       StudentInformation: ...       [ ...          { ...             Name:"John", ...             CountryName:"US" ...          }, ...          { ...             Name:"David", ...             CountryName:"AUS" ...          }, ...          { ...             Name:"Chris", ... ... Read More

MongoDB aggregation to combine or merge fields and then count?

AmitDiwan
Updated on 12-May-2020 08:47:14

556 Views

To combine or merge fields and then perform count, use $group along with $sum and $sort. Let us create a collection with documents −> db.demo647.insertOne({"Subject":"MySQL"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9c86316c954c74be91e6ee") } > db.demo647.insertOne({"Subject":"MongoDB"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9c86356c954c74be91e6ef") } > db.demo647.insertOne({"Subject":"MySQL"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9c86376c954c74be91e6f0") } > db.demo647.insertOne({"Subject":"SQL Server"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9c86406c954c74be91e6f1") } > db.demo647.insertOne({"Subject":"MongoDB"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9c86436c954c74be91e6f2") } > db.demo647.insertOne({"Subject":"PL/SQL"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9c864b6c954c74be91e6f3") } > db.demo647.insertOne({"Subject":"MongoDB"}); {   ... Read More

Advertisements