Articles on Trending Technologies

Technical articles with clear explanations and examples

Tutorix - AI Tutor

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 235 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 828 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 793 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 224 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

Perform conditional upserts or updates in MongoDB

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

In MongoDB, you can perform conditional updates that only modify a field if certain conditions are met. The $max operator is particularly useful for conditional updates - it only updates a field if the new value is greater than the current value. Syntax db.collection.update( { "field": "matchValue" }, { $max: { "fieldToUpdate": newValue } } ); Sample Data Let us create a collection with sample student score documents ? db.conditionalUpdatesDemo.insertMany([ { "_id": ...

Read More

Pull and add to set at the same time with MongoDB? Is it Possible?

Arjun Thakur
Arjun Thakur
Updated on 15-Mar-2026 920 Views

Yes, you can use pull and add operations simultaneously in MongoDB using $addToSet and $pull operators with bulk operations. This allows you to modify arrays in multiple ways within a single database round trip. Syntax var bulkOp = db.collection.initializeOrderedBulkOp(); bulkOp.find({condition1}).updateOne({$addToSet: {arrayField: newValue}}); bulkOp.find({condition2}).updateOne({$pull: {arrayField: valueToRemove}}); bulkOp.execute(); Sample Data db.pullAndAddToSetDemo.insertOne({StudentScores: [78, 89, 90]}); { "acknowledged": true, "insertedId": ObjectId("5c9a797e15e86fd1496b38af") } Let's view the initial document ? db.pullAndAddToSetDemo.find().pretty(); { "_id": ObjectId("5c9a797e15e86fd1496b38af"), ...

Read More

Find oldest/ youngest post in MongoDB collection?

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

To find oldest/youngest post in MongoDB collection, you can use sort() with limit(1). Use ascending sort (1) for oldest and descending sort (-1) for newest posts based on date fields. Syntax // For oldest post db.collection.find().sort({"dateField": 1}).limit(1); // For newest post db.collection.find().sort({"dateField": -1}).limit(1); Sample Data db.getOldestAndYoungestPostDemo.insertMany([ { "UserId": "Larry@123", "UserName": "Larry", "UserPostDate": new ISODate('2019-03-27 12:00:00') }, ...

Read More
Showing 23701–23710 of 61,297 articles
Advertisements