AmitDiwan has Published 10744 Articles

Implement a MongoDB $cond field in a projection pipeline based on the presence or absence of a field?

AmitDiwan

AmitDiwan

Updated on 31-Mar-2020 07:48:46

243 Views

For this, you can use $cond along with $anyElementTrue. NULL values (absence of a field) would evaluate to FALSE. With that, an empty array also returns FALSE with $ anyElementTrue.Let us first create a collection with documents −> db.presenceDemo.insertOne({"StudentName":null}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e06f70c25ddae1f53b621f3") } > ... Read More

How can I search a collection to find a nested value in one of its documents in MongoDB?

AmitDiwan

AmitDiwan

Updated on 31-Mar-2020 07:43:48

114 Views

For this, use double underscore( __) in find(). Let us first create a collection with documents −> db.nestedDemo.insertOne({"Information":{"__StudentName":"John Smith"}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e06f39125ddae1f53b621f0") } > db.nestedDemo.insertOne({"Information":{"__StudentName":"John Doe"}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e06f39e25ddae1f53b621f1") } > db.nestedDemo.insertOne({"Information":{"__StudentName":"Chris Brown"}}); {    "acknowledged" : true, ... Read More

Find MongoDB document with array containing the maximum occurrence of a specific value

AmitDiwan

AmitDiwan

Updated on 31-Mar-2020 07:40:57

220 Views

For this, you can use aggregate(). Let us first create a collection with documents −> db.countOccurrencesDemo.insertOne({"ListOfValues":[65, 87, 89, 65, 67, 87, 87, 87]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e06ef9325ddae1f53b621eb") } > db.countOccurrencesDemo.insertOne({"ListOfValues":[102, 65, 87, 65, 89, 65, 89, 65, 89, 65]}); {    "acknowledged" : true,   ... Read More

Update only a single document in MongoDB

AmitDiwan

AmitDiwan

Updated on 31-Mar-2020 07:38:42

205 Views

To update only a single document in a collection. use updateOne(). Let us first create a collection with documents −> db.updateOneDemo.insertOne({"StudentId":1, "StudentFirstName":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e06ed3725ddae1f53b621e8") } > db.updateOneDemo.insertOne({"StudentId":2, "StudentFirstName":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e06ed3825ddae1f53b621e9") } > db.updateOneDemo.insertOne({"StudentId":1, "StudentFirstName":"Mike"}); {   ... Read More

Fetch specific field values in MongoDB

AmitDiwan

AmitDiwan

Updated on 31-Mar-2020 07:36:50

1K+ Views

To fetch specific field values, use $in operator. The $in selects the documents where the value of a field equals any value in the specified array.Let us first create a collection with documents −> db.indexesDemo.createIndex({"StudentFirstName":1}); {    "createdCollectionAutomatically" : true,    "numIndexesBefore" : 1,    "numIndexesAfter" : 2,    "ok" ... Read More

Appending an entry in one to many embedded documents with MongoDB

AmitDiwan

AmitDiwan

Updated on 30-Mar-2020 14:34:24

341 Views

To append an entry in MongoDB, use $push. Let us create a collection with documents −> db.demo253.insertOne( ...   { ...      _id: "101", ...      isActive: false, ...      details: [ ...         { ...            Name: "Chris", ... ... Read More

Sort array in MongoDB query and project all fields?

AmitDiwan

AmitDiwan

Updated on 30-Mar-2020 14:32:20

685 Views

To sort array, use $sort. For projection, use $project in MongoBD aggregate(). Let us create a collection with documents −> db.demo252.insertOne( ...   {"Values" : [ { "v1" : 20, "v2" :30 }, { "v1" : 20, "v2" : 20 }, { "v1" : 10, "v2" : 7 } ] ... Read More

Perform min/max with MongoDB aggregation

AmitDiwan

AmitDiwan

Updated on 30-Mar-2020 14:28:55

1K+ Views

For min/max in MongoDB, use $min and $max. Let us create a collection with documents −> db.demo251.insertOne({"Marks":78}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e46c0001627c0c63e7dba74") } > db.demo251.insertOne({"Marks":87}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e46c0031627c0c63e7dba75") } > db.demo251.insertOne({"Marks":56}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e46c0061627c0c63e7dba76") ... Read More

Find the MongoDB collection size for name “Chris”

AmitDiwan

AmitDiwan

Updated on 30-Mar-2020 14:20:41

112 Views

For this, use bsonsize() in MongoDB. Let us create a collection with documents −> db.demo250.insertOne({"Name":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e46bd501627c0c63e7dba70") } > db.demo250.insertOne({"Name":"Bob"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e46bd531627c0c63e7dba71") } > db.demo250.insertOne({"Name":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e46bd561627c0c63e7dba72") } > ... Read More

Query BinData by Type in MongoDB

AmitDiwan

AmitDiwan

Updated on 30-Mar-2020 14:10:18

777 Views

To query by type, use subtype() in MongoDB. Let us create a collection with documents −> db.demo249.insertOne({ "_id" : BinData(0, "AQAAAAEBAAVlbl9VSwAAAAAAAAhv") }); {    "acknowledged" : true,    "insertedId" : BinData(0, "AQAAAAEBAAVlbl9VSwAAAAAAAAhv") } > db.demo249.insertOne({"_id" : BinData(4, "CNDF66qIlCY92q1vFAAAAQ==")}); {    "acknowledged" : true,    "insertedId" : UUID("08d0c5eb-aa88-9426-3dda-ad6f14000001") } > db.demo249.insertOne({"_id" ... Read More

Advertisements