Big Data Analytics Articles

Page 5 of 135

How to get a rating average in MongoDB based on duplicate ids?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 404 Views

To get a rating average in MongoDB based on duplicate ids, use the $avg operator with $group. The $ifNull operator handles null rating values by treating them as zero in the calculation. Syntax db.collection.aggregate([ { "$group": { "_id": "$fieldToGroupBy", "averageField": { "$avg": { "$ifNull": ["$ratingField", 0] } } } ...

Read More

Update quantity in MongoDB based on two conditions?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 267 Views

To update quantity in MongoDB based on two conditions, use the update() method with multiple filter criteria. Combine document-level and array element conditions to target specific nested objects. Syntax db.collection.update( { "documentField": "value", "arrayField.nestedField": "arrayValue" }, { $set: { "arrayField.$.targetField": newValue } } ); Sample Data db.demo605.insertMany([ { _id: 1, ...

Read More

How to remove duplicates from MongoDB Collection?

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

To remove duplicates from a MongoDB collection, create a unique index on the field that should not have duplicate values. This prevents duplicate insertion and removes existing duplicates when combined with the dropDups option. Syntax db.collectionName.createIndex( { fieldName: 1 }, { unique: true, dropDups: true } ); Example: Preventing Duplicate FirstNames Let's create a unique index on the FirstName field to prevent duplicates ? db.demo604.createIndex( { FirstName: 1 }, { unique: true, dropDups: true ...

Read More

Difference between NumberLong(x) and NumberLong("x") in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 371 Views

The NumberLong(x) function can lose precision with very large integers due to JavaScript's number limitations, while NumberLong("x") preserves exact precision by accepting the number as a string. Syntax NumberLong(number) // May lose precision for very large values NumberLong("string") // Preserves exact precision Example Let us create a collection with documents to demonstrate the difference ? db.demo603.insertMany([ {"longValue": NumberLong(988998985857575789)}, {"longValueInString": NumberLong("988998985857575789")} ]); { "acknowledged": true, "insertedIds": ...

Read More

MongoDB aggregation of elements with similar ids in different documents?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 729 Views

To aggregate elements with similar ids in different documents, use $group in MongoDB aggregation pipeline. This technique groups documents by id and collects all related data into arrays. Syntax db.collection.aggregate([ { $group: { '_id': { id: "$id", "fieldName": "$fieldName" }, "count": { "$sum": 1 } }}, { $group: { '_id': "$_id.id", "aggregatedData": { ...

Read More

Update in MongoDB and prevent overwrite?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 629 Views

To update a document in MongoDB without overwriting the entire document, use the $set operator to modify specific fields while preserving existing data. This prevents accidental data loss during updates. Syntax db.collection.update( { "field": "matchValue" }, { $set: { "fieldToUpdate": "newValue" } } ); Sample Data db.demo601.insertMany([ { id: 1, userDetails: { userName: ...

Read More

Create a new user and set role in MongoDB

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 554 Views

To create a new user and set their role in MongoDB, use the createUser() method. The userAdminAnyDatabase role provides comprehensive administrative privileges across all databases. Syntax use admin db.createUser( { user: "yourUserName", pwd: "yourPassword", roles: [ { role: "yourRoleName", db: "yourDatabaseName" } ] } ) Example Create a user with the "userAdminAnyDatabase" role ? use admin db.createUser( ...

Read More

How to use custom variable while updating a MongoDB document?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 535 Views

To use custom variables while updating MongoDB documents, first declare a variable with var, then reference it in your update() operation. This allows for dynamic updates and better code reusability. Syntax var variableName = yourValue; db.collectionName.update( { filter }, { $set: { fieldName: variableName } } ); Create Sample Data db.demo600.insertMany([ { id: 1, Name: "Robert" }, { id: 2, Name: "Mike" }, { id: 3, Name: "Sam" } ]); ...

Read More

How to subtract values (TotalPrice – Discount) from document field values in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 418 Views

To subtract values from document field values in MongoDB, use the $subtract operator within the aggregation pipeline. This operator performs arithmetic subtraction between numeric field values or expressions. Syntax db.collection.aggregate([ { $project: { newField: { $subtract: ["$field1", "$field2"] } ...

Read More

Find which MongoDB document contains a specific string?

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

To find which document contains a specific string, use $regex along with find(). The regex operator performs pattern matching to locate documents containing the specified text substring. Syntax db.collection.find({ fieldName: { $regex: /pattern/flags } }); Sample Data db.demo597.insertMany([ { "Name": "John Doe" }, { "Name": "John Smith" }, { "Name": "Chris Brown" }, { "Name": "Adam Smith" } ]); { "acknowledged": true, ...

Read More
Showing 41–50 of 1,348 articles
« Prev 1 3 4 5 6 7 135 Next »
Advertisements