Found 1661 Articles for Big Data Analytics

Display the undefined and exact MongoDB document records

AmitDiwan
Updated on 13-May-2020 05:38:59

318 Views

For this, use the forEach(). To display the values, use printjson(). Let us create a collection with documents −> db.demo496.insertOne({"Name":"David", "CountryName":"US"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e84b04ab0f3fa88e22790ce") } > db.demo496.insertOne({"Name":"John", "CountryName":"AUS"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e84b054b0f3fa88e22790cf") } > db.demo496.insertOne({"Name":"Robert", "CountryName":"UK"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e84b05db0f3fa88e22790d0") }Display all documents from a collection with the help of find() method −> db.demo496.find();This will produce the following output −{ "_id" : ObjectId("5e84b04ab0f3fa88e22790ce"), "Name" : "David", "CountryName" : "US" } { "_id" : ObjectId("5e84b054b0f3fa88e22790cf"), "Name" : "John", "CountryName" : "AUS" } { "_id" : ObjectId("5e84b05db0f3fa88e22790d0"), "Name" : ... Read More

Update only a single MongoDB document without deleting any date

AmitDiwan
Updated on 13-May-2020 05:36:54

234 Views

To update only a single document, you need to update a specific data with updateOne(). The updateOne() is used to update a single document within the collection based on the filter.Let us create a collection with documents −> db.demo495.insertOne({"FirstName":"Chris", "Age":19});{    "acknowledged" : true,    "insertedId" : ObjectId("5e84adfeb0f3fa88e22790ca") } > db.demo495.insertOne({"FirstName":"David", "Age":21});{    "acknowledged" : true,    "insertedId" : ObjectId("5e84ae05b0f3fa88e22790cb") } > db.demo495.insertOne({"FirstName":"Bob", "Age":26});{    "acknowledged" : true,    "insertedId" : ObjectId("5e84ae0eb0f3fa88e22790cc") } > db.demo495.insertOne({"FirstName":"John", "Age":22});{    "acknowledged" : true,    "insertedId" : ObjectId("5e84ae15b0f3fa88e22790cd") }Display all documents from a collection with the help of find() method −> db.demo495.find();This will produce ... Read More

Update elements inside an array in MongoDB?

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

2K+ 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

830 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

487 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

4K+ 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

294 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

871 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

3K+ 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

121 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

Advertisements