AmitDiwan has Published 10744 Articles

How can I count the documents in an array based on the value of a specific field?

AmitDiwan

AmitDiwan

Updated on 15-May-2020 09:12:54

151 Views

For such match and count, use $match in MongoDB. Let us create a collection with documents −> db.demo726.insertOne( ...    { ...       id:101, ...       "details": [ ...          { ...             Name:"Chris" ... ...     ... Read More

Set filtering conditions for nested array in MongoDB

AmitDiwan

AmitDiwan

Updated on 15-May-2020 09:10:01

1K+ Views

To set filtering conditions, use $filter and $cond in MongoDB aggregate(). The $filter selects a subset of an array to return based on the specified condition. Let us create a collection with documents −> db.demo725.insertOne( ...    { ... ...       "details": { ... ...       ... Read More

How to count a cursor’s iteration in MongoDB?

AmitDiwan

AmitDiwan

Updated on 15-May-2020 09:05:42

199 Views

You need to use custom logic with the help of while loop along with find() cursor. Let us create a collection with documents −> db.demo724.insertOne( ...    { ...       details: ...       { ...          id:101, ...          otherDetails:[ ... Read More

Get count of array elements from a specific field in MongoDB documents?

AmitDiwan

AmitDiwan

Updated on 15-May-2020 09:02:54

772 Views

To count array elements from a specific field, use $size in MongoDB. Let us create a collection with documents −> db.demo723.insertOne({"Subject":["MySQL", "MongoDB"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5eab094d43417811278f588a") } > db.demo723.insertOne({"Subject":["C"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5eab095243417811278f588b") } > db.demo723.insertOne({"Subject":["C++", "Java", "Python"]}); {   ... Read More

Ignore null values in MongoDB documents

AmitDiwan

AmitDiwan

Updated on 15-May-2020 09:01:54

3K+ Views

To ignore null values in MongoDB, use "$ne" : null in aggregate(). Let us create a collection with documents −> db.demo722.insertOne( ...    { ...       id:101, ...       details: [ ...          { Name:""}, ...          { Name: "David"}, ... Read More

Count by multiple fields with MongoDB aggregation

AmitDiwan

AmitDiwan

Updated on 15-May-2020 08:59:09

813 Views

To count by multiple fields, use $facet in MongoDB. The $facet processes multiple aggregation pipelines within a single stage on the same set of input documents. Let us create a collection with documents −> db.demo721.insertOne( ...    { ... ...       "details1": { ...         ... Read More

Update all the values of a field with a specific string in MongoDB?

AmitDiwan

AmitDiwan

Updated on 15-May-2020 08:55:19

262 Views

To update all the values, use update() along with multi:true. Let us create a collection with documents −> db.demo720.insertOne({"SubjectName":"MySQL"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5eaae7ca43417811278f5883") } > db.demo720.insertOne({"SubjectName":"Java"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5eaae7ce43417811278f5884") } > db.demo720.insertOne({"SubjectName":"C"}); {    "acknowledged" : true,    "insertedId" ... Read More

MongoDB query to add a new field and concatenate the price result divided by a specific number in it

AmitDiwan

AmitDiwan

Updated on 15-May-2020 08:52:31

425 Views

To add a new field, use the $addFields in MongoDB. Let us create a collection with documents −> db.demo719.insertOne( ...    { ...       "Number":"7374644", ...       "details" : { ...          "otherDetails" : [ ...             { ... Read More

How can I display only unique record from MongoDB and ignore the duplicates?

AmitDiwan

AmitDiwan

Updated on 15-May-2020 08:11:33

391 Views

To display only unique records, use distinct() in MongoDB. Let us create a collection with documents −> db.demo613.insertOne({"Name":"Chris"});{    "acknowledged" : true, "insertedId" : ObjectId("5e988bd4f6b89257f5584d88") } > db.demo613.insertOne({"Name":"Bob"});{    "acknowledged" : true, "insertedId" : ObjectId("5e988bdbf6b89257f5584d89") } > db.demo613.insertOne({"Name":"Bob"});{    "acknowledged" : true, "insertedId" : ObjectId("5e988bddf6b89257f5584d8a") } > db.demo613.insertOne({"Name":"David"});{    "acknowledged" ... Read More

How to select MongoDB document that does not consist a specific field?

AmitDiwan

AmitDiwan

Updated on 15-May-2020 08:09:00

270 Views

Check for a specific field using MongoDB $exists. If that field does not exist in a document, then you need to display the same document with find().Let us create a collection with documents −> db.demo612.insertOne({id:1, "Info":[{Name:"Chris", Age:21}, {Name:"David"}]});{    "acknowledged" : true, "insertedId" : ObjectId("5e987372f6b89257f5584d87") }Display all documents from a ... Read More

Advertisements