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
MongoDB Articles
Page 94 of 111
How to reduce MongoDB storage space after deleting large amount of data?
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 MoreConcatenate strings from two fields into a third field in MongoDB?
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 MoreCan we remove _id from MongoDB query result?
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 MoreHow to change the password in MongoDB for existing user?
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 MoreHow to operate on all databases from the MongoDB shell?
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 MoreHow to update value of a key in a list of a json in MongoDB?
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 MoreIs it possible to sum two fields in MongoDB using the Aggregation framework?
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 MoreGet Distinct Values with Sorted Data in MongoDB?
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 MoreHow to update MongoDB collection using $toLower?
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 MoreUpdate two separate arrays in a document with one update call in MongoDB?
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