Found 6702 Articles for Database

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

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

722 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"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e399a209e4f06af55199807") }Display all documents from a collection with the help of find() method −> db.demo184.find();This will produce the following output −{ "_id" : ObjectId("5e3999fb9e4f06af55199805"), "Names" : [ "Chris", "David", "Bob" ] } { "_id" : ObjectId("5e399a0d9e4f06af55199806"), "Names" : [ "Chris", "Mike" ] } { ... Read More

Query MongoDB subdocument to print on one line?

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

224 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", ...         "isAvailable": true ...      }, ...      { ...         "ProductName": "Product-2", ...         "isAvailable": false ...      } ...   ] ...   } ...); { "acknowledged" : true, "insertedId" : "110" }Display all documents from a collection with the ... Read More

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

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"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e398ebf9e4f06af55199804") }Display all documents from a collection with the help of find() method −> db.demo182.find();This will produce the following output −{ "_id" : ObjectId("5e398ea19e4f06af55199802"), "FirstName" : "Chris", "LastName" : null } { "_id" : ObjectId("5e398ead9e4f06af55199803"), "FirstName" : null, "LastName" : "Miller" } { "_id" ... Read More

MongoDB query to search date records using only Month and Day

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

634 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,    "insertedId" : ObjectId("5e398a7d9e4f06af55199800") } > db.demo181.insertOne({"ShippingDate":new ISODate("2020-10-12")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e398a879e4f06af55199801") }Display all documents from a collection with the help of find() method −> db.demo181.find();This will produce the following output −{ "_id" : ObjectId("5e398a699e4f06af551997fe"), "ShippingDate" : ISODate("2020-01-10T00:00:00Z") } { "_id" : ObjectId("5e398a729e4f06af551997ff"), "ShippingDate" : ... Read More

Match MongoDB documents with fields not containing values in array?

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

241 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", "1010"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3988cc9e4f06af551997fd") }Display all documents from a collection with the help of find() method −> db.demo180.find();This will produce the following output −{ "_id" : ObjectId("5e3988a69e4f06af551997fb"), "Scores" : [ "80", "90", "110" ] } { "_id" : ObjectId("5e3988b79e4f06af551997fc"), "Scores" : [ "110", "70", ... Read More

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

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

72 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, ...      "Subject":"MySQL" ...   } ...   ] ...} ...); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3980299e4f06af551997f9") } > db.demo179.insertOne( ...{ ...   "Name":"David", ...   "Details": [ ...   { ...      "Id":103, ...      "Subject":"Java" ...   }, ...   { ...   ... Read More

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

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

267 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")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e397c0f9e4f06af551997f8") }Display all documents from a collection with the help of find() method −> db.demo178.find();This will produce the following output −{ "_id" : ObjectId("5e397bd89e4f06af551997f5"), "DueDate" : ISODate("2019-01-10T06:18:20.474Z") } { "_id" : ObjectId("5e397bf39e4f06af551997f6"), "DueDate" : ISODate("2020-11-10T18:05:11.474Z") } { "_id" : ObjectId("5e397c039e4f06af551997f7"), "DueDate" : ISODate("2020-03-15T07:05:10.474Z") ... Read More

MongoDB query to add a document in an already created collection

AmitDiwan
Updated on 27-Mar-2020 07:36:36

120 Views

To add a document in an already created collection, use $push in MongoDB. Let us create a collection with documents −> db.demo177.insertOne(    { "Id": "101", "details": [         { "StudentName": "Chris",  "Scores": [67, 71, 74], "SubjectName": ["MySQL", "Java"]  },       { "StudentName": "David", "Scores": [89, 98, 45], "SubjectName": ["PL/SQL", "C"] } ]    } ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e384b2b9e4f06af551997f4") }Display all documents from a collection with the help of find() method −> db.demo177.find().pretty();This will produce the following output −{    "_id" : ObjectId("5e384b2b9e4f06af551997f4"),    "Id" : "101",    "details" ... Read More

Get substring in MongoDB aggregate

AmitDiwan
Updated on 27-Mar-2020 07:27:23

502 Views

To get substring, use $substr in MongoDB. Let us create a collection with documents −> db.demo176.insertOne({"ProductName":"PRODUCT-1"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3843a09e4f06af551997ef") } > db.demo176.insertOne({"ProductName":"PRODUCT-102"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3843a69e4f06af551997f0") } > db.demo176.insertOne({"ProductName":"PRODUCT-105"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3843aa9e4f06af551997f1") }Display all documents from a collection with the help of find() method −> db.demo176.find();This will produce the following output −{ "_id" : ObjectId("5e3843a09e4f06af551997ef"), "ProductName" : "PRODUCT-1" } { "_id" : ObjectId("5e3843a69e4f06af551997f0"), "ProductName" : "PRODUCT-102" } { "_id" : ObjectId("5e3843aa9e4f06af551997f1"), "ProductName" : "PRODUCT-105" }Following is the query to get substring in MongoDB ... Read More

How do I use MongoDB to count only collections that match two fields?

AmitDiwan
Updated on 27-Mar-2020 06:52:51

78 Views

To count only collections that match two fields, use count(). Let us create a collection with documents −> db.demo175.insertOne({"EmployeeName":"Bob", "isMarried":"YES"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3840969e4f06af551997e8") } > db.demo175.insertOne({"EmployeeName":"David", "isMarried":"NO"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e38409e9e4f06af551997e9") } > db.demo175.insertOne({"EmployeeName":"Mike", "isMarried":"YES"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3840a79e4f06af551997ea") } > db.demo175.insertOne({"EmployeeName":"Sam", "isMarried":"NO"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3840ae9e4f06af551997eb") }Display all documents from a collection with the help of find() method −> db.demo175.find();This will produce the following output −{ "_id" : ObjectId("5e3840969e4f06af551997e8"), "EmployeeName" : "Bob", "isMarried" : "YES" } { "_id" ... Read More

Advertisements