Display Undefined and Exact MongoDB Document Records

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

340 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 a Single MongoDB Document Without Deleting Any Data

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

255 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

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

Count Unique Items in Array-Based Fields in MongoDB Documents

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

853 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

506 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

Match Date with MongoDB Match

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

4K+ 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

Check if All Elements of a Field Are Contained in a Superset in MongoDB

AmitDiwan
Updated on 12-May-2020 14:59:26

176 Views

For all elements of a field in MongoDB, use find() and in that, use $elemMatch. The $elemMatch operator matches documents that contain an array field with at least one element that matches all the specified query criteria.Let us create a collection with documents −> db.demo624.insertOne({"ListOfName":["John", "Chris", "David", "Bob"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9ab3ff6c954c74be91e6a5") } > db.demo624.insertOne({"ListOfName":["John", "Chris"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9ab4026c954c74be91e6a6") } > db.demo624.insertOne({"ListOfName":["John", "Chris", "Carol"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9ab4076c954c74be91e6a7") } > db.demo624.insertOne({"ListOfName":["John", "Chris", "Bob"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9ab40e6c954c74be91e6a8") } > ... Read More

Search Array of Objects in MongoDB

AmitDiwan
Updated on 12-May-2020 14:58:03

525 Views

Yes, to search an array of objects, use $unwind in MongoDB aggregate(). To match, use $match. Let us create a collection with documents −> db.demo623.insertOne( ...    { ...       _id:1, ...       details:[ ...          { ...             Name:"Chris" ...          }, ...          { ...             DueDate:new ISODate("2020-01-10") ...          }, ...          { ...             CountryName:"US" ...          } ... ... Read More

Fix Failed to Convert from Type String to Type Date in MongoDB

AmitDiwan
Updated on 12-May-2020 14:57:13

525 Views

To fix this, use $dateFromString in MongoDB aggregate(). The $dateFromString converts a date/time string to a date object.Let us create a collection with documents −> db.demo619.insertOne({"DueDate":"10-10-2020"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e99d7846c954c74be91e69e") } > db.demo619.insertOne({"DueDate":"12-01-2019"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e99d7996c954c74be91e69f") } > db.demo619.insertOne({"DueDate":"28-10-2010"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e99d7ab6c954c74be91e6a0") }Display all documents from a collection with the help of find() method −> db.demo619.find();This will produce the following output −{ "_id" : ObjectId("5e99d7846c954c74be91e69e"), "DueDate" : "10-10-2020" } { "_id" : ObjectId("5e99d7996c954c74be91e69f"), "DueDate" : "12-01-2019" } { "_id" : ObjectId("5e99d7ab6c954c74be91e6a0"), "DueDate" : ... Read More

Remove Document Matching Value in MongoDB Collection

AmitDiwan
Updated on 12-May-2020 14:52:51

306 Views

Remove document using remove(), whose value is matched with $eq from a MongoDB collection. The $eq operator matches documents where the value of a field equals the specified value.Let us create a collection with documents −> db.demo626.insertOne({id:1, "Name":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9ac6376c954c74be91e6ae") } > db.demo626.insertOne({id:2, "Name":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9ac63e6c954c74be91e6af") } > db.demo626.insertOne({id:3, "Name":"Bob"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9ac6436c954c74be91e6b0") } > db.demo626.insertOne({id:4, "Name":"Mike"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9ac6486c954c74be91e6b1") }Display all documents from a collection with the help of find() method −> db.demo626.find();This ... Read More

Advertisements