AmitDiwan has Published 10744 Articles

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

657 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

GROUP BY array of document to get the count of repeated Age values

AmitDiwan

AmitDiwan

Updated on 14-May-2020 08:14:51

155 Views

To GROUP BY array of the document, use $group. Let us create a collection with documents −>db.demo559.insertOne({details:[{Name:"Chris", Age:21}, {Name:"Bob", Age:22}, {Name:"Carol", Age:21}, {Name:"Sam", Age:21}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e8f38d954b4472ed3e8e866") }Display all documents from a collection with the help of find() method −> db.demo559.find().pretty();This will produce the ... Read More

MongoDB aggregate to get the count of field values of corresponding duplicate names?

AmitDiwan

AmitDiwan

Updated on 14-May-2020 08:11:54

1K+ Views

Let us see an example and create a collection with documents −> db.demo558.insertOne( ... { ...    _id : 100, ...    CountryCode:101, ...    details: [ ...       { ...          Name:"Chris", ...          Subject:"MySQL" ...       }, ... ... Read More

How to run MongoDB query to update only a specific field value?

AmitDiwan

AmitDiwan

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

221 Views

Let us see an example and create a collection with documents −> db.demo557.insertOne({Name:"Chris"});{    "acknowledged" : true, "insertedId" : ObjectId("5e8f28e954b4472ed3e8e864") } > db.demo557.insertOne({Name:"David"});{    "acknowledged" : true, "insertedId" : ObjectId("5e8f28ee54b4472ed3e8e865") }Display all documents from a collection with the help of find() method −> db.demo557.find();This will produce the following output −{ ... Read More

Fetch records from a subdocument array wherein id begins from 234 in MongoDB

AmitDiwan

AmitDiwan

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

114 Views

To fetch records from a subdocument array, use $unwind along with $push. For ids beginning from 234, use regex in MongoDB.Let us create a collection with documents −> db.demo556.insertOne( ... { ...    _id:101, ...    details:[ ...       { ...          id:"234336", ...   ... Read More

MongoDB query (aggregation framework) to match a specific field value

AmitDiwan

AmitDiwan

Updated on 14-May-2020 08:04:09

312 Views

To match a specific field value, use a $match in MongoDB aggregation. Let us create a collection with documents −> db.demo555.insertOne({"CountryName":"US"});{    "acknowledged" : true, "insertedId" : ObjectId("5e8f21bf54b4472ed3e8e85f") } > db.demo555.insertOne({"CountryName":"UK"});{    "acknowledged" : true, "insertedId" : ObjectId("5e8f21c254b4472ed3e8e860") } > db.demo555.insertOne({"CountryName":"US"});{    "acknowledged" : true, "insertedId" : ObjectId("5e8f21c354b4472ed3e8e861") } > ... Read More

Query MongoDB with “like” implementation on name and email field beginning with a specific letter?

AmitDiwan

AmitDiwan

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

915 Views

For “like” implementation in MongoDB, use / / and set that specific letter in between. For example −/J/Let us create a collection with documents −> db.demo554.insertOne({"UserName":"John", "UserMailId":"John@gmail.com"});{    "acknowledged" : true, "insertedId" : ObjectId("5e8f1cfed1d72c4545cb8679") } > db.demo554.insertOne({"UserName":"Chris", "UserMailId":"Chris@gmail.com"});{    "acknowledged" : true, "insertedId" : ObjectId("5e8f1d0cd1d72c4545cb867a") } > db.demo554.insertOne({"UserName":"Jace", "UserMailId":"Jace@gmail.com"});{   ... Read More

MongoDB syntax for updating an object inside an array within a document?

AmitDiwan

AmitDiwan

Updated on 14-May-2020 07:59:43

3K+ Views

For this, use findOneAndUpdate() in MongoDB. The findOneAndUpdate() method updates a single document based on the filter and sort criteria.Let us create a collection with documents −> db.demo553.insertOne( ... { ...    id:101, ...    "Name":"John", ...    midExamDetails: ...    [ ...       {"SubjectName":"MySQL", "Marks":70}, ...   ... Read More

How to select documents with values above the average in MongoDB?

AmitDiwan

AmitDiwan

Updated on 14-May-2020 07:56:34

463 Views

Use aggregate() in MongoDB to select documents with values above the average. To find the average, use $avg in MongoDB.Let us create a collection with documents −> db.demo552.insertOne({values:10});{    "acknowledged" : true, "insertedId" : ObjectId("5e8e3b1c9e5f92834d7f05ea") } > db.demo552.insertOne({values:50});{    "acknowledged" : true, "insertedId" : ObjectId("5e8e3b1f9e5f92834d7f05eb") } > db.demo552.insertOne({values:40});{    "acknowledged" ... Read More

MongoDB multiple OR conditions on same key?

AmitDiwan

AmitDiwan

Updated on 14-May-2020 07:54:25

2K+ Views

For this, simply use $or once. Let us create a collection with documents −> db.demo551.insertOne({"Name":"John"});{    "acknowledged" : true, "insertedId" : ObjectId("5e8e36d39e5f92834d7f05e5") } > db.demo551.insertOne({"Name":"Chris Brown"});{    "acknowledged" : true, "insertedId" : ObjectId("5e8e36d89e5f92834d7f05e6") } > db.demo551.insertOne({"Name":"John Doe"});{    "acknowledged" : true, "insertedId" : ObjectId("5e8e36de9e5f92834d7f05e7") } > db.demo551.insertOne({"Name":"John Smith"});{    "acknowledged" ... Read More

Advertisements