AmitDiwan has Published 10744 Articles

How to merge multiple documents in MongoDB?

AmitDiwan

AmitDiwan

Updated on 03-Apr-2020 14:22:58

1K+ Views

To merge multiple documents in MongoDB, use aggregate(). Let us create a collection with documents −> db.demo436.insertOne( ...    { ...       "_id" : "101", ...       "Name": "Chris", ...       "details" : [ ...          { ...       ... Read More

How to append to array in MongoDB?

AmitDiwan

AmitDiwan

Updated on 03-Apr-2020 14:20:41

348 Views

To append to array in MongoDB, use $concatArrays. Let us create a collection with documents −> db.demo435.insertOne({"FirstName":["Chris"], "LastName":["Brown"]} ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e7719b1bbc41e36cc3cae97") } > db.demo435.insertOne({"FirstName":["David"], "LastName":["Miller"]} ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e7719bdbbc41e36cc3cae98") } > db.demo435.insertOne({"FirstName":["John"], "LastName":["Doe"]} ); {    "acknowledged" ... Read More

MongoDB aggregation / math operation to sum score of a specific student

AmitDiwan

AmitDiwan

Updated on 03-Apr-2020 14:18:50

268 Views

To sum, use aggregate() along with $sum. Let us create a collection with documents −> db.demo434.insertOne({"Name":"Chris", "Score":45}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e771603bbc41e36cc3cae93") } > db.demo434.insertOne({"Name":"David", "Score":55}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e77161abbc41e36cc3cae94") } > db.demo434.insertOne({"Name":"Chris", "Score":55}); {    "acknowledged" : true,    "insertedId" ... Read More

How to filter a query on specific date format with MongoDB?

AmitDiwan

AmitDiwan

Updated on 03-Apr-2020 14:17:09

2K+ Views

To filter a query on specific date format, use $dateToString. Let us create a collection with documents −> db.demo433.insertOne({"DueDate":new Date("2019-11-23")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e771278bbc41e36cc3cae91") } > db.demo433.insertOne({"DueDate":new Date("2020-01-03")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e771290bbc41e36cc3cae92") }Display all documents from a collection with the ... Read More

Aggregation framework to get the name of students with test one score less than the total average of all the tests

AmitDiwan

AmitDiwan

Updated on 03-Apr-2020 14:15:06

122 Views

For this, you can use aggregate(). We have considered test records as “Value1”, “Value2”, etc. Let us create a collection with documents −> db.demo432.insertOne( ...    { ...       "_id" : 101, ...       "Name" : "David", ...       "Value1" : 67, ...   ... Read More

What is the fastest way to update the whole document (all fields) in MongoDB?

AmitDiwan

AmitDiwan

Updated on 03-Apr-2020 14:10:34

562 Views

The fastest way is to use replaceOne() in MongoDB. Let us create a collection with documents −> db.demo431.insertOne({"Name":"Chris", "Age":32}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e770ba6bbc41e36cc3cae89") } > db.demo431.insertOne({"Name":"David", "Age":31}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e770bacbbc41e36cc3cae8a") } > db.demo431.insertOne({"Name":"John", "Age":24}); {    "acknowledged" : true, ... Read More

MongoDB query to group records and display a specific value with dot notation

AmitDiwan

AmitDiwan

Updated on 03-Apr-2020 14:08:42

229 Views

Let us create a collection with documents −> db.demo430.insertOne( ...    { ...       "details": [ ...          { ...             "Name":"Chris" ...          } , ...          {"Name":"David"}, ...         ... Read More

MongoDB query to create new field and count set the count of another field in it?

AmitDiwan

AmitDiwan

Updated on 03-Apr-2020 14:06:59

645 Views

For new field, use $addFields in MongoDB. The $addFields is used to add new fields to documents. Let us create a collection with documents −> db.demo429.insertOne( ...    { ...       "_id": 101, ...       "Value": 3, ...       "details": [ ...     ... Read More

Conditional upsert (multiple insert) when updating document in MongoDB?

AmitDiwan

AmitDiwan

Updated on 03-Apr-2020 14:02:02

472 Views

For multiple write operations, use bulkWrite() in MongoDB. Let us create a collection with documents −> db.demo428.insertOne({ "Name" : "Chris", "Age" : 21 }); {    "acknowledged" : true,    "insertedId" : ObjectId("5e75f428bbc41e36cc3cae83") } > db.demo428.insertOne({ "Name" : "Chris", "Age" : 23 }); {    "acknowledged" : true,    "insertedId" ... Read More

Update a MongoDB document with Student Id and Name

AmitDiwan

AmitDiwan

Updated on 03-Apr-2020 13:58:49

542 Views

To update, use UPDATE() and $set. Let us create a collection with documents −> db.demo427.insertOne({"StudentId":101, "StudentName":"Chris Brown"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e75e711bbc41e36cc3cae75") } > db.demo427.insertOne({"StudentId":102, "StudentName":"David Miller"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e75e71abbc41e36cc3cae76") } > db.demo427.insertOne({"StudentId":103, "StudentName":"John Smith"}); {    "acknowledged" : true, ... Read More

Advertisements