Found 1659 Articles for Big Data Analytics

Find sum of fields inside array in MongoDB?

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

2K+ 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

3K+ 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

619 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

Find records on or after a specific date in MongoDB?

AmitDiwan
Updated on 30-Mar-2020 09:23:49

2K+ Views

To find records on or after a date, use $gte i.e., greater than equal. Let us create a collection with documents −> db.demo91.insertOne({"ArrivalDate":new ISODate("2020-01-10")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e2d49fd79799acab037af66") } > db.demo91.insertOne({"ArrivalDate":new ISODate("2019-12-14")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e2d4a0679799acab037af67") }Display all documents from a collection with the help of find() method −> db.demo91.find();This will produce the following output −{ "_id" : ObjectId("5e2d49fd79799acab037af66"), "ArrivalDate" : ISODate("2020-01-10T00:00:00Z") } { "_id" : ObjectId("5e2d4a0679799acab037af67"), "ArrivalDate" : ISODate("2019-12-14T00:00:00Z") }Following is the query to find records on or after a specific date in MongoDB −> db.demo91.find({ArrivalDate: { $gte: ISODate('2020-01-10') ... Read More

MongoDB query to find documents whose array contains a string that is a substring of a specific word

AmitDiwan
Updated on 30-Mar-2020 09:16:58

649 Views

For such evaluations, use aggregate() in MongoDB. Let us create a collection with documents −> db.demo90.insertOne( ... {"words": ["john", "jace"] ... } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e2c1ada79799acab037af56") } > db.demo90.insertOne( ... {"words": ["sam", "adam"] ... } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e2c1adb79799acab037af57") }Display all documents from a collection with the help of find() method −> db.demo90.find();This will produce the following output −{ "_id" : ObjectId("5e2c1ada79799acab037af56"), "words" : [ "john", "jace" ] } { "_id" : ObjectId("5e2c1adb79799acab037af57"), "words" : [ "sam", "adam" ] }Following is the query to find documents ... Read More

MongoDB query to get array of nested string?

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

230 Views

To get array of nested string in MongoDB, use dot notation in find(). Let us create a collection with documents −> db.demo89.insertOne( ... { id: 101, Details: [ { Name: "Chris", Marks: 45 }, { Name: "David", Marks: 55, Subjects : ["MySQL", "MongoDB", "Java", "C"] } ] ... } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e2c163b79799acab037af51") } > db.demo89.insertOne( ... { id: 102, Details: [ { Name: "Mike", Marks: 48 }, { Name: "Bob", Marks: 98, Subjects : ["C++", "MySQL"] } ] ... } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e2c163c79799acab037af52") }Display ... Read More

Is it possible to use MongoDB field value as a pattern in $regex?

AmitDiwan
Updated on 30-Mar-2020 08:59:45

66 Views

Yes, you can use $indexOfCP to use a filed value as a pattern. Let us create a collection with documents −> db.demo88.insertOne( ...    { ...       "Name": "Chris", ...       "PassoutYear": "2020", ...       "websiteName": "chris.shop.com/Carol-2020-" ...    } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e2c14c471bf0181ecc422b1") } > db.demo88.insertOne( ...    { ...       "Name": "David", ...       "PassoutYear": "2010", ...       "websiteName": "david.bigshop.com/David-2010-" ...    } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e2c14c571bf0181ecc422b2") }Display all documents from a ... Read More

MongoDB query to filter by several array elements?

AmitDiwan
Updated on 30-Mar-2020 08:57:22

98 Views

To filter by several array elements, use $elemMatch. Let us create a collection with documents −> db.demo87.insertOne( ...    { ...       id:101, ...       "Details": [ ...          { ...             "EmployeeName": "Chris", ...             "Salary": 45000 ...          }, ...          { ...             "EmployeeName": "David", ...             "Salary": 50000 ...       } ...    ] ... } ... ); {    "acknowledged" ... Read More

MongoDB query to exclude if id is equal to a document field array value

AmitDiwan
Updated on 30-Mar-2020 08:50:22

663 Views

For this, use $not along with $in. Let us create a collection with documents −[    {       id: "101",       subjectid: [          "102"       ]    },    {       id: "102",       subjectid: [          "102"       ]    } ]Here is the snapshot.Display all documents from a collection with the help of find() method −db.collection.find()This will produce the following output −[    {       "_id": ObjectId("5a934e000102030405000000"),       "id": "101",       "subjectid": [          "102"       ]    },    {       "_id": ObjectId("5a934e000102030405000001"),       "id": "102",       "subjectid": [          "102"       ]    } ]Following is the query that uses $expr, $not and $in to fetch values excluding matching field array value −db.collection.find({    $expr: {       $not:{          $in: [             "$id",             "$subjectid"          ]       }    } })This will produce the following output −[    {       "_id": ObjectId("5a934e000102030405000000"),       "id": "101",       "subjectid": [          "102"       ]    } ]

Advertisements