AmitDiwan has Published 10744 Articles

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

AmitDiwan

AmitDiwan

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

329 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") ... Read More

Find a specified amount of records in MongoDB?

AmitDiwan

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" ... Read More

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

AmitDiwan

AmitDiwan

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

966 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 ... Read More

MongoDB concurrent update with sub collection?

AmitDiwan

AmitDiwan

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

507 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 : ...    { ... ... Read More

Using $redact in MongoDB aggregate?

AmitDiwan

AmitDiwan

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

652 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" : ... Read More

Implement $match and $project in MongoDB aggregate

AmitDiwan

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}}) ... Read More

How to print NumberLong value in MongoDB?

AmitDiwan

AmitDiwan

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

682 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 ... Read More

Need to aggregate by hour and $avg in MongoDB

AmitDiwan

AmitDiwan

Updated on 14-May-2020 07:02:42

522 Views

To aggregate, use aggregate() in MongoDB. It calculates aggregate values for the data in a collection.Let us create a collection with documents −> db.demo544.insertOne({"DueTime":new ISODate("2020-01-10 12:10:20"), Amount:100});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8e1f029e5f92834d7f05ce") } > db.demo544.insertOne({"DueTime":new ISODate("2020-01-12 12:00:00"), Amount:500});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8e1f089e5f92834d7f05cf") } ... Read More

Find document that matches same array elements in MongoDB?

AmitDiwan

AmitDiwan

Updated on 14-May-2020 06:56:39

404 Views

To find a document that matches the same array elements, use find() and within that, use $all. The $all operator selects the documents where the value of a field is an array that contains all the specified elements.Let us create a collection with documents −> db.demo543.insertOne({id:101, subject:["MySQL", "Java" ,"C", "Python"]});{ ... Read More

Add a boolean field true to returned objects, when a specified value is in array. For NULLor other values, set false.

AmitDiwan

AmitDiwan

Updated on 14-May-2020 06:54:08

247 Views

For this, use $ifNull. It evaluates an expression and returns the value of the expression if the expression evaluates to a non-null value. Let us first create a collection with documents −> db.demo542.insertOne({"ListOfName":["Chris", "David"]});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8cabc6ef4dcbee04fbbc17") } > db.demo542.insertOne({"ListOfName":null});{    "acknowledged" : true,   ... Read More

Advertisements