AmitDiwan has Published 10744 Articles

Find in MongoDB documents with filled nested array and reshape the documents result

AmitDiwan

AmitDiwan

Updated on 27-Mar-2020 08:13:08

187 Views

Let us first create a collection with documents −> db.demo187.insertOne( ...    { ...      "_id" : "101", ...      "Details" : [ ...         { "Subject" : "MongoDB" }, ...         { "Subject" : "MySQL" } ...      ] ... ... Read More

MongoDB query for specific case insensitive search

AmitDiwan

AmitDiwan

Updated on 27-Mar-2020 08:10:28

264 Views

Let us first create a collection with documents −> db.demo186.insertOne({"UserEmailId":"JOHN@GMAIL.COM", "UserName":"John"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e399d769e4f06af55199808") } > db.demo186.insertOne({"UserEmailId":"chris@gmail.com", "UserName":"chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e399d879e4f06af55199809") } > db.demo186.insertOne({"UserEmailId":"DAVID@GMAIL.COM", "UserName":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e399d979e4f06af5519980a") }Display all documents from ... Read More

How to use or operator in MongoDB to fetch records on the basis of existence?

AmitDiwan

AmitDiwan

Updated on 27-Mar-2020 08:07:38

99 Views

To fetch records with $or on the basis of existence, use $or along with $exists. Let us create a collection with documents −>db.demo185.insertOne({_id:101, details:{Name:"Chris", Score:78, Subjects:{"Name":"MySQL"}}}); { "acknowledged" : true, "insertedId" : 101 } > db.demo185.insertOne({_id:102, details:{Name:"Bob", Score:78}}); { "acknowledged" : true, "insertedId" : 102 } >db.demo185.insertOne({_id:103, details:{Name:"David", Score:78, Subjects:{"Name":"MongoDB"}}}); ... Read More

MongoDB query to count the frequency of every element of the array

AmitDiwan

AmitDiwan

Updated on 27-Mar-2020 08:05:48

841 Views

To count, you can also use aggregate() along with $sum. Let us create a collection with documents −> db.demo184.insertOne({"Names":["Chris", "David", "Bob"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3999fb9e4f06af55199805") } > db.demo184.insertOne({"Names":["Chris", "Mike"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e399a0d9e4f06af55199806") } > db.demo184.insertOne({"Names":["Chris", "Bob", "Carol"]}); {   ... Read More

Query MongoDB subdocument to print on one line?

AmitDiwan

AmitDiwan

Updated on 27-Mar-2020 08:02:46

325 Views

To display subdocuments in one line, use $unwind along with aggregate(). Let us create a collection with documents −> db.demo183.insertOne( ... { ...   "_id": "110", ...   "DueDate": ISODate("2020-02-04T01:10:42.000Z"), ...   "ProductDetails": [ ...      { ...         "ProductName": "Product-1", ...         ... Read More

MongoDB query to select one field if the other is null?

AmitDiwan

AmitDiwan

Updated on 27-Mar-2020 07:58:40

1K+ Views

To select one field if the other is null, use $ifNull. Let us create a collection with documents −> db.demo182.insertOne({"FirstName":"Chris", "LastName":null}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e398ea19e4f06af55199802") } > db.demo182.insertOne({"FirstName":null, "LastName":"Miller"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e398ead9e4f06af55199803") } > > db.demo182.insertOne({"FirstName":"John", "LastName":"Smith"}); {   ... Read More

MongoDB query to search date records using only Month and Day

AmitDiwan

AmitDiwan

Updated on 27-Mar-2020 07:56:16

772 Views

To search using month and day only, use $where. Let us create a collection with documents −> db.demo181.insertOne({"ShippingDate":new ISODate("2020-01-10")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e398a699e4f06af551997fe") } > db.demo181.insertOne({"ShippingDate":new ISODate("2019-12-11")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e398a729e4f06af551997ff") } > db.demo181.insertOne({"ShippingDate":new ISODate("2018-01-10")}); {    "acknowledged" : true, ... Read More

Match MongoDB documents with fields not containing values in array?

AmitDiwan

AmitDiwan

Updated on 27-Mar-2020 07:53:35

323 Views

To match documents with fields not containing values in array, use $nin. Let us create a collection with documents −> db.demo180.insertOne({"Scores":["80", "90", "110"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3988a69e4f06af551997fb") } > db.demo180.insertOne({"Scores":["110", "70", "60"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3988b79e4f06af551997fc") } > db.demo180.insertOne({"Scores":["40", "70", ... Read More

How to find latest entries in array over all MongoDB documents?

AmitDiwan

AmitDiwan

Updated on 27-Mar-2020 07:49:57

123 Views

To find latest entries in array over all document, use aggregate(). Let us create a collection with documents −> db.demo179.insertOne( ...{ ...   "Name":"Chris", ...   "Details": [ ...   { ...      "Id":101, ...      "Subject":"MongoDB" ...   }, ...   { ...      "Id":102, ... ... Read More

MongoDB query to fetch date records (ISODate format) in a range

AmitDiwan

AmitDiwan

Updated on 27-Mar-2020 07:40:40

327 Views

Let us create a collection with documents −> db.demo178.insertOne({"DueDate":new ISODate("2019-01-10T06:18:20.474Z")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e397bd89e4f06af551997f5") } > db.demo178.insertOne({"DueDate":new ISODate("2020-11-10T18:05:11.474Z")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e397bf39e4f06af551997f6") } > db.demo178.insertOne({"DueDate":new ISODate("2020-03-15T07:05:10.474Z")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e397c039e4f06af551997f7") } > db.demo178.insertOne({"DueDate":new ISODate("2020-06-11T16:05:10.474Z")}); { ... Read More

Advertisements