MongoDB Articles

Page 94 of 111

How to reduce MongoDB storage space after deleting large amount of data?

Chandu yadav
Chandu yadav
Updated on 15-Mar-2026 537 Views

To reduce MongoDB storage space after deleting a large amount of data, you need to reclaim the freed disk space since MongoDB doesn't automatically release space after deletions. The database continues to use the same amount of disk space even after documents are removed. Syntax db.runCommand({ "compact": "collectionName" }) Or for the entire database: db.repairDatabase() Method 1: Using compact Command (Recommended) The compact command is the modern approach to reclaim space from a specific collection ? db.runCommand({ "compact": "users" }) { "ok" : 1, "bytesFreed" ...

Read More

Concatenate strings from two fields into a third field in MongoDB?

Ankith Reddy
Ankith Reddy
Updated on 15-Mar-2026 1K+ Views

To concatenate strings from two fields into a third field in MongoDB, use the $concat operator within an aggregation pipeline with $project stage. Syntax db.collection.aggregate([ { $project: { "newFieldName": { $concat: [ "$field1", "delimiter", "$field2" ] } } } ]); Sample Data db.concatenateStringsDemo.insertMany([ {"StudentFirstName": "John", "StudentLastName": "Doe"}, {"StudentFirstName": "John", "StudentLastName": "Smith"}, {"StudentFirstName": "Carol", "StudentLastName": "Taylor"}, {"StudentFirstName": "David", "StudentLastName": "Miller"}, {"StudentFirstName": "James", "StudentLastName": "Williams"} ]); { ...

Read More

Can we remove _id from MongoDB query result?

George John
George John
Updated on 15-Mar-2026 6K+ Views

To remove _id from MongoDB query result, you need to set 0 for the _id field in the projection parameter. This excludes the _id field from the returned documents. Syntax db.collectionName.find({}, {_id: 0}); Sample Data Let us create a collection with sample documents ? db.removeIdDemo.insertMany([ {"UserName": "John", "UserAge": 23}, {"UserName": "Mike", "UserAge": 27}, {"UserName": "Sam", "UserAge": 34}, {"UserName": "Carol", "UserAge": 29} ]); { "acknowledged": true, ...

Read More

How to change the password in MongoDB for existing user?

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

To change the password in MongoDB for an existing user, use the changeUserPassword() method. This operation requires administrative privileges and must be performed on the database where the user was created. Syntax db.changeUserPassword("username", "newPassword"); Step 1: Switch to Admin Database First, switch to the admin database where user management operations are performed ? use admin switched to db admin Step 2: Display Existing Users Check the current users in the database ? db.getUsers(); [ { ...

Read More

How to operate on all databases from the MongoDB shell?

Ankith Reddy
Ankith Reddy
Updated on 15-Mar-2026 216 Views

To operate on all databases from MongoDB shell, you can use listDatabases along with adminCommand(). This method retrieves comprehensive information about all databases in your MongoDB instance. Syntax var allDatabaseList = db.adminCommand('listDatabases'); printjson(allDatabaseList); Example First, check the current database with the db command − db; test Now retrieve information about all databases using adminCommand() − var allDatabaseList = db.adminCommand('listDatabases'); printjson(allDatabaseList); { "databases" : [ { ...

Read More

How to update value of a key in a list of a json in MongoDB?

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

To update a value of a key in a list (array) of JSON documents in MongoDB, you can use the $ positional operator for single element updates or retrieve the document, modify it in JavaScript, and replace the entire array using $set. Syntax // Method 1: Update single array element db.collection.update( {"arrayField.key": "matchValue"}, { $set: { "arrayField.$.key": "newValue" } } ); // Method 2: Update multiple elements using JavaScript var doc = db.collection.findOne({"_id": ObjectId("id")}); doc.arrayField.forEach(function(element) { element.key = "newValue"; }); db.collection.update( ...

Read More

Is it possible to sum two fields in MongoDB using the Aggregation framework?

Chandu yadav
Chandu yadav
Updated on 15-Mar-2026 1K+ Views

Yes, it is possible to sum two fields in MongoDB using the $add operator within the $project stage of the aggregation framework. This creates a new computed field containing the sum of existing fields. Syntax db.collection.aggregate([ { $project: { field1: "$field1", field2: "$field2", sumField: { $add: ["$field1", "$field2"] } ...

Read More

Get Distinct Values with Sorted Data in MongoDB?

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

To get distinct values with sorted data in MongoDB, use the distinct() method followed by sort(). The distinct() method returns unique values from a specified field, and sort() arranges them in alphabetical or numerical order. Syntax db.collection.distinct("fieldName").sort() Sample Data db.getDistinctWithSortedDataDemo.insertMany([ {"StudentId": 10, "StudentName": "John", "StudentAge": 23}, {"StudentId": 20, "StudentName": "Carol", "StudentAge": 21}, {"StudentId": 10, "StudentName": "John", "StudentAge": 23}, {"StudentId": 30, "StudentName": "Chris", "StudentAge": 22}, {"StudentId": 20, "StudentName": "Carol", "StudentAge": 21}, ...

Read More

How to update MongoDB collection using $toLower?

Ankith Reddy
Ankith Reddy
Updated on 15-Mar-2026 775 Views

To update a MongoDB collection using $toLower functionality, use the forEach() method to iterate through documents and apply JavaScript's toLowerCase() function. While $toLower is primarily an aggregation operator, this approach allows you to permanently update field values in your collection. Syntax db.collection.find().forEach( function(document) { document.fieldName = document.fieldName.toLowerCase(); db.collection.save(document); } ); Sample Data First, let's create a collection with mixed-case student names ? db.toLowerDemo.insertMany([ {"StudentId": ...

Read More

Update two separate arrays in a document with one update call in MongoDB?

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

To update two separate arrays in a MongoDB document with a single update call, use the $push operator with multiple field specifications in the update document. This allows you to add values to multiple arrays simultaneously. Syntax db.collection.update( { "query": "condition" }, { $push: { "arrayField1": "value1", "arrayField2": "value2" ...

Read More
Showing 931–940 of 1,106 articles
« Prev 1 92 93 94 95 96 111 Next »
Advertisements