Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Articles by Nishtha Thakur
Page 6 of 40
MongoDB regex to display records whose first five letters are uppercase?
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 MoreHow to perform $gt on a hash in a MongoDB document?
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 MoreHow can I drop a collection in MongoDB with two dashes in the name?
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 MoreDecrement only a single value in MongoDB?
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 MoreConcatenate fields in MongoDB?
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 MoreUpdate multiple rows in a single MongoDB query?
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 MoreGetting a list of values by using MongoDB $group?
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 MoreImplement MongoDB $concatArrays even when some of the values are null?
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 MoreHow to print document value in MongoDB shell?
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 MoreHow to get tag count in MongoDB query results based on list of names?
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