Found 1659 Articles for Big Data Analytics

How to display only the keys from nested MongoDB documents?

AmitDiwan
Updated on 30-Jun-2020 07:55:23

450 Views

Let us create a collection with documents −> db.demo740.insertOne({ ...    "details": ...    [ ...       { ...          Name:"Chris", ...          Age:21, ...          CountryName:"US" ...       }, ...       { ...          Name:"Bob", ...          Age:20, ...          CountryName:"UK", ...          isMarried:true ...       } ...    ] ... }); {    "acknowledged" : true,    "insertedId" : ObjectId("5ead700c57bb72a10bcf066d") }Display all documents from a collection with the help ... Read More

How to push value with for loop in MongoDB?

AmitDiwan
Updated on 30-Jun-2020 07:52:34

853 Views

To push value, use save() along with for loop. Let us create a collection with documents −> for(var v=1; v db.demo739.find();This will produce the following output −{ "_id" : ObjectId("5ead6e7857bb72a10bcf0666"), "Name" : "Chris", "SubjectName" : "MongoDB" } { "_id" : ObjectId("5ead6e7857bb72a10bcf0667"), "Name" : "Chris", "SubjectName" : "MongoDB" } { "_id" : ObjectId("5ead6e7857bb72a10bcf0668"), "Name" : "Chris", "SubjectName" : "MongoDB" } { "_id" : ObjectId("5ead6e7857bb72a10bcf0669"), "Name" : "Chris", "SubjectName" : "MongoDB" } { "_id" : ObjectId("5ead6e7857bb72a10bcf066a"), "Name" : "Chris", "SubjectName" : "MongoDB" } { "_id" : ObjectId("5ead6e7857bb72a10bcf066b"), "Name" : "Chris", "SubjectName" : "MongoDB" }

Work with $push in MongoDB

AmitDiwan
Updated on 30-Jun-2020 07:51:10

137 Views

Let us create a collection with documents −> db.demo738.insertOne({Subjects:["C", "C++"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ead696557bb72a10bcf0661") } > db.demo738.insertOne({Subjects:["MySQL", "PL/SQL"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ead696657bb72a10bcf0662") }Display all documents from a collection with the help of find() method −> db.demo738.find();This will produce the following output −{ "_id" : ObjectId("5ead696557bb72a10bcf0661"), "Subjects" : [ "C", "C++" ] } { "_id" : ObjectId("5ead696657bb72a10bcf0662"), "Subjects" : [ "MySQL", "PL/SQL" ] }Following is the query to push −>db.demo738.update({_id:ObjectId("5ead696657bb72a10bcf0662")}, {$push:{"Subjects":"MongoDB"}}); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })Display all documents from a collection with the help of ... Read More

How to order by timestamp (descending order) in MongoDB

AmitDiwan
Updated on 30-Jun-2020 07:49:10

2K+ Views

