George John

George John

789 Articles Published

Articles by George John

Page 28 of 79

Adding new property to each document in a large MongoDB collection?

George John
George John
Updated on 15-Mar-2026 889 Views

To add a new property to each document in a large MongoDB collection, use the forEach() method combined with update() and $set operator. This approach iterates through all documents and adds the new field dynamically. Syntax db.collection.find().forEach(function(doc) { db.collection.update( {_id: doc._id}, {$set: {newProperty: computedValue}} ); }); Create Sample Data db.addingNewPropertyDemo.insertMany([ {"StudentName": "John", "StudentAge": 23, "CountryName": "US"}, {"StudentName": "David", "StudentAge": 21, "CountryName": ...

Read More

Renaming column name in a MongoDB collection?

George John
George John
Updated on 15-Mar-2026 806 Views

To rename a field (column) name in a MongoDB collection, use the $rename operator. This operator allows you to change field names across multiple documents in a single operation. Syntax db.collectionName.updateMany( {}, { $rename: { "oldFieldName": "newFieldName" } } ); Sample Data Let us create a collection with sample documents ? db.renamingColumnNameDemo.insertMany([ { "StudentName": "Larry", "Age": 23 }, { "StudentName": "Sam", "Age": 26 }, { "StudentName": "Robert", "Age": 27 } ]); ...

Read More

How to check if field is a number in MongoDB?

George John
George John
Updated on 15-Mar-2026 589 Views

To check if a field is a number in MongoDB, use the $type operator with the value "number". This operator matches documents where the specified field contains numeric values (integers, doubles, or decimals). Syntax db.collectionName.find({fieldName: {$type: "number"}}); Sample Data Let us create a collection with documents containing different data types ? db.checkIfFieldIsNumberDemo.insertMany([ {"StudentName": "John", "StudentAge": 23}, {"StudentName": "Chris", "StudentMathScore": 98, "StudentCountryName": "US"}, {"StudentName": "Robert", "StudentCountryName": "AUS"}, {"StudentId": 101, "StudentName": "Larry", "StudentCountryName": "AUS"} ]); ...

Read More

How to get connected clients in MongoDB?

George John
George John
Updated on 15-Mar-2026 898 Views

To get connected clients in MongoDB, use the db.currentOp() method with true parameter to display all operations including idle connections. The client field in the output shows the IP address and port of each connected client. Syntax db.currentOp(true) Example Run the following command to view all connected clients ? db.currentOp(true) The output displays all connected clients with detailed information ? { "inprog" : [ { "host" : "DESKTOP-QN2RB3H:27017", ...

Read More

Write a MongoDB query to get nested value?

George John
George John
Updated on 15-Mar-2026 272 Views

You can use dot notation to query nested values in MongoDB documents. Dot notation allows you to access fields within embedded documents using the format parentField.nestedField. Syntax db.collection.find({"parentField.nestedField": "value"}); Sample Data Let us first create a collection with nested documents ? db.nestedQueryDemo.insertMany([ { "EmployeeName": "John", "EmployeeDetails": { "_id": "EMP-101", ...

Read More

How to use $slice operator to get last element of array in MongoDB?

George John
George John
Updated on 15-Mar-2026 927 Views

To get the last element of an array in MongoDB, use the $slice operator with -1 in the projection. This returns only the last element of the specified array field. Syntax db.collectionName.find( {}, { arrayFieldName: { $slice: -1 } } ); Sample Data Let us create a collection with sample student documents ? db.getLastElementOfArrayDemo.insertMany([ { "StudentName": "James", "StudentMathScore": [78, 68, 98] ...

Read More

Insert to specific index for MongoDB array?

George John
George John
Updated on 15-Mar-2026 586 Views

To insert an element at a specific index in a MongoDB array, use the $push operator combined with $each and $position modifiers. This allows precise control over where new elements are inserted in the array. Syntax db.collection.update( { _id: ObjectId("document_id") }, { $push: { arrayField: { $each: [ "newElement" ], ...

Read More

Removing all collections whose name matches a string in MongoDB

George John
George John
Updated on 15-Mar-2026 578 Views

To remove all collections whose name matches a string in MongoDB, use a for loop to iterate over all collections, check for the string pattern using indexOf(), and apply the drop() method to matching collections. Syntax var allCollectionNames = db.getCollectionNames(); for(var i = 0; i < allCollectionNames.length; i++){ var colName = allCollectionNames[i]; if(colName.indexOf('searchString') == 0){ db[colName].drop(); } } Sample Data Let's say we are using the database "sample" with the following collections ? ...

Read More

Handling optional/empty data in MongoDB?

George John
George John
Updated on 15-Mar-2026 457 Views

To handle optional or empty data in MongoDB, use the $ne operator to exclude null values, the $exists operator to check field presence, and combine operators to filter various empty states like null, empty strings, or missing fields. Syntax // Exclude null values db.collection.find({ fieldName: { $ne: null } }) // Check if field exists db.collection.find({ fieldName: { $exists: true } }) // Exclude null AND missing fields db.collection.find({ fieldName: { $exists: true, $ne: null } }) Sample Data db.handlingAndEmptyDataDemo.insertMany([ { "StudentName": "John", "StudentCountryName": "" }, ...

Read More

How to compare field values in MongoDB?

George John
George John
Updated on 15-Mar-2026 346 Views

To compare field values in MongoDB, you can use the $where operator for JavaScript-based comparisons or the $expr operator (recommended) for aggregation expression comparisons. Both methods allow you to compare fields within the same document. Syntax // Using $where (JavaScript evaluation) db.collection.find({ $where: "this.field1 > this.field2" }); // Using $expr (Aggregation expressions - recommended) db.collection.find({ $expr: { $gt: ["$field1", "$field2"] } }); Sample Data db.comparingFieldDemo.insertMany([ {"Value1": 30, "Value2": 40}, {"Value1": 60, "Value2": 70}, {"Value1": 160, "Value2": 190}, ...

Read More
Showing 271–280 of 789 articles
« Prev 1 26 27 28 29 30 79 Next »
Advertisements