Found 1359 Articles for MongoDB

Check for duplicates in an array in MongoDB?

AmitDiwan
Updated on 01-Jul-2020 06:46:00

863 Views

To check for duplicates in an array, use aggregate() in MongoDB. Let us create a collection with documents −> db.demo756.insertOne({"SubjectName":["MySQL", "MongoDB", "Java"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5eb01e0d5637cd592b2a4add") } > db.demo756.insertOne({"SubjectName":["MongoDB", "MySQL", "MongoDB", "C", "C+", "MySQL"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5eb01e2b5637cd592b2a4ade") }Display all documents from a collection with the help of find() method −> db.demo756.find();This will produce the following output −{ "_id" : ObjectId("5eb01e0d5637cd592b2a4add"), "SubjectName" : [ "MySQL", "MongoDB", "Java" ] } { "_id" : ObjectId("5eb01e2b5637cd592b2a4ade"), "SubjectName" : [ "MongoDB", "MySQL", "MongoDB", "C", "C+", "MySQL" ] }Following is the query to check for ... Read More

Sort MongoDB field which contains both integer and decimal values?

AmitDiwan
Updated on 01-Jul-2020 06:42:25

279 Views

To sort, use sort() in MongoDB. Let us create a collection with documents −> db.demo755.insertOne({"Value":10}); {    "acknowledged" : true,    "insertedId" : ObjectId("5eae9e72a930c785c834e572") } > db.demo755.insertOne({"Value":10.5}); {    "acknowledged" : true,    "insertedId" : ObjectId("5eae9e75a930c785c834e573") } > db.demo755.insertOne({"Value":9.5}); {    "acknowledged" : true,    "insertedId" : ObjectId("5eae9e79a930c785c834e574") } > db.demo755.insertOne({"Value":12.5}); {    "acknowledged" : true,    "insertedId" : ObjectId("5eae9e7fa930c785c834e575") } > db.demo755.insertOne({"Value":11.5}); {    "acknowledged" : true,    "insertedId" : ObjectId("5eae9e87a930c785c834e576") } > db.demo755.insertOne({"Value":6}); {    "acknowledged" : true,    "insertedId" : ObjectId("5eae9e97a930c785c834e577") }Display all documents from a collection with the help of find() method −> db.demo755.find();This will produce ... Read More

How to convert birth date records to age with MongoDB

AmitDiwan
Updated on 01-Jul-2020 06:41:02

1K+ Views

Let us create a collection with documents −> db.demo754.insertOne({"DateOfBirth":new Date("2000-05-03")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5eae9b2da930c785c834e56f") } > db.demo754.insertOne({"DateOfBirth":new Date("2010-01-21")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5eae9b34a930c785c834e570") } > db.demo754.insertOne({"DateOfBirth":new Date("2018-05-03")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5eae9b3da930c785c834e571") }Display all documents from a collection with the help of find() method −> db.demo754.find();This will produce the following output −{ "_id" : ObjectId("5eae9b2da930c785c834e56f"), "DateOfBirth" : ISODate("2000-05-03T00:00:00Z") } { "_id" : ObjectId("5eae9b34a930c785c834e570"), "DateOfBirth" : ISODate("2010-01-21T00:00:00Z") } { "_id" : ObjectId("5eae9b3da930c785c834e571"), "DateOfBirth" : ISODate("2018-05-03T00:00:00Z") }Following is the query to convert date of birth to age −> db.demo754.aggregate( ... Read More

Filter specific values from a MongoDB document

AmitDiwan
Updated on 30-Jun-2020 08:15:02

439 Views

To filter specific values, use $filter in MongoDB. Let us create a collection with documents −> db.demo751.insertOne( ...    { ...       _id: 101, ...       details: [ ...          { Name: "Robert", id:110, Age:21}, ...          { Name: "Rae", id:110, Age:22}, ...          {Name: "Ralph", id:116, Age:23} ...       ] ...    } ... ); { "acknowledged" : true, "insertedId" : 101 }Display all documents from a collection with the help of find() method −> db.demo751.find().pretty();This will produce the following output −{    "_id" ... Read More

How to get max values for distinct elements in MongoDB

AmitDiwan
Updated on 30-Jun-2020 08:12:49

556 Views

To get max values for distinct elements, use $sort and $group in MongoDB aggregate(). Let us create a collection with documents −> db.demo750.insertOne({id:101, value:50}); {    "acknowledged" : true,    "insertedId" : ObjectId("5eae74b2a930c785c834e566") } > db.demo750.insertOne({id:102, value:40}); {    "acknowledged" : true,    "insertedId" : ObjectId("5eae74c8a930c785c834e567") } > db.demo750.insertOne({id:101, value:110}); {    "acknowledged" : true,    "insertedId" : ObjectId("5eae74dba930c785c834e568") }Display all documents from a collection with the help of find() method −> db.demo750.find();This will produce the following output −{ "_id" : ObjectId("5eae74b2a930c785c834e566"), "id" : 101, "value" : 50 } { "_id" : ObjectId("5eae74c8a930c785c834e567"), "id" : 102, "value" : 40 } ... Read More

MongoDB query to update each field of documents in collection with a formula?

AmitDiwan
Updated on 30-Jun-2020 08:11:21

157 Views

To update each field of documents in collection with a formula, use MongoDB update(). Let us create a collection with documents −> db.demo749.insertOne({"details":[{"id":1, a:10}, {"id":2, a:5}, {"id":3, a:20}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5eae6fb0a930c785c834e565") }Display all documents from a collection with the help of find() method −> db.demo749.find().pretty();This will produce the following output −{    "_id" : ObjectId("5eae6fb0a930c785c834e565"),    "details" : [       {          "id" : 1,          "a" : 10       },       {          "id" : 2,     ... Read More

What is the maximum size of a document in MongoDB?

AmitDiwan
Updated on 30-Jun-2020 08:08:46

324 Views

The document is a record in a collection. Each document has the limitation of 16 MB size. The document is wrapped inside the curly bracket ({}).Let us create a collection with documents −> db.demo748.insertOne({_id:101, Name:"Chris", Age:21}); { "acknowledged" : true, "insertedId" : 101 } > db.demo748.insertOne({_id:102, Name:"Bob", Age:20}); { "acknowledged" : true, "insertedId" : 102 } > db.demo748.insertOne({_id:103, Name:"David", Age:23}); { "acknowledged" : true, "insertedId" : 103 } > db.demo748.insertOne({_id:104, Name:"Sam", Age:19}); { "acknowledged" : true, "insertedId" : 104 }Display all documents from a collection with the help of find() method −> db.demo748.find();This will produce the following output −{ "_id" ... Read More

Pushing values into array with multi field set to TRUE?

AmitDiwan
Updated on 30-Jun-2020 08:07:21

80 Views

To push values, use $push along with update() with multi field set to TRUE. Let us create a collection with documents −> db.demo747.insertOne({"CountryName":["US", "IND"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5eae6a50a930c785c834e55f") } > db.demo747.insertOne({"CountryName":["UK", "US"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5eae6a57a930c785c834e560") } > db.demo747.insertOne({"CountryName":["UK", "IND"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5eae6a60a930c785c834e561") }Display all documents from a collection with the help of find() method −> db.demo747.find();This will produce the following output −{ "_id" : ObjectId("5eae6a50a930c785c834e55f"), "CountryName" : [ "US", "IND" ] } { "_id" : ObjectId("5eae6a57a930c785c834e560"), "CountryName" : [ "UK", "US" ] } ... Read More

Find posts that are older than current date in MongoDB?

AmitDiwan
Updated on 30-Jun-2020 08:06:11

1K+ Views

To find posts older than current date in MongoDB, use $lte. Let us create a collection with documents −> db.demo746.insertOne({DueDate:new Date("2020-01-10")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5eae67eca930c785c834e55b") } > db.demo746.insertOne({DueDate:new Date("2020-10-10")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5eae67eda930c785c834e55c") } > db.demo746.insertOne({DueDate:new Date("2020-03-05")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5eae67eea930c785c834e55d") } > db.demo746.insertOne({DueDate:new Date("2020-05-04")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5eae67f1a930c785c834e55e") }Display all documents from a collection with the help of find() method −> db.demo746.find();This will produce the following output −{ "_id" : ObjectId("5eae67eca930c785c834e55b"), "DueDate" : ISODate("2020-01-10T00:00:00Z") } { "_id" : ObjectId("5eae67eda930c785c834e55c"), ... Read More

Concatenate with condition in MongoDB?

AmitDiwan
Updated on 30-Jun-2020 08:04:42

266 Views

To concatenate with condition in MongoDB, use $cond and in that, work with $concat. Let us create a collection with documents −> db.demo745.insertOne({Value1:"100", Value2:"100"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5eae6419a930c785c834e554") } > db.demo745.insertOne({Value1:"40", Value2:"50"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5eae6421a930c785c834e555") } > db.demo745.insertOne({Value1:"13", Value2:"45"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5eae6429a930c785c834e556") }Display all documents from a collection with the help of find() method −> db.demo745.find();This will produce the following output −{ "_id" : ObjectId("5eae6419a930c785c834e554"), "Value1" : "100", "Value2" : "100" } { "_id" : ObjectId("5eae6421a930c785c834e555"), "Value1" : "40", "Value2" : "50" } ... Read More

Advertisements