AmitDiwan has Published 10744 Articles

How do I return a document with filtered sub-documents using Mongo?

AmitDiwan

AmitDiwan

Updated on 11-May-2020 09:20:20

144 Views

For this, use $project in MongoDB. Within that, use $filter. Let us create a collection with documents −> db.demo457.insertOne( ... { ...    _id: 101, ...    details: [ ...       { ProductName:"Product-1" , ProductPrice:90 }, ...       { ProductName:"Product-2" , ProductPrice:190 } ...    ] ... Read More

How to aggregate two lists if at least one element matches in MongoDB?

AmitDiwan

AmitDiwan

Updated on 11-May-2020 09:17:18

284 Views

For this, use $group in MongoDB. Within that, use $unwind, $group, $addToSet, etc. Let us create a collection with documents −> db.demo456.insertOne( ... { _id: 101, StudentName: ["Chris", "David"] } ... ); { "acknowledged" : true, "insertedId" : 101 } > > db.demo456.insertOne( ... { _id: 102, StudentName: ["Mike", "Sam"] ... Read More

How do I display a list of objects based on a specific property with MongoDB?

AmitDiwan

AmitDiwan

Updated on 11-May-2020 09:16:38

336 Views

To display a list of objects based on a specific property, use dot notation in find(). Let us create a collection with documents −> db.demo455.insertOne({"Information":{"Student":[{"Name":"Chris", "Age":22}]}});{    "acknowledged" : true,    "insertedId" : ObjectId("5e7e1876dbcb9adb296c95c5") } > db.demo455.insertOne({"Information":{"Student":[{"Name":"David", "Age":21}]}});{    "acknowledged" : true,    "insertedId" : ObjectId("5e7e1883dbcb9adb296c95c6") } > db.demo455.insertOne({"Information":{"Student":[{"Name":"Bob", "Age":24}]}});{ ... Read More

How to continuously publish the latest N records with sorting using MongoDB?

AmitDiwan

AmitDiwan

Updated on 11-May-2020 09:14:18

85 Views

To publish the latest N records with sorting, use sort() along with limit(). Here, set the number of records you want to show with limit(). Let us create a collection with documents −> db.demo454.insertOne({"ClientName":"Chris"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e7cce8cdbcb9adb296c95c0") } > db.demo454.insertOne({"ClientName":"John"});{    "acknowledged" : true,   ... Read More

How to filter documents based on an array in MongoDB?

AmitDiwan

AmitDiwan

Updated on 11-May-2020 09:13:33

1K+ Views

To filter documents based on an array, use $elemMatch. The $elemMatch operator matches documents that contain an array field.Let us create a collection with documents −> db.demo453.insertOne( ... { _id: 101, details: [ { Name: "David", Marks: 60 }, { Name: "Mike", Marks: 55} ] } ... ) { "acknowledged" ... Read More

Get the aggregated result and find the count of repeated values in different MongoDBdocuments

AmitDiwan

AmitDiwan

Updated on 11-May-2020 09:11:28

169 Views

To get the count of repeated values in different documents, use aggregate(). Let us create a collection with documents −> db.demo452.insertOne({"StudentName":"John", "StudentAge":21});{    "acknowledged" : true,    "insertedId" : ObjectId("5e7b7e3371f552a0ebb0a6f3") } > db.demo452.insertOne({"StudentName":"John", "StudentAge":22});{    "acknowledged" : true,    "insertedId" : ObjectId("5e7b7e3671f552a0ebb0a6f4") } > db.demo452.insertOne({"StudentName":"John", "StudentAge":23});{    "acknowledged" : true, ... Read More

How to reach subdata in MongoDB and display a particular document?

AmitDiwan

AmitDiwan

Updated on 11-May-2020 09:02:59

164 Views

In order to reach subdata, you need to use key in MongoDB. Let us create a collection with documents −>db.demo450.insertOne({"Information":{"StudentDetails":{"StudentName":"Chris", "StudentAge":21}}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e7b590e71f552a0ebb0a6e6") } >db.demo450.insertOne({"Information":{"StudentDetails":{"StudentName":"David", "StudentAge":23}}});{    "acknowledged" : true,    "insertedId" : ObjectId("5e7b591a71f552a0ebb0a6e7") } >db.demo450.insertOne({"Information":{"StudentDetails":{"StudentName":"Mike", "StudentAge":22}}});{    "acknowledged" : true,    "insertedId" ... Read More

How to insert an item to an array that is inside an object in MongoDB?

AmitDiwan

AmitDiwan

Updated on 11-May-2020 09:02:05

883 Views

To insert an item to an already created array inside an object, use MongoDB $push. Let us create a collection with documents −> db.demo449.insertOne( ... { ...    details1: { ...       details2: [{ ...          _id:new ObjectId(), ...             ... Read More

What is the difference between deleteOne() and findOneAndDelete() operation in MongoDB?

AmitDiwan

AmitDiwan

Updated on 11-May-2020 08:59:12

3K+ Views

The findOneAndDelete() deletes single documents from the collection on the basis of a filter and sort criteria as well as it returns the deleted document.The deleteOne() removes single document from the collection.Let us see an example and create a collection with documents −> db.demo448.insertOne({"Name":"Chris", "Age":21});{    "acknowledged" : true,   ... Read More

How to create a custom scrollbar with CSS?

AmitDiwan

AmitDiwan

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

193 Views

To create a custom scrollbar with CSS, the code is as follows −Example Live Demo    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;       height: 200vh; /*to create a scrollbar*/    }    ::-webkit-scrollbar {       width: 20px; ... Read More

Advertisements