AmitDiwan has Published 10740 Articles

Count the documents with a field value beginning with 13

AmitDiwan

AmitDiwan

Updated on 14-May-2020 08:39:01

111 Views

To count the documents, use $count. For values beginning with 13, use $regex. You can use $regex. Let us create a collection with documents −> db.demo570.insertOne({Information:{Value:"13675"}});{    "acknowledged" : true, "insertedId" : ObjectId("5e90959b39cfeaaf0b97b583") } > db.demo570.insertOne({Information:{Value:"14135"}});{    "acknowledged" : true, "insertedId" : ObjectId("5e9095a739cfeaaf0b97b584") } > db.demo570.insertOne({Information:{Value:"15113"}});{    "acknowledged" : true, ... Read More

MongoDB query to match documents whose _id is in an array as part of a subdocument?

AmitDiwan

AmitDiwan

Updated on 14-May-2020 08:37:07

195 Views

Let us create a collection with documents −> db.demo568.insertOne({ _id: 101, details: [ {id : 101 }, { id:103 } ] }); { "acknowledged" : true, "insertedId" : 101 }Display all documents from a collection with the help of find() method −> db.demo568.find();This will produce the following output −{ "_id" ... Read More

MongoDB collection query to exclude some fields in find()?

AmitDiwan

AmitDiwan

Updated on 14-May-2020 08:35:46

10K+ Views

Set the fields you don’t want to include as 0 as in the below syntax. Here, we have set fields “yourFieldName1” and “yourFieldName2” as 0 −db.yourCollectionName.find(yourQuery, {yourFieldName1:0, yourFieldName2:0});To understand the above syntax, let us create a collection with documents −> db.demo567.insertOne({"Name":"Chris", Age:21});{    "acknowledged" : true, "insertedId" : ObjectId("5e908fa139cfeaaf0b97b57b") } ... Read More

Grouping the array items in MongoDB and get the count the products with similar price?

AmitDiwan

AmitDiwan

Updated on 14-May-2020 08:33:48

947 Views

To group the array items, use $group along with $sort. Let us create a collection with documents −> db.demo566.insertOne( ... { ... ...    "ProductInformation" : [ ...       { ...          "ProductName" : "Product-1", ...          "ProductPrice" :100 ...     ... Read More

MongoDB - how can I access fields in a document?

AmitDiwan

AmitDiwan

Updated on 14-May-2020 08:31:37

404 Views

To access fields in a document, simply use find(). Let us create a collection with documents −> db.demo565.insertOne( ... { ...    id:101, ...    Name:"David", ...    "CountryName":"US" ... } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e90896739cfeaaf0b97b577") } > > db.demo565.insertOne( ... { ...   ... Read More

Make MongoDB replace single array value with string?

AmitDiwan

AmitDiwan

Updated on 14-May-2020 08:29:49

718 Views

To replace, use $set and positional($) operator. Let us create a collection with documents −> db.demo564.insertOne({"StudentName":["Chris", "David", "Mike", "Sam"]});{    "acknowledged" : true, "insertedId" : ObjectId("5e90880a39cfeaaf0b97b576") }Display all documents from a collection with the help of find() method −> db.demo564.find().pretty();This will produce the following output −{    "_id" : ObjectId("5e90880a39cfeaaf0b97b576"), ... Read More

How to get items with a specific value from documents using MongoDB shell?

AmitDiwan

AmitDiwan

Updated on 14-May-2020 08:28:12

499 Views

To get items with a specific value, simply use find(). Let us create a collection with documents −> db.demo563.insertOne({"Name":"Chris", "Age":21, "isMarried":true}){    "acknowledged" : true, "insertedId" : ObjectId("5e8f546c54b4472ed3e8e878") } > db.demo563.insertOne({"Name":"Bob", "Age":23, "isMarried":false}){    "acknowledged" : true, "insertedId" : ObjectId("5e8f547854b4472ed3e8e879") } > db.demo563.insertOne({"Name":"Carol", "Age":23, "isMarried":true}){    "acknowledged" : true, "insertedId" ... Read More

How to search for a record (field) and then delete it in MongoDB?

AmitDiwan

AmitDiwan

Updated on 14-May-2020 08:26:23

495 Views

To search for a field, use $exists and to delete it, use $unset. The $unset operator in MongoDB deletes a particular field.Let us create a collection with documents −> db.demo562.insertOne({"Name":"Chris", "Age":21});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8f4ae854b4472ed3e8e872") } > db.demo562.insertOne({"Age":20});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8f4ae954b4472ed3e8e873") ... Read More

Randomizing unique data with MongoDB and placing values for emailid with wordJohn in the beginning

AmitDiwan

AmitDiwan

Updated on 14-May-2020 08:21:48

113 Views

To randomize unique data, use Math.random() in MongoDB. Let us create a collection with documents −> db.demo561.insertOne({EmailId:null});{    "acknowledged" : true, "insertedId" : ObjectId("5e8f490454b4472ed3e8e86c") } > db.demo561.insertOne({EmailId:null});{    "acknowledged" : true, "insertedId" : ObjectId("5e8f490654b4472ed3e8e86d") } > db.demo561.insertOne({EmailId:null});{    "acknowledged" : true, "insertedId" : ObjectId("5e8f490a54b4472ed3e8e86e") }Display all documents from a collection ... Read More

Fetch data between two dates and with a specific value in MongoDB. Group and get the sum with count?

AmitDiwan

AmitDiwan

Updated on 14-May-2020 08:17:34

677 Views

To match, use $match in MongoDB and to get data between two dates, use $gte and $lte. Let us create a collection with documents −> db.demo560.insertOne({"value1":40, "value2":40, shippingDate:new ISODate("2020-02-26")});{    "acknowledged" : true, "insertedId" : ObjectId("5e8f3d5254b4472ed3e8e867") } > db.demo560.insertOne({"value1":20, "value2":60, shippingDate:new ISODate("2020-02-26")});{    "acknowledged" : true, "insertedId" : ObjectId("5e8f3d5254b4472ed3e8e868") } ... Read More

Advertisements