Found 6705 Articles for Database

Implement $dateToString on array items with MongoDB

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

261 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" : [ ...          { ...             "ClientName" : "Chris", ...             "Deadline" : new ISODate("2020-03-10") ...          } ...       ] ...    } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e2ed7fd9fd5fd66da21446f") }Display all documents from a collection with the help of ... Read More

Query on the last object of an array with MongoDB

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

849 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" } ], "Details1" : [ { "StudentId" : 104, "Number" : 3 } ] } ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e2ed2dd9fd5fd66da21446e") }Display all documents from a collection with the help of find() method −> db.demo103.find();This will produce the following output −{ "_id" : ObjectId("5e2ed2dd9fd5fd66da21446e"), "Details" : ... Read More

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

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" } ] } ... ) { "acknowledged" : true, "insertedId" : "1" }Display all documents from a collection with the help of find() method −> db.demo101.find();This will produce the following output −{ "_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
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") } > db.demo100.insertOne({"Name":"Bob", "Age":19}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e2d9634b8903cdd865577c2") }Display all documents from a collection with the help of find() method −> db.demo100.find();This will produce the following output −{ "_id" : ObjectId("5e2d9624b8903cdd865577c0"), "Name" : "Chris", "Age" : 21 } { "_id" : ObjectId("5e2d962cb8903cdd865577c1"), "Name" : "David", ... Read More

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

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], ...          'Number':3, ...          }, ...          'Y': ...          { ...             'Values': [1000, 180], ...             'Number': 2, ...          } ...       } ... Read More

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

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

525 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", ...          "Marks": 45 ...       }, ...       { ...          "Name": "Bob", ...          "Marks": 88 ...       }y ...    ] ... } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e2d6d24b8903cdd865577af") }Display all ... Read More

Find sum of fields inside array in MongoDB?

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 ...       }, ...       { ...          Marks:33 ...       }, ...       { ...          Marks:50 ...       } ...    ] ... } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e2d6aadb8903cdd865577ad") }Display all documents from a ... Read More

Convert string to objectid in MongoDB?

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"), "Id" : "5ab9cbe531c2ab715d42129a" }Following is the query to convert string to objectid in MongoDB −> db.demo95.aggregate([ { "$addFields": { "d" : { "$toObjectId": "$Id" } }} ])This will produce the following output −{ "_id" : ObjectId("5e2d5ef5b8903cdd865577ac"), "Id" : "5ab9cbe531c2ab715d42129a", "d" : ObjectId("5ab9cbe531c2ab715d42129a") }Read More

How to update array with multiple conditions in MongoDB

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" : [] ...       }, ...       { ...          "Name" : "David", ...          "Subject" : [] ...       }, ...       { ...          "Name" : "Bob", ...          "Subject" : [] ... Read More

How to convert date to timestamp in MongoDB

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 the help of find() method −> db.demo93.find();This will produce the following output −{ "_id" : ObjectId("5e2d4b6479799acab037af68"), "UserName" : "Chris", "ArrivalDate" : ISODate("2020-10-01T00:00:00Z") } { "_id" : ObjectId("5e2d4b7379799acab037af69"), "UserName" : "David", "ArrivalDate" : ISODate("2019-12-31T00:00:00Z") }Following is the query to convert date to timestamp in MongoDB −> db.demo93.aggregate([ ...    { "$match": ... Read More

Advertisements