AmitDiwan has Published 10744 Articles

MongoDB query to group duplicate documents

AmitDiwan

AmitDiwan

Updated on 13-May-2020 06:04:13

417 Views

To group duplicate documents, use MongoDB aggregate(). Let us create a collection with documents −> db.demo501.insertOne({"Name":"Chris"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8752f0987b6e0e9d18f566") } > db.demo501.insertOne({"Name":"Bob"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8752f4987b6e0e9d18f567") } > db.demo501.insertOne({"Name":"Chris"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8752f8987b6e0e9d18f568") } > db.demo501.insertOne({"Name":"John"});{   ... Read More

MongoDB query to select 10 most recent documents without changing order?

AmitDiwan

AmitDiwan

Updated on 13-May-2020 06:01:32

176 Views

For this, use skip() in MongoDB. Under skip(), set “count() – 10” to get 10 most recent documents. Let us create a collection with documents −> db.demo500.insertOne({value:10});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8749c5987b6e0e9d18f55a") } > db.demo500.insertOne({value:1200});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8749c8987b6e0e9d18f55b") } > db.demo500.insertOne({value:19});{   ... Read More

Retrieving group by result with arrays in MongoDB?

AmitDiwan

AmitDiwan

Updated on 13-May-2020 05:49:14

1K+ Views

To retrieve group by result with arrays, use aggregate(). We will also use $addToSet operator. It adds a value to an array unless the value is already present, in which case $addToSet does nothing to that array.Let us create a collection with documents −> db.demo498.insertOne({id:1, Name:["Chris"]});{    "acknowledged" : true, ... Read More

Change date format in MongoDB

AmitDiwan

AmitDiwan

Updated on 13-May-2020 05:42:17

468 Views

We have the following date −01-10-2019To change the date format, let us use custom variable and convert date to a string and change its format −Following is the query to implement date to string −> var inputDate="01-10-2019"; > var formatDate= inputDate.split(/-|\//); > var outputString= formatDate[2]+'-'+formatDate[0]+'-'+formatDate[1];Displaying the variable value −> print(outputString);This ... Read More

How to find if element exists in document - MongoDB?

AmitDiwan

AmitDiwan

Updated on 13-May-2020 05:41:08

571 Views

To find if element exists in a MongoDB document, use MongoDB $exists. Let us create a collection with documents −> db.demo497.insertOne({"details":[{"Name":"Chris"}, {"Name":"Bob"}]});{    "acknowledged" : true,    "insertedId" : ObjectId("5e84b3cfb0f3fa88e22790d1") } > db.demo497.insertOne({"details":[{"Name":"Carol"}]});{    "acknowledged" : true,    "insertedId" : ObjectId("5e84b3d9b0f3fa88e22790d2") } > db.demo497.insertOne({"details":[{}]});{    "acknowledged" : true,    "insertedId" ... Read More

Display the undefined and exact MongoDB document records

AmitDiwan

AmitDiwan

Updated on 13-May-2020 05:38:59

319 Views

For this, use the forEach(). To display the values, use printjson(). Let us create a collection with documents −> db.demo496.insertOne({"Name":"David", "CountryName":"US"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e84b04ab0f3fa88e22790ce") } > db.demo496.insertOne({"Name":"John", "CountryName":"AUS"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e84b054b0f3fa88e22790cf") } > db.demo496.insertOne({"Name":"Robert", "CountryName":"UK"});{    "acknowledged" : true,   ... Read More

Update only a single MongoDB document without deleting any date

AmitDiwan

AmitDiwan

Updated on 13-May-2020 05:36:54

235 Views

To update only a single document, you need to update a specific data with updateOne(). The updateOne() is used to update a single document within the collection based on the filter.Let us create a collection with documents −> db.demo495.insertOne({"FirstName":"Chris", "Age":19});{    "acknowledged" : true,    "insertedId" : ObjectId("5e84adfeb0f3fa88e22790ca") } > ... Read More

Update elements inside an array in MongoDB?

AmitDiwan

AmitDiwan

Updated on 13-May-2020 05:34:05

2K+ Views

To update elements inside an array, use $set in MongoDB. Let us create a collection with documents −> db.demo494.insertOne( ... { ... ...    "CollegeDetails" : [ ...       { ...          "CollegeName" : "MIT", ...          "Fees" : 80000 ...   ... Read More

Count unique items in array-based fields across all MongoDB documents?

AmitDiwan

AmitDiwan

Updated on 13-May-2020 05:29:37

835 Views

To count unique items in array-based fields, use $group along with aggregate(). Let us create a collection with documents −> db.demo493.insertOne({"SubjectName":["MySQL", "MongoDB", "Java"]});{    "acknowledged" : true,    "insertedId" : ObjectId("5e849f97b0f3fa88e22790c4") } > db.demo493.insertOne({"SubjectName":["C++", "MongoDB", "C"]});{    "acknowledged" : true,    "insertedId" : ObjectId("5e849fa4b0f3fa88e22790c5") } > db.demo493.insertOne({"SubjectName":["MySQL", "MongoDB", "C"]});{   ... Read More

Make nested queries in MongoDB 4 to fetch a specific document

AmitDiwan

AmitDiwan

Updated on 13-May-2020 05:27:50

488 Views

For nested queries, let us first create a collection with documents −> db.demo492.insertOne({ ...    "ProductDetails" : ...    { ...       "StockDetails" : [ ...          { "ProductName" : "Product-1" }, ...          {"ProductName" : "Product-2"}, ...         ... Read More

Advertisements