Database Articles

Page 8 of 547

MongoDB query to remove entire array from collection?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 515 Views

To remove an entire array field from all documents in a MongoDB collection, use the $unset operator. This completely removes the specified field from the documents. Syntax db.collection.updateMany( {}, { $unset: { "arrayFieldName": "" } } ); Sample Data Let us create a collection with documents containing array fields ? db.demo609.insertMany([ { "ListOfSubject": ["MySQL", "MongoDB"] }, { "ListOfSubject": ["Java"] } ]); { "acknowledged": true, "insertedIds": [ ...

Read More

Getting unique values within two arrays in one MongoDB document

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

To get unique values within two arrays in a MongoDB document, use the $setUnion operator in the aggregation pipeline. The $setUnion takes two or more arrays and returns an array containing the unique elements that appear in any input array. Syntax db.collection.aggregate([ { $project: { uniqueArray: { $setUnion: ["$array1", "$array2"] } } } ]) Sample Data Let us ...

Read More

MongoDB query to update the nested document?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 508 Views

To update a nested document in MongoDB, use the update() method with dot notation to specify the path to the nested field. This allows you to modify deeply embedded values without affecting the parent document structure. Syntax db.collection.update( { "filterField": "value" }, { $set: { "parentField.nestedField.targetField": "newValue" } } ); Sample Data Let us create a collection with a nested document structure ? db.demo607.insertOne({ id: 1, "Info1": { ...

Read More

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

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 403 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 266 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 726 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 628 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
Showing 71–80 of 5,468 articles
« Prev 1 6 7 8 9 10 547 Next »
Advertisements