Found 1661 Articles for Big Data Analytics

MongoDB syntax for updating an object inside an array within a document?

AmitDiwan
Updated on 14-May-2020 07:59:43

3K+ Views

For this, use findOneAndUpdate() in MongoDB. The findOneAndUpdate() method updates a single document based on the filter and sort criteria.Let us create a collection with documents −> db.demo553.insertOne( ... { ...    id:101, ...    "Name":"John", ...    midExamDetails: ...    [ ...       {"SubjectName":"MySQL", "Marks":70}, ...       {"SubjectName":"MongoDB", "Marks":35} ...    ] ... } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e8e3da19e5f92834d7f05ed") }Display all documents from a collection with the help of find() method −> db.demo553.find();This will produce the following output −{ "_id" : ObjectId("5e8e3da19e5f92834d7f05ed"), "id" : 101, "Name" : "John", "midExamDetails" ... Read More

How to select documents with values above the average in MongoDB?

AmitDiwan
Updated on 14-May-2020 07:56:34

463 Views

Use aggregate() in MongoDB to select documents with values above the average. To find the average, use $avg in MongoDB.Let us create a collection with documents −> db.demo552.insertOne({values:10});{    "acknowledged" : true, "insertedId" : ObjectId("5e8e3b1c9e5f92834d7f05ea") } > db.demo552.insertOne({values:50});{    "acknowledged" : true, "insertedId" : ObjectId("5e8e3b1f9e5f92834d7f05eb") } > db.demo552.insertOne({values:40});{    "acknowledged" : true, "insertedId" : ObjectId("5e8e3b289e5f92834d7f05ec") }Display all documents from a collection with the help of find() method −> db.demo552.find();This will produce the following output −{ "_id" : ObjectId("5e8e3b1c9e5f92834d7f05ea"), "values" : 10 } { "_id" : ObjectId("5e8e3b1f9e5f92834d7f05eb"), "values" : 50 } { "_id" : ObjectId("5e8e3b289e5f92834d7f05ec"), "values" : 40 }Following is the ... Read More

MongoDB multiple OR conditions on same key?

AmitDiwan
Updated on 14-May-2020 07:54:25

2K+ Views

For this, simply use $or once. Let us create a collection with documents −> db.demo551.insertOne({"Name":"John"});{    "acknowledged" : true, "insertedId" : ObjectId("5e8e36d39e5f92834d7f05e5") } > db.demo551.insertOne({"Name":"Chris Brown"});{    "acknowledged" : true, "insertedId" : ObjectId("5e8e36d89e5f92834d7f05e6") } > db.demo551.insertOne({"Name":"John Doe"});{    "acknowledged" : true, "insertedId" : ObjectId("5e8e36de9e5f92834d7f05e7") } > db.demo551.insertOne({"Name":"John Smith"});{    "acknowledged" : true, "insertedId" : ObjectId("5e8e36e59e5f92834d7f05e8") } > db.demo551.insertOne({"Name":"Carol"});{    "acknowledged" : true, "insertedId" : ObjectId("5e8e36ec9e5f92834d7f05e9") }Display all documents from a collection with the help of find() method −> db.demo551.find();This will produce the following output −{ "_id" : ObjectId("5e8e36d39e5f92834d7f05e5"), "Name" : "John" } { "_id" : ObjectId("5e8e36d89e5f92834d7f05e6"), "Name" : "Chris Brown" ... Read More

Perform multiple updates with bulk operations and update elements in an array in MongoDB

AmitDiwan
Updated on 14-May-2020 07:52:15

327 Views

For this, use initializeOrderedBulkOp(). It initializes and returns a new Bulk() operations builder for a collection. The builder constructs an ordered list of write operations that MongoDB executes in bulk.Let us create a collection with documents −>db.demo550.insertOne({"Name":"Chris", "details":[{"Marks":49, Result:"fail"}, {"Marks":58, Result:"fail"}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e8e35bd9e5f92834d7f05e4") }Display all documents from a collection with the help of find() method −> db.demo550.find();This will produce the following output −{ "_id" : ObjectId("5e8e35bd9e5f92834d7f05e4"), "Name" : "Chris", "details" : [ { "Marks" : 49, "Result" : "fail" }, { "Marks" : 58, "Result" : "fail" } ] }Following is the query ... Read More

Find a specified amount of records in MongoDB?

AmitDiwan
Updated on 14-May-2020 07:49:38

1K+ Views

To find a specific amount of records, use LIMIT() in MongoDB. The method accepts one number type argument, which is the number of documents that you want to be displayed.Let us create a collection with documents −> db.demo549.insertOne({"Name":"Chris"});{    "acknowledged" : true, "insertedId" : ObjectId("5e8e32889e5f92834d7f05df") } > db.demo549.insertOne({"Name":"David"});{    "acknowledged" : true, "insertedId" : ObjectId("5e8e328c9e5f92834d7f05e0") } > db.demo549.insertOne({"Name":"Bob"});{    "acknowledged" : true, "insertedId" : ObjectId("5e8e328f9e5f92834d7f05e1") } > db.demo549.insertOne({"Name":"John"});{    "acknowledged" : true, "insertedId" : ObjectId("5e8e32929e5f92834d7f05e2") }Display all documents from a collection with the help of find() method −> db.demo549.find();This will produce the following output −{ "_id" : ObjectId("5e8e32889e5f92834d7f05df"), "Name" : ... Read More

Fetch month, day, year, etc. from ISODate in MongoDB?

AmitDiwan
Updated on 14-May-2020 07:13:56

963 Views

The mongo shell provides various methods like ISODate() to return the date, either as a string or as a Date object. ISODate() constructor returns a Date object using the ISODate() wrapper.Let us create a collection with documents −> db.demo548.insertOne({"dueDate":new ISODate("2020-04-09 12:12:40")});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8e30499e5f92834d7f05de") }Display all documents from a collection with the help of find() method −> db.demo548.find();This will produce the following output −{ "_id" : ObjectId("5e8e30499e5f92834d7f05de"), "dueDate" : ISODate("2020-04- 09T12:12:40Z") }Following is the query to display, month, day, week, year, etc. from ISODate −> db.demo548.aggregate( [ { $project: { Year: { $year: "$dueDate" }, ... Read More

MongoDB concurrent update with sub collection?

AmitDiwan
Updated on 14-May-2020 07:10:11

506 Views

For update, simply use update(). Use the $push operator to append a specified value and the dot notation to reach the sub collection and update inside update().Let us create a collection with documents −> db.demo547.insertOne( ... { ...    Name : "Chris", ...    Test : ...    { ...       "FirstTest" : ...       { ...          Scores: [56, 29, 76] ...       }, ...       "SecondTest" : ...       { ...          Scores: [98, 91, 78] ...       } ... ... Read More

Using $redact in MongoDB aggregate?

AmitDiwan
Updated on 14-May-2020 07:07:44

649 Views

The $redact restricts the contents of the documents based on information stored in the documents themselves. You can use $cond along with $redact in aggregate. Let us create a collection with documents −> db.demo546.insertOne({"Value1":10, "Value2":20});{    "acknowledged" : true, "insertedId" : ObjectId("5e8e263f9e5f92834d7f05d7") } > db.demo546.insertOne({"Value1":40, "Value2":30, Value3:50});{    "acknowledged" : true, "insertedId" : ObjectId("5e8e26549e5f92834d7f05d8") } > db.demo546.insertOne({"Value1":100, "Value2":200, Value3:null});{    "acknowledged" : true, "insertedId" : ObjectId("5e8e26619e5f92834d7f05d9") } > db.demo546.insertOne({"Value1":400, "Value2":1000, Value3:null});{    "acknowledged" : true, "insertedId" : ObjectId("5e8e26e09e5f92834d7f05da") } > db.demo546.insertOne({"Value1":400, "Value2":200, Value3:null});{    "acknowledged" : true, "insertedId" : ObjectId("5e8e26f59e5f92834d7f05db") } > db.demo546.insertOne({"Value1":400, "Value2":1000, Value3:60});{    "acknowledged" : true, "insertedId" ... Read More

Implement $match and $project in MongoDB aggregate

AmitDiwan
Updated on 14-May-2020 07:05:36

2K+ Views

The $match filters the documents to pass only the documents that match the specified condition to the next pipeline stage.The $project passes along the documents with the requested fields to the next stage in the pipeline.Let us see an example and create a collection with documents −> db.demo545.insert({Name:"Chris", details:{SubjectScore1:56, SubjectScore2:56}}) WriteResult({ "nInserted" : 1 }) > db.demo545.insert({Name:"David", details:{SubjectScore1:78, SubjectScore2:78}}) WriteResult({ "nInserted" : 1 })Display all documents from a collection with the help of find() method −> db.demo545.find();This will produce the following output −{ "_id" : ObjectId("5e8e246e9e5f92834d7f05d5"), "Name" : "Chris", "details" : { "SubjectScore1" : 56, "SubjectScore2" : 56 } } ... Read More

How to print NumberLong value in MongoDB?

AmitDiwan
Updated on 14-May-2020 07:04:13

680 Views

The mongo shell provides the NumberLong() wrapper to handle 64-bit integers. Following is the syntax using custom variable and print using toString() −var anyVariableName=NumberLong("yourLongNumber"); yourVariableName.toString();To understand the above concept, let us implement the above syntax −> var number=NumberLong("231231231231121231"); > number.toString();This will produce the following output −NumberLong("231231231231121231")The second example is as follows to display NumberLong −> var anotherNumber=NumberLong("765765765765567576"); > anotherNumber.toString();This will produce the following output −NumberLong("765765765765567576")

Advertisements