Found 1349 Articles for MongoDB

Change date format in MongoDB

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

350 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 will produce the following output −2019-01-10

How to find if element exists in document - MongoDB?

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

455 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" : ObjectId("5e84b3e9b0f3fa88e22790d3") }Display all documents from a collection with the help of find() method −> db.demo497.find();This will produce the following output −{ "_id" : ObjectId("5e84b3cfb0f3fa88e22790d1"), "details" : [ { "Name" : "Chris" }, { "Name" : "Bob" } ] } { "_id" : ObjectId("5e84b3d9b0f3fa88e22790d2"), "details" : [ { "Name" : ... Read More

Display the undefined and exact MongoDB document records

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

280 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,    "insertedId" : ObjectId("5e84b05db0f3fa88e22790d0") }Display all documents from a collection with the help of find() method −> db.demo496.find();This will produce the following output −{ "_id" : ObjectId("5e84b04ab0f3fa88e22790ce"), "Name" : "David", "CountryName" : "US" } { "_id" : ObjectId("5e84b054b0f3fa88e22790cf"), "Name" : "John", "CountryName" : "AUS" } { "_id" : ObjectId("5e84b05db0f3fa88e22790d0"), "Name" : ... Read More

Update only a single MongoDB document without deleting any date

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

172 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") } > db.demo495.insertOne({"FirstName":"David", "Age":21});{    "acknowledged" : true,    "insertedId" : ObjectId("5e84ae05b0f3fa88e22790cb") } > db.demo495.insertOne({"FirstName":"Bob", "Age":26});{    "acknowledged" : true,    "insertedId" : ObjectId("5e84ae0eb0f3fa88e22790cc") } > db.demo495.insertOne({"FirstName":"John", "Age":22});{    "acknowledged" : true,    "insertedId" : ObjectId("5e84ae15b0f3fa88e22790cd") }Display all documents from a collection with the help of find() method −> db.demo495.find();This will produce ... Read More

Update elements inside an array in MongoDB?

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

1K+ 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 ...       }, ...       { ...          "CollegeName" : "SU", ...          "Fees" : 90000 ...       } ...    ] ... } ... ) {    "acknowledged" : true,    "insertedId" : ObjectId("5e84a5c1b0f3fa88e22790c9") }Display all documents from a ... Read More

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

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

673 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"]});{    "acknowledged" : true,    "insertedId" : ObjectId("5e849fb2b0f3fa88e22790c6") }Display all documents from a collection with the help of find() method −> db.demo493.find();This will produce the following output −{ "_id" : ObjectId("5e849f97b0f3fa88e22790c4"), "SubjectName" : [ "MySQL", "MongoDB", "Java" ] } { "_id" : ObjectId("5e849fa4b0f3fa88e22790c5"), "SubjectName" : [ "C++", "MongoDB", "C" ] } ... Read More

Make nested queries in MongoDB 4 to fetch a specific document

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

358 Views

For nested queries, let us first create a collection with documents −> db.demo492.insertOne({ ...    "ProductDetails" : ...    { ...       "StockDetails" : [ ...          { "ProductName" : "Product-1" }, ...          {"ProductName" : "Product-2"}, ...          { "ProductName" : "Product-3"} ... ...       ] ... ...    } ... }); {    "acknowledged" : true,    "insertedId" : ObjectId("5e849db8b0f3fa88e22790c2") } > > > > db.demo492.insertOne({ ...    "ProductDetails" : ...    { ...       "StockDetails" : [ ...         ... Read More

How to match date with MongoDB $match?

AmitDiwan
Updated on 13-May-2020 05:25:24

3K+ Views

To match date, use $match along with aggregate(). Let us create a collection with documents −> db.demo491.insertOne({"ShippingDate":new ISODate("2020-01-10")});{    "acknowledged" : true,    "insertedId" : ObjectId("5e849a09b0f3fa88e22790be") } > db.demo491.insertOne({"ShippingDate":new ISODate("2020-02-21")});{    "acknowledged" : true,    "insertedId" : ObjectId("5e849a0eb0f3fa88e22790bf") } > db.demo491.insertOne({"ShippingDate":new ISODate("2020-03-23")});{    "acknowledged" : true,    "insertedId" : ObjectId("5e849a1db0f3fa88e22790c0") } > db.demo491.insertOne({"ShippingDate":new ISODate()});{    "acknowledged" : true,    "insertedId" : ObjectId("5e849a28b0f3fa88e22790c1") }Display all documents from a collection with the help of find() method −> db.demo491.find();This will produce the following output −{ "_id" : ObjectId("5e849a09b0f3fa88e22790be"), "ShippingDate" : ISODate("2020-01- 10T00:00:00Z") } { "_id" : ObjectId("5e849a0eb0f3fa88e22790bf"), "ShippingDate" : ISODate("2020-02- 21T00:00:00Z") } { ... Read More

MongoDB query to fetch random value using Map Reduce concept.

AmitDiwan
Updated on 12-May-2020 09:02:03

221 Views

For random value with Map Reduce, use mapReduce() concept along with Math.random(). Let us create a collection with documents −> db.demo651.insertOne({Value:10}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9f0330e3c3cd0dcff36a57") } > db.demo651.insertOne({Value:20}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9f0332e3c3cd0dcff36a58") } > db.demo651.insertOne({Value:30}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9f0335e3c3cd0dcff36a59") } > db.demo651.insertOne({Value:40}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9f0337e3c3cd0dcff36a5a") } > db.demo651.insertOne({Value:50}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9f0339e3c3cd0dcff36a5b") } > db.demo651.insertOne({Value:60}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9f033be3c3cd0dcff36a5c") } > db.demo651.insertOne({Value:70}); {    "acknowledged" : true,    "insertedId" ... Read More

Querying only the field name and display only the id in MongoDB?

AmitDiwan
Updated on 12-May-2020 08:57:02

745 Views

To query only the field name, set fieldName to 0 i.e. the fieldName to hide. Let us create a collection with documents −> db.demo650.insertOne({_id:101, details:{Name:"Chris", Age:21}}); { "acknowledged" : true, "insertedId" : 101 } > db.demo650.insertOne({_id:102, details:{Name:"Bob", Age:22}}); { "acknowledged" : true, "insertedId" : 102 } > db.demo650.insertOne({_id:103, details:{Name:"Sam", Age:20}}); { "acknowledged" : true, "insertedId" : 103 } > db.demo650.insertOne({_id:104, details:{Name:"Robert", Age:24}}); { "acknowledged" : true, "insertedId" : 104 }Display all documents from a collection with the help of find() method −> db.demo650.find();This will produce the following output −{ "_id" : 101, "details" : { "Name" : "Chris", "Age" : ... Read More

Advertisements