Nishtha Thakur

Nishtha Thakur

398 Articles Published

Articles by Nishtha Thakur

Page 8 of 40

MongoDB find() to operate on recursive search?

Nishtha Thakur
Nishtha Thakur
Updated on 15-Mar-2026 902 Views

Use dot notation with find() to perform recursive search within nested arrays and embedded documents. This allows querying deep fields without complex operators. Syntax db.collection.find({"arrayName.fieldName": value}); db.collection.find({"nestedDocument.field": value}); Sample Data db.findOperationDemo.insertMany([ { "ClientDetails": [ {"ClientId": 101, "ClientName": "Chris"}, {"ClientId": 102, "ClientName": "Robert"} ] }, ...

Read More

Get all fields names in a MongoDB collection?

Nishtha Thakur
Nishtha Thakur
Updated on 15-Mar-2026 3K+ Views

To get all field names in a MongoDB collection, you can use Map-Reduce with the distinct() method. This approach extracts all unique field names across all documents in the collection. Syntax db.runCommand({ "mapreduce": "collectionName", "map": function() { for (var key in this) { emit(key, null); } }, "reduce": function(key, values) { return null; }, "out": "temp_collection" }); db.temp_collection.distinct("_id"); Sample Data Let us create a collection ...

Read More

MongoDB regex to display records whose first five letters are uppercase?

Nishtha Thakur
Nishtha Thakur
Updated on 15-Mar-2026 1K+ Views

To find MongoDB documents whose first five letters are uppercase, use the $regex operator with the pattern /^[A-Z]{5}/. The ^ ensures matching from the string beginning, and {5} specifies exactly five consecutive uppercase letters. Syntax db.collection.find({ fieldName: { $regex: /^[A-Z]{5}/ } }); Sample Data db.upperCaseFiveLetterDemo.insertMany([ { "StudentFullName": "JOHN Smith" }, { "StudentFullName": "SAM Williams" }, { "StudentFullName": "CAROL Taylor" }, { "StudentFullName": "Bob Taylor" }, { "StudentFullName": "DAVID Miller" ...

Read More

How to perform $gt on a hash in a MongoDB document?

Nishtha Thakur
Nishtha Thakur
Updated on 15-Mar-2026 162 Views

The $gt operator selects documents where the value of a field is greater than a specified value. When working with embedded documents (hash/object structures), use dot notation to access nested fields within the $gt query. Syntax db.collection.find({ "parentField.nestedField": { $gt: value } }); Sample Data db.performQueryDemo.insertMany([ { "PlayerDetails": { "PlayerScore": 1000, "PlayerLevel": 2 ...

Read More

How can I drop a collection in MongoDB with two dashes in the name?

Nishtha Thakur
Nishtha Thakur
Updated on 15-Mar-2026 481 Views

To drop a collection in MongoDB that has special characters like two dashes in its name, you need to use the getCollection() method instead of direct dot notation, since MongoDB cannot parse collection names with special characters using standard syntax. Syntax db.getCollection("collectionNameWithSpecialCharacters").drop(); Create Sample Collection First, let's create a collection with two dashes in the name ? db.createCollection("company--EmployeeInformation"); { "ok" : 1 } Insert Sample Data Add some documents to the collection ? db.getCollection("company--EmployeeInformation").insertMany([ {"CompanyName": "Amazon", "EmployeeName": "Chris"}, ...

Read More

Decrement only a single value in MongoDB?

Nishtha Thakur
Nishtha Thakur
Updated on 15-Mar-2026 867 Views

To decrement only a single value in MongoDB, use the $inc operator with a negative value. This allows you to decrease a numeric field by a specified amount while targeting only one document. Syntax db.collection.update( { field: "matchValue" }, { $inc: { numericField: -decrementValue } } ); Create Sample Data db.decrementingOperationDemo.insertMany([ { "ProductName": "Product-1", "ProductPrice": 756 }, { "ProductName": "Product-2", "ProductPrice": 890 }, { "ProductName": "Product-3", "ProductPrice": 994 }, ...

Read More

Concatenate fields in MongoDB?

Nishtha Thakur
Nishtha Thakur
Updated on 15-Mar-2026 420 Views

To concatenate fields in MongoDB, use the $concat operator within an aggregation pipeline. This operator combines multiple string values into a single string, with optional separators between them. Syntax db.collection.aggregate([ { $project: { "newField": { $concat: ["$field1", "separator", "$field2"] } ...

Read More

Update multiple rows in a single MongoDB query?

Nishtha Thakur
Nishtha Thakur
Updated on 15-Mar-2026 449 Views

To update multiple rows in a single MongoDB query, use bulk operations with initializeUnorderedBulkOp() method. This allows you to batch multiple update operations together and execute them as one atomic operation. Syntax var bulkOp = db.collection.initializeUnorderedBulkOp(); bulkOp.find({condition1}).updateOne({$set: {field: "value"}}); bulkOp.find({condition2}).updateOne({$set: {field: "value"}}); bulkOp.execute(); Sample Data db.upDateMultipleRowsDemo.insertMany([ {"CustomerName": "John", "CustomerPurchaseAmount": 500}, {"CustomerName": "Chris", "CustomerPurchaseAmount": 700}, {"CustomerName": "David", "CustomerPurchaseAmount": 50}, {"CustomerName": "Larry", "CustomerPurchaseAmount": 1900} ]); { "acknowledged": true, ...

Read More

Getting a list of values by using MongoDB $group?

Nishtha Thakur
Nishtha Thakur
Updated on 15-Mar-2026 1K+ Views

To get a list of values using MongoDB, use the $group aggregation stage along with the $push operator. This approach groups documents by a specific field and creates an array of values from another field. Syntax db.collection.aggregate([ { $group: { _id: "$groupingField", arrayField: { $push: "$fieldToCollect" } } } ...

Read More

Implement MongoDB $concatArrays even when some of the values are null?

Nishtha Thakur
Nishtha Thakur
Updated on 15-Mar-2026 372 Views

Use the $ifNull operator with $concatArrays in the aggregation framework to concatenate arrays even when some fields are null or missing. The $ifNull operator replaces null values with an empty array, allowing concatenation to proceed successfully. Syntax db.collection.aggregate([ { $project: { concatenatedField: { $concatArrays: [ { $ifNull: ["$array1", []] }, { $ifNull: ["$array2", []] } ...

Read More
Showing 71–80 of 398 articles
« Prev 1 6 7 8 9 10 40 Next »
Advertisements