Found 1359 Articles for MongoDB

How to count a cursor’s iteration in MongoDB?

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

135 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:[ ...             {Name:"Chris"} ...          ] ...       } ...    } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5eab0cce43417811278f5890") } > > > db.demo724.insertOne( ... { ... ... } ... ); {    "acknowledged" : true, ... Read More

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

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

599 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"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5eab095f43417811278f588c") }Display all documents from a collection with the help of find() method −> db.demo723.find();This will produce the following output −{ "_id" : ObjectId("5eab094d43417811278f588a"), "Subject" : [ "MySQL", "MongoDB" ] } { "_id" : ObjectId("5eab095243417811278f588b"), "Subject" : [ "C" ] } { "_id" : ... Read More

Ignore null values in MongoDB documents

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

2K+ 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"}, ...          {Name:null}, ...          {Name:"Carol"} ...       ] ...    } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5eab07d543417811278f5889") }Display all documents from a collection with the help of find() method −> db.demo722.find();This will produce the following output ... Read More

Count by multiple fields with MongoDB aggregation

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

673 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": { ...          "id":101 ... ...       }, ...       "details2": { ...          "id":101 ...       }, ...       "details3": { ...          "id":101 ...       } ...    } ... ); {    "acknowledged" : ... Read More

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

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

204 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" : ObjectId("5eaae7d143417811278f5885") } > db.demo720.insertOne({"SubjectName":"C++"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5eaae7d543417811278f5886") }Display all documents from a collection with the help of find() method −> db.demo720.find();This will produce the following output −{ "_id" : ObjectId("5eaae7ca43417811278f5883"), "SubjectName" : "MySQL" } { "_id" : ObjectId("5eaae7ce43417811278f5884"), "SubjectName" : "Java" } { ... Read More

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

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

357 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" : [ ...             { ...                "ProductId" :"102", ...                "ProductPrice" : NumberInt(500) ...             }, ...             { ...                "ProductId" :"103", ...     ... Read More

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

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

263 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" : true, "insertedId" : ObjectId("5e988be0f6b89257f5584d8b") } > db.demo613.insertOne({"Name":"Chris"});{    "acknowledged" : true, "insertedId" : ObjectId("5e988be2f6b89257f5584d8c") } > db.demo613.insertOne({"Name":"Sam"});{    "acknowledged" : true, "insertedId" : ObjectId("5e988be8f6b89257f5584d8d") }Display all documents from a collection with the help of find() method −> db.demo613.find();This will produce the following output −{ "_id" : ObjectId("5e988bd4f6b89257f5584d88"), "Name" : ... Read More

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

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

192 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 collection with the help of find() method −> db.demo612.find().pretty();This will produce the following output −{    "_id" : ObjectId("5e987372f6b89257f5584d87"),    "id" : 1,    "Info" : [       {          "Name" : "Chris",          "Age" : 21       },   ... Read More

How can I sort documents in MongoDB 4 and display only a single field?

AmitDiwan
Updated on 15-May-2020 08:07:27

95 Views

To sort documents in MongoDB 4, use sort(). To show only a single field, which is sorted, set it to 1.Let us create a collection with documents −> db.demo611.insertOne({"Name":"Chris"});{    "acknowledged" : true, "insertedId" : ObjectId("5e987110f6b89257f5584d83") } > db.demo611.insertOne({"Name":"Adam"});{    "acknowledged" : true, "insertedId" : ObjectId("5e987115f6b89257f5584d84") } > db.demo611.insertOne({"Name":"John"});{    "acknowledged" : true, "insertedId" : ObjectId("5e987118f6b89257f5584d85") } > db.demo611.insertOne({"Name":"Bob"});{    "acknowledged" : true, "insertedId" : ObjectId("5e98711bf6b89257f5584d86") }Display all documents from a collection with the help of find() method −> db.demo611.find(); This will produce the following output: { "_id" : ObjectId("5e987110f6b89257f5584d83"), "Name" : "Chris" } { "_id" : ObjectId("5e987115f6b89257f5584d84"), "Name" : ... Read More

How to add together a subset of elements of an array in MongoDB aggregation?

AmitDiwan
Updated on 15-May-2020 08:05:57

158 Views

To add together a subset of elements of an array, use $first along with $sum. Let us create a collection with documents −> db.demo610.insertOne({Values:[10, 20, 30, 40, 50]});{    "acknowledged" : true, "insertedId" : ObjectId("5e9747b8f57d0dc0b182d62e") }Display all documents from a collection with the help of find() method −> db.demo610.find().pretty()This will produce the following output −{    "_id" : ObjectId("5e9747b8f57d0dc0b182d62e"),    "Values" : [       10,       20,       30,       40,       50    ] }Here is the query to add together a subset of elements of an array in ... Read More

Previous 1 ... 4 5 6 7 8 ... 136 Next
Advertisements