AmitDiwan has Published 10744 Articles

Find records on or after a specific date in MongoDB?

AmitDiwan

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

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

AmitDiwan

AmitDiwan

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

769 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,   ... Read More

MongoDB query to get array of nested string?

AmitDiwan

AmitDiwan

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

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

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

AmitDiwan

AmitDiwan

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

118 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-" ...    } ... ); {   ... Read More

MongoDB query to filter by several array elements?

AmitDiwan

AmitDiwan

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

166 Views

To filter by several array elements, use $elemMatch. Let us create a collection with documents −> db.demo87.insertOne( ...    { ...       id:101, ...       "Details": [ ...          { ...             "EmployeeName": "Chris", ...       ... Read More

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

AmitDiwan

AmitDiwan

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

826 Views

For this, use $not along with $in. Let us create a collection with documents −[    {       id: "101",       subjectid: [          "102"       ]    },    {       id: "102",       subjectid: [ ... Read More

How to match multiple criteria inside an array with MongoDB?

AmitDiwan

AmitDiwan

Updated on 30-Mar-2020 08:36:36

1K+ Views

To match multiple criteria inside an array, use aggregate(). Let us create a collection with documents −> db.demo84.insertOne({ ...    "EmployeeDetails": [ ...       {Name: 'John', Salary:45000, isMarried: true}, ...       {Name: 'Chris', Salary:50000, isMarried: false} ...       ] ...    } ... ); ... Read More

MongoDB “$and” operator for subcollection to fetch a document?

AmitDiwan

AmitDiwan

Updated on 30-Mar-2020 08:33:17

472 Views

To fetch a document, use $in, instead of $and in MongoDB. Let us first create a collection with documents −> db.demo83.insertOne( ... { ...    "Details":[ ...       { ...          "Name":"Chris", ...          "Subject":[ ...             ... Read More

How to make a unique field in MongoDB?

AmitDiwan

AmitDiwan

Updated on 30-Mar-2020 08:28:03

1K+ Views

To make a unique field in MongoDB, use unique − true. Let us create a collection with documents −> db.demo82.createIndex({"EmployeeName":1}, {unique:true}); {    "createdCollectionAutomatically" : true,    "numIndexesBefore" : 1,    "numIndexesAfter" : 2,    "ok" : 1 } > db.demo82.insertOne({"EmployeeName":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e2bfb1b71bf0181ecc422a0") ... Read More

Finding a specific item in subdocument with MongoDB?

AmitDiwan

AmitDiwan

Updated on 30-Mar-2020 08:25:06

155 Views

To get a specific item in a sudocument, use the dot(.) notation. Let us create a collection with documents −> db.demo81.insertOne({"StudentDetails":[{"StudentName":"Carol", "StudentSubject":"Java"}, { "StudentName" : "David", "StudentSubject" : "MongoDB" }]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e2bf6ec71bf0181ecc4229d") } > db.demo81.insertOne({"StudentDetails":[{"StudentName":"Mike", "StudentSubject":"Python"}, { "StudentName" : "David", "StudentSubject" : "C" ... Read More

Advertisements