Big Data Analytics Articles

Page 29 of 135

Update salary field value with 10 percent of each employee in MongoDB

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 1K+ Views

To update salary field values with a 10 percent increase for each employee in MongoDB, use the $mul operator with a multiplier of 1.1. This operator multiplies the existing field value by the specified number. Syntax db.collection.update( { query }, { $mul: { fieldName: multiplier } }, { multi: true } ); Create Sample Data Let us first create a collection with employee documents ? db.demo417.insertMany([ {"EmployeeName": "Chris", "EmployeeSalary": 500}, {"EmployeeName": "Mike", ...

Read More

How to select specific columns in MongoDB query?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 7K+ Views

To select specific columns in MongoDB, use projection in the find() method. Set fields to 1 to include them or 0 to exclude them. You can hide unwanted columns by setting them to 0, showing only the desired fields. Syntax db.collection.find(query, projection) // Include specific fields db.collection.find({}, {field1: 1, field2: 1}) // Exclude specific fields db.collection.find({}, {field1: 0, field2: 0}) Sample Data db.demo415.insertMany([ {"ClientName": "Robert", "ClientCountryName": "US"}, {"ClientName": "David", "ClientCountryName": "UK"}, {"ClientName": "Bob", "ClientCountryName": "AUS"} ]); ...

Read More

Find by _id on an array of objects in MongoDB database?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 898 Views

To find by _id on an array of objects in MongoDB, use the aggregation framework with $unwind and $match operators. This approach is more effective than using find() when working with nested array elements. Syntax db.collection.aggregate([ { $unwind: "$arrayField" }, { $match: { "arrayField.field": "value" } } ]); Sample Data db.demo414.insertOne({ "_id": "110", "details": [ { "StudentName": ...

Read More

Is it possible to exclude nested fields in MongoDB with a wildcard?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 969 Views

Yes, you can exclude nested fields with a wildcard pattern in MongoDB using the aggregation pipeline. This technique uses $objectToArray and $arrayToObject operators to transform nested objects, exclude specific fields, and reconstruct the document. Syntax db.collection.aggregate([ { $project: { "nestedField": { $objectToArray: "$nestedField" } } }, { $project: { "nestedField.v.fieldToExclude": 0 } }, { $project: { "nestedField": { $arrayToObject: "$nestedField" } } } ]); Sample Data db.demo413.insertOne({ "_id": "101", "details": { ...

Read More

Update an array of strings nested within an array of objects in MongoDB

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 308 Views

To update an array of strings nested within an array of objects in MongoDB, use the $pull operator with the positional all operator $[] to remove elements from nested arrays across all array elements. Syntax db.collection.updateMany( { query }, { $pull: { "arrayField.$[].nestedArray": "valueToRemove" } } ); Sample Data db.demo412.insertOne({ "Information1": [ { "Information2": [ ...

Read More

How to get only values in arrays with MongoDB aggregate?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 975 Views

To get only values from arrays in MongoDB aggregate operations, use the $map operator within $project to transform array elements and extract specific field values into a new array structure. Syntax db.collection.aggregate([ { $project: { arrayField: { $map: { ...

Read More

How do I remove elements not matching conditions in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 456 Views

To remove elements not matching conditions in MongoDB, use the $pull operator combined with query conditions. The $pull operator removes all array elements that match the specified condition. Syntax db.collection.updateMany( { "arrayField": { "$elemMatch": { "field": { "$ne": "value" } } } }, { "$pull": { "arrayField": { "field": { "$ne": "value" } } } } ); Sample Data Let us create a collection with documents ? db.demo410.insertOne({ details: [ {isMarried: false}, ...

Read More

Understanding MongoDB Query Plan

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 609 Views

To understand how MongoDB executes queries, use the explain() method. Query plans show the execution strategy, including which indexes are used and performance statistics for query optimization. Syntax db.collection.explain().find(query); db.collection.explain("executionStats").find(query); db.collection.explain("allPlansExecution").find(query); Sample Data Let us create a collection with documents and an index ? db.demo408.insertMany([ {"Value": 50}, {"Value": 20}, {"Value": 45}, {"Value": 35} ]); { "acknowledged": true, "insertedIds": [ ...

Read More

How to create MongoDB user on existing database with correct role?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 314 Views

To create a new user in MongoDB, use the createUser() method on an existing database. This allows you to assign specific roles and authentication mechanisms to control user permissions. Syntax db.createUser({ user: "username", pwd: "password", roles: [ { role: "roleName", db: "databaseName" } ], mechanisms: ["SCRAM-SHA-256"] }); Example: Create User with readWrite Role Create a user named "John" with readWrite permissions on the test database ? ...

Read More

How to use arrays as filters by querying subdocuments in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 174 Views

To use arrays as filters when querying subdocuments in MongoDB, use the $setIsSubset operator within an aggregation pipeline. This operator checks if one array is a subset of another, making it useful for filtering array elements based on specific values. Syntax db.collection.aggregate([ { $project: { "arrayField": { $filter: { input: "$arrayField", ...

Read More
Showing 281–290 of 1,348 articles
« Prev 1 27 28 29 30 31 135 Next »
Advertisements