Found 1659 Articles for Big Data Analytics

How do I $set and $push in single update with MongoDB?

AmitDiwan
Updated on 31-Mar-2020 12:41:11

756 Views

For this, simply use update() to update. Let us create a collection with documents −> db.dem0143.insertOne({"StudentId":1, "Details":{"Name":"Chris"}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e32eb9efdf09dd6d08539b7") } > db.dem0143.insertOne({"StudentId":2, "Details":{"Name":"David"}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e32eba5fdf09dd6d08539b8") }Display all documents from a collection with the help of find() method −> db.dem0143.find();This will produce the following output −{ "_id" : ObjectId("5e32eb9efdf09dd6d08539b7"), "StudentId" : 1, "Details" : { "Name" : "Chris" } } { "_id" : ObjectId("5e32eba5fdf09dd6d08539b8"), "StudentId" : 2, "Details" : { "Name" : "David" } }Following is the query to implement $set and $push in a single update ... Read More

Push query results into variable with MongoDB?

AmitDiwan
Updated on 31-Mar-2020 12:39:38

727 Views

For this, you can use aggregate(). Let us create a collection with documents −> db.demo142.insertOne({"Value":50}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e32e9c6fdf09dd6d08539b2") } > db.demo142.insertOne({"Value":45}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e32e9cafdf09dd6d08539b3") } > db.demo142.insertOne({"Value":60}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e32e9cdfdf09dd6d08539b4") } > db.demo142.insertOne({"Value":55}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e32e9d0fdf09dd6d08539b5") } > db.demo142.insertOne({"Value":50}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e32e9d9fdf09dd6d08539b6") }Display all documents from a collection with the help of find() method −> db.demo142.find();This will produce the following output −{ "_id" : ObjectId("5e32e9c6fdf09dd6d08539b2"), "Value" : 50 } { ... Read More

MongoDB query to find last object in collection?

AmitDiwan
Updated on 31-Mar-2020 12:37:36

5K+ Views

To find last object in collection, at first sort() to sort the values. Use limit() to get number of values i.e. if you want only the last object, then use limit(1).Let us first create a collection with documents −> db.demo141.insertOne({"Name":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e31c347fdf09dd6d08539ae") } > db.demo141.insertOne({"Name":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e31c34bfdf09dd6d08539af") } > db.demo141.insertOne({"Name":"Bob"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e31c34ffdf09dd6d08539b0") } > db.demo141.insertOne({"Name":"Mike"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e31c352fdf09dd6d08539b1") }Display all documents from a collection with the help of find() method −> db.demo141.find();This will ... Read More

MongoDB: $nin and $in not working together in $elemMatch to fetch documents having subjects “MongoDB”, but not “Java”

AmitDiwan
Updated on 31-Mar-2020 12:34:42

218 Views

For such kind of fetching, use only $nin and $in. Let us create a collection with documents −> db.demo140.insertOne({"Id":101, "Subjects":["MongoDB", "MySQL"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e31c149fdf09dd6d08539a9") } > db.demo140.insertOne({"Id":102, "Subjects":["MongoDB", "Java"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e31c14cfdf09dd6d08539aa") } > db.demo140.insertOne({"Id":103, "Subjects":["MongoDB", "PL/SQL"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e31c157fdf09dd6d08539ab") } > db.demo140.insertOne({"Id":104, "Subjects":["MongoDB", "SQL Server"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e31c163fdf09dd6d08539ac") } > db.demo140.insertOne({"Id":105, "Subjects":["C", "C++"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e31c16ffdf09dd6d08539ad") }Display all documents from a collection with the help of find() method −> ... Read More

How to retrieve all nested fields from MongoDB collection?

AmitDiwan
Updated on 31-Mar-2020 12:28:47

790 Views

For this, use aggregate(). Let us create a collection with documents −>db.demo138.insertOne({"Id":101, "PlayerDetails":[{"PlayerName":"Chris", "PlayerScore":400}, {"PlayerName":"David", "PlayerScore":1000}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e31bb9ffdf09dd6d08539a1") } >db.demo138.insertOne({"Id":102, "PlayerDetails":[{"PlayerName":"Bob", "PlayerScore":500}, {"PlayerName":"Carol", "PlayerScore":600}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e31bbcefdf09dd6d08539a2") }Display all documents from a collection with the help of find() method −> db.demo138.find();This will produce the following output −{    "_id" : ObjectId("5e31bb9ffdf09dd6d08539a1"), "Id" : 101, "PlayerDetails" : [       { "PlayerName" : "Chris", "PlayerScore" : 400 },       { "PlayerName" : "David", "PlayerScore" : 1000 }    ] } {    "_id" : ObjectId("5e31bbcefdf09dd6d08539a2"), ... Read More

How to compare two fields in aggregation filter with MongoDB?

AmitDiwan
Updated on 31-Mar-2020 12:26:21

1K+ Views

For this, use aggregate() along with $filter. Let us create a collection with documents −> db.demo137.insertOne( ...    { ...       Name1:"Chris", ...       Name2:"David", ...       Detail1:[ ...          {_id:"John", Name3:"Chris"}, ...          {_id:"Chris", Name3:"Chris"}, ...       ], ...       Detail2:[{_id:"David", Name3:"Chris"}, ...       {_id:"Carol", Name3:"Chris"}, ...       {_id:"John", Name3:"Chris"}] ...    } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e31b4cefdf09dd6d08539a0") }Display all documents from a collection with the help of find() method −> db.demo137.find();This will ... Read More

MongoDB query to sort subdocuments

AmitDiwan
Updated on 31-Mar-2020 12:21:34

136 Views

Let us create a collection with documents −> db.demo136.insertOne( ...    { ... ...       "Name":"Chris", ...       "Details":[ ...          { ...             "Id":"101", ...             "EmployeeName":"Mike", ...             "EmployeeDetails":[ ...             { ...                "EmpId":1001, ...                "Salary":1000 ...             }, ...             { ...         ... Read More

MongoDB query to update a specific document from a collection

AmitDiwan
Updated on 31-Mar-2020 12:08:12

189 Views

To update, use $set along with UPDATE. Let us create a collection with documents −>db.demo135.insertOne({"Details":[{"EmployeeId":101, "EmployeeName":"Chris", "EmployeeSalary":45000}, {"EmployeeId":102, "EmployeeName":"Chris", "EmployeeSalary":45000}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e31a5ddfdf09dd6d085399c") }Display all documents from a collection with the help of find() method −> db.demo135.find().pretty();This will produce the following output −{    "_id" : ObjectId("5e31a5ddfdf09dd6d085399c"),    "Details" : [       {          "EmployeeId" : 101,          "EmployeeName" : "Chris",          "EmployeeSalary" : 45000       },       {          "EmployeeId" : 102,       ... Read More

How to update array of subdocuments in MongoDB?

AmitDiwan
Updated on 31-Mar-2020 12:04:39

2K+ Views

To update, use update() along with $set. Let us create a collection with documents −>db.demo134.insertOne({"EmployeeId":101, "EmployeeDetails":[{"EmployeeName":"Chris", "EmployeeAge":27}, {"EmployeeName":"Bob", "EmployeeAge":28}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e319b2f68e7f832db1a7f7c") } >db.demo134.insertOne({"EmployeeId":102, "EmployeeDetails":[{"EmployeeName":"David", "EmployeeAge":24}, {"EmployeeName":"Carol", "EmployeeAge":29}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e319b4468e7f832db1a7f7d") }Display all documents from a collection with the help of find() method −> db.demo134.find().pretty();This will produce the following output −{    "_id" : ObjectId("5e319b2f68e7f832db1a7f7c"),    "EmployeeId" : 101,    "EmployeeDetails" : [       {          "EmployeeName" : "Chris",          "EmployeeAge" : 27       },       ... Read More

Group by day/month/week based on the date range in MongoDB

AmitDiwan
Updated on 31-Mar-2020 12:01:22

2K+ Views

To group, use $week and $month in MongoDB. Let us create a collection with documents −> db.demo133.insertOne({"Rank":18, "DueDate":new ISODate("2020-01-10")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e31980968e7f832db1a7f78") } > db.demo133.insertOne({"Rank":12, "DueDate":new ISODate("2020-01-10")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e31982568e7f832db1a7f79") } > db.demo133.insertOne({"Rank":12, "DueDate":new ISODate("2020-02-01")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e31986568e7f832db1a7f7a") } > db.demo133.insertOne({"Rank":20, "DueDate":new ISODate("2020-02-01")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e31986c68e7f832db1a7f7b") }Display all documents from a collection with the help of find() method −> db.demo133.find();This will produce the following output −{ "_id" : ObjectId("5e31980968e7f832db1a7f78"), "Rank" : 18, "DueDate" : ISODate("2020-01-10T00:00:00Z") } ... Read More

Advertisements