Nishtha Thakur

Nishtha Thakur

398 Articles Published

Articles by Nishtha Thakur

Page 6 of 40

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 866 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

How to print document value in MongoDB shell?

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

To print specific document values in MongoDB shell, use the forEach() method combined with the print() function. This allows you to extract and display individual field values from query results. Syntax db.collection.find(query, projection).forEach(function(document) { print(document.fieldName); }); Sample Data db.printDocumentValueDemo.insertMany([ {"InstructorName": "John Smith"}, {"InstructorName": "Sam Williams"}, {"InstructorName": "David Miller"} ]); { "acknowledged": true, "insertedIds": [ ObjectId("5cd6804f7924bb85b3f48950"), ...

Read More

How to get tag count in MongoDB query results based on list of names?

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

To get tag count in MongoDB query results based on a list of names, use the $in operator to match documents containing any of the specified names in an array field, then apply count() to get the total number of matching documents. Syntax db.collection.find({"arrayField": {$in: ["value1", "value2"]}}).count(); Sample Data db.tagCountDemo.insertMany([ {"ListOfNames": ["John", "Sam", "Carol"]}, {"ListOfNames": ["Bob", "David", "John"]}, {"ListOfNames": ["Mike", "Robert", "Chris"]}, {"ListOfNames": ["James", "Carol", "Jace"]} ]); { "acknowledged": true, ...

Read More
Showing 51–60 of 398 articles
« Prev 1 4 5 6 7 8 40 Next »
Advertisements