To order by timestamp, use sort() in MongoDB. Let us create a collection with documents −> db.demo737.insertOne({"timestamp" : new ISODate("2020-04-01" )}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ead682157bb72a10bcf065c") } > db.demo737.insertOne({"timestamp" : new ISODate("2020-10-31" )}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ead682757bb72a10bcf065d") } > db.demo737.insertOne({"timestamp" : new ISODate("2020-05-02" )}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ead682a57bb72a10bcf065e") }Display all documents from a collection with the help of find() method −> db.demo737.find();This will produce the following output −{ "_id" : ObjectId("5ead682157bb72a10bcf065c"), "timestamp" : ISODate("2020-04-01T00:00:00Z") } { "_id" : ObjectId("5ead682757bb72a10bcf065d"), "timestamp" : ISODate("2020-10-31T00:00:00Z") } { "_id" : ... Read More

Filter query on array of embedded document with MongoDB?

AmitDiwan
Updated on 30-Jun-2020 07:47:50

258 Views

For this, use aggregate() in MongoDB. Let us create a collection with documents −> db.demo736.insertOne( ...    { ...       "_id": "101", ...       "details1": [ ...          { ...             "details2": [ ...                { ...                   "details3": { ...                      "Name": "John" ...                   } ...                } ... ... Read More

Using MongoDB updateOne() & insertOne()

AmitDiwan
Updated on 30-Jun-2020 07:44:44

763 Views

MongoDB insertOne() inserts a document into a collection, whereas updateOne() update a single document in a collection based on a query filter.Let us create a collection with documents −> db.demo735.insertOne({id:1, Name:"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ead51b657bb72a10bcf0652") } > db.demo735.insertOne({id:1, Name:"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ead51bb57bb72a10bcf0653") } > db.demo735.insertOne({id:1, Name:"Bob"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ead51be57bb72a10bcf0654") } > db.demo735.insertOne({id:1, Name:"Carol"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ead51c757bb72a10bcf0655") }Display all documents from a collection with the help of find() method −> db.demo735.find();This will produce the following output −{ "_id" ... Read More

Difference between “now” and a given date with MongoDB?

AmitDiwan
Updated on 30-Jun-2020 07:42:37

1K+ Views

To get the difference between dates in MongpDB, use aggregate(). Let us create a collection with documents −> db.demo734.insertOne({GivenDate:new ISODate("2020-01-10")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ead4f1a57bb72a10bcf064e") } > db.demo734.insertOne({GivenDate:new ISODate("2020-02-20")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ead4f2157bb72a10bcf064f") } > db.demo734.insertOne({GivenDate:new ISODate("2010-12-01")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ead4f2b57bb72a10bcf0650") } > db.demo734.insertOne({GivenDate:new ISODate("2020-05-01")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ead506f57bb72a10bcf0651") }Display all documents from a collection with the help of find() method −> db.demo734.find();This will produce the following output −{ "_id" : ObjectId("5ead4f1a57bb72a10bcf064e"), "GivenDate" : ISODate("2020-01-10T00:00:00Z") } { "_id" : ObjectId("5ead4f2157bb72a10bcf064f"), "GivenDate" ... Read More

How to move an array of embedded documents up to parent and change key/value with aggregation pipeline?

AmitDiwan
Updated on 15-May-2020 09:26:12

922 Views

Use $replaceRoot in MongoDB aggregation. The $replaceRoot replaces the input document with the specified document. The operation replaces all existing fields in the input document, including the _id field. Let us create a collection with documents −> db.demo733.insertOne( ...    { ...       "SubjectDetails": ...       [ ...          { ...             SubjectName:"MongoDB", ...             "Marks":85 ...          }, ...          { ...             SubjectName:"MySQL", ...             ... Read More

How to add a field with specific datatype (list, object) in an existing MongoDB document?

AmitDiwan
Updated on 15-May-2020 09:23:53

400 Views

You can use $set. Let us create a collection with documents −> db.demo732.insertOne({_id:1, Language:"English"}); { "acknowledged" : true, "insertedId" : 1 } > db.demo732.insertOne({_id:2, Language:"Hindi"}); { "acknowledged" : true, "insertedId" : 2 }Display all documents from a collection with the help of find() method −> db.demo732.find();This will produce the following output −{ "_id" : 1, "Language" : "English" } { "_id" : 2, "Language" : "Hindi" }Following is the query to add a field with specific datatype (list, object) in an existing MongoDB document −> db.demo732.update({_id:1}, ... { $set:{details:{'subjectName':"MongoDB"}, studentDetails:[{Name:"David"}, {CountryName:"US"}]}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : ... Read More

How to display a specific field in array using $project in MongoDB and ignore other fields?

AmitDiwan
Updated on 15-May-2020 09:22:15

741 Views

To display a specific field, use $project along with $unwind. To ignore a field, set to 0. Let us create a collection with documents −> db.demo731.insertOne({ "ProductInformation": [ { ProductId:"Product-1", ProductPrice:80 }, { ProductId:"Product-2", ProductPrice:45 }, { ProductId:"Product-3", ProductPrice:50 } ] } ); {    "acknowledged" : true,    "insertedId" : ObjectId("5eac5efd56e85a39df5f6341") }Display all documents from a collection with the help of find() method −> db.demo731.find();This will produce the following output −{ "_id" : ObjectId("5eac5efd56e85a39df5f6341"), "ProductInformation" : [ { "ProductId" : "Product-1", "ProductPrice" : 80 }, { "ProductId" : "Product-2", "ProductPrice" : 45 }, { "ProductId" : "Product-3", "ProductPrice" : ... Read More

Advertisements