AmitDiwan has Published 10744 Articles

Implement $dateToString on array items with MongoDB

AmitDiwan

AmitDiwan

Updated on 30-Mar-2020 11:33:48

263 Views

To implement $dateToString on array items, use aggregate() in MongoDB. Let us create a collection with documents −> db.demo104.insertOne( ...    { ... ...       "AppName" : "Online Book", ...       "Details" : [ ...          { ...           ... Read More

Query on the last object of an array with MongoDB

AmitDiwan

AmitDiwan

Updated on 30-Mar-2020 11:30:20

850 Views

To query on the last object of an array, use aggregate(). Let us create a collection with documents −> db.demo103.insertOne( { "Details" : [    { "StudentId" : 101, "Details" : "MongoDB" },    {"StudentId" : 102, "Details" : "MySQL" },    { "StudentId" : 103, "Details" : "Java" } ... Read More

MongoDB Aggregate JSON array field for the matching field of other collection?

AmitDiwan

AmitDiwan

Updated on 30-Mar-2020 11:27:49

597 Views

For this, create two collections and add some document. After that, use $lookup for match. Let us create a collection with documents −> db.demo101.insertOne( ... { "_id" : "1", "Details" : [ { "PId" : "200" }, { "PId" : "201" }, { "PId" : "201" } ] } ... ... Read More

MongoDB query to implement nor query to fetch documents except a specific document

AmitDiwan

AmitDiwan

Updated on 30-Mar-2020 11:25:31

176 Views

To fetch documents except a specific document, set the document to be missed using the $nor in MongoDB. Let us create a collection with documents −> db.demo100.insertOne({"Name":"Chris", "Age":21}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e2d9624b8903cdd865577c0") } > db.demo100.insertOne({"Name":"David", "Age":23}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e2d962cb8903cdd865577c1") ... Read More

How to group nested fields in MongoDB aggregation with count value in array?

AmitDiwan

AmitDiwan

Updated on 30-Mar-2020 11:22:07

1K+ Views

At first, let us create a collection with documents −> db.demo99.insertOne( ...    { ... ...       'Details': ...       { ...          'X': ...          { ...          'Values': [10, 30, 50], ...       ... Read More

Increment a property value of an element in array object with MongoDB

AmitDiwan

AmitDiwan

Updated on 30-Mar-2020 11:10:58

526 Views

To increment a property value of an element, use update() in MongoDB and in that, work with #$inc to increment. Let us first create a collection with documents −> db.demo97.insertOne({ ...    "Details": [ ...       { ...          "Name": "Chris", ...       ... Read More

Find sum of fields inside array in MongoDB?

AmitDiwan

AmitDiwan

Updated on 30-Mar-2020 10:56:46

3K+ Views

To find sum of fields inside array, use $sum. Let us create a collection with documents −> db.demo96.insertOne( ... { ... ...    "Name" : "Chris", ...    "Details" : [ ...       { ...          Marks:67 ...       }, ...     ... Read More

Convert string to objectid in MongoDB?

AmitDiwan

AmitDiwan

Updated on 30-Mar-2020 09:41:02

4K+ Views

To convert string to objectid in MongoDB, use $toObjectId. Let us create a collection with documents −> db.demo95.insertOne({"Id":"5ab9cbe531c2ab715d42129a"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e2d5ef5b8903cdd865577ac") }Display all documents from a collection with the help of find() method −> db.demo95.find();This will produce the following output −{ "_id" : ObjectId("5e2d5ef5b8903cdd865577ac"), ... Read More

How to update array with multiple conditions in MongoDB

AmitDiwan

AmitDiwan

Updated on 30-Mar-2020 09:34:01

739 Views

To update array with multiple conditions, use $push in MongoDB. Let us create a collection with documents −> db.demo94.insertOne( ... { ... ...    "Details" : [ ...       { ...          "Name" : "Chris", ...          "Subject" : [] ...   ... Read More

How to convert date to timestamp in MongoDB

AmitDiwan

AmitDiwan

Updated on 30-Mar-2020 09:28:36

2K+ Views

To convert date to timestamp in MongoDB, use aggregate(). Let us create a collection with documents −> db.demo93.insertOne({"UserName":"Chris", "ArrivalDate":new ISODate("2020-10-01")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e2d4b6479799acab037af68") } > db.demo93.insertOne({"UserName":"David", "ArrivalDate":new ISODate("2019-12-31")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e2d4b7379799acab037af69") }Display all documents from a collection with ... Read More

Advertisements