Database Articles

Page 78 of 547

Update array in MongoDB document by variable index?

Anvi Jain
Anvi Jain
Updated on 15-Mar-2026 440 Views

To update array in MongoDB document by variable index, use a JavaScript variable to dynamically construct the field path. This allows you to target specific array positions using a variable instead of hardcoding the index value. Syntax var indexVariable = yourIndexValue, updateObject = { "$set": {} }; updateObject["$set"]["arrayFieldName." + indexVariable] = "newValue"; db.collectionName.update({ "_id": ObjectId("...") }, updateObject); Sample Data Let us first create a collection with documents ? db.updateByVariableDemo.insertOne({ "StudentSubjects": ["MySQL", "Java", "SQL Server", "PL/SQL"] }); { ...

Read More

Delete partial data in MongoDB?

Nishtha Thakur
Nishtha Thakur
Updated on 15-Mar-2026 215 Views

To delete partial data in MongoDB, you can combine find(), skip(), and map() methods to select specific documents, then use remove() with the $in operator to delete them based on their IDs. Syntax var ids = db.collection.find({}, {_id: 1}).skip(N).toArray().map(function(doc) { return doc._id; }); db.collection.remove({_id: {$in: ids}}); Sample Data db.deleteDemo.insertMany([ {"Name": "John"}, {"Name": "Carol"}, {"Name": "Sam"}, {"Name": "David"}, {"Name": "Robert"}, {"Name": "Chris"}, ...

Read More

Selecting only a single field from MongoDB?

Smita Kapse
Smita Kapse
Updated on 15-Mar-2026 532 Views

To select only a single field from MongoDB, use the projection parameter in the find() method. Set the desired field to 1 to include it, and set _id to 0 to exclude the default ID field. Syntax db.collection.find( { query }, { fieldName: 1, _id: 0 } ); Sample Data db.selectingASingleFieldDemo.insertMany([ {"StudentFirstName": "John", "StudentAge": 23, "StudentCountryName": "US"}, {"StudentFirstName": "Carol", "StudentAge": 21, "StudentCountryName": "UK"}, {"StudentFirstName": "David", "StudentAge": 24, "StudentCountryName": "AUS"}, ...

Read More

MongoDB divide aggregation operator?

Anvi Jain
Anvi Jain
Updated on 15-Mar-2026 348 Views

The $divide operator in MongoDB performs division between two numbers during aggregation. It takes an array of exactly two numeric expressions and returns the result of dividing the first by the second. Syntax { $divide: [ , ] } Sample Data Let us first create a collection with documents ? db.aggregationOperatorDemo.insertOne({ "FirstValue": 392883, "SecondValue": 10000000000 }); { "acknowledged": true, "insertedId": ObjectId("5cd541452cba06f46efe9f01") } Following is the query to display all documents ...

Read More

Search an array of hashes in MongoDB?

Nishtha Thakur
Nishtha Thakur
Updated on 15-Mar-2026 285 Views

To search an array of hashes in MongoDB, use dot notation to access fields within the hash objects stored in the array. This allows you to query specific key-value pairs within nested hash structures. Syntax db.collection.find({ "arrayName.fieldName": "value" }) Sample Data db.searchAnArrayDemo.insertMany([ { _id: 1, "TechnicalDetails": [{ "Language": "MongoDB" }] }, { _id: 2, "TechnicalDetails": [{ "Language": "MySQL" }] }, { _id: 3, "TechnicalDetails": [{ "Language": "MongoDB" }] }, { _id: 4, "TechnicalDetails": [{ "Language": "MongoDB" }] ...

Read More

Want to update inner field in a MongoDB

Smita Kapse
Smita Kapse
Updated on 15-Mar-2026 221 Views

To update an inner field in MongoDB, use the $set operator with dot notation to target the nested field within a document. This allows you to modify specific fields inside embedded documents without affecting other fields. Syntax db.collectionName.update( {"_id": ObjectId}, {$set: {"outerField.innerField": newValue}} ); Sample Data Let us create a collection with a document containing nested fields ? db.updateDocumentDemo.insertOne({ "StudentDetails": { "StudentFirstName": "Adam", "StudentLastName": ...

Read More

Query Array for 'true' value at index n in MongoDB?

Nishtha Thakur
Nishtha Thakur
Updated on 15-Mar-2026 192 Views

To query an array for a true value at a specific index in MongoDB, use dot notation with the array field name followed by the index position. This allows you to check boolean values at exact array positions. Syntax db.collection.find({"arrayField.INDEX": true}); // Multiple index conditions db.collection.find({ $and: [ {"arrayField.INDEX1": true}, {"arrayField.INDEX2": true} ] }); Sample Data db.containsTrueValueDemo.insertOne({ "IsMarried": [true, false, true, true, true, true, ...

Read More

How to find MongoDB documents in a collection with a filter on multiple combined fields?

Smita Kapse
Smita Kapse
Updated on 15-Mar-2026 401 Views

To find MongoDB documents with filters on multiple combined fields, use the $or or $and operators within the find() method. These operators allow you to combine multiple conditions across different fields. Syntax // OR operator - matches documents that satisfy ANY condition db.collection.find({ $or: [ { "field1": condition1 }, { "field2": condition2 } ] }); // AND operator - matches documents that satisfy ALL conditions db.collection.find({ ...

Read More

How to pull even numbers from an array in MongoDB?

Anvi Jain
Anvi Jain
Updated on 15-Mar-2026 635 Views

To pull even numbers from an array in MongoDB, use the $pull operator combined with the $mod operator. The $mod operator performs modulo division to identify even numbers (remainder 0 when divided by 2). Syntax db.collection.updateMany( {}, { $pull: { "arrayField": { $mod: [2, 0] } } } ); Sample Data db.pullEvenNumbersDemo.insertOne({ "AllNumbers": [101, 102, 104, 106, 108, 109, 110, 112, 14, 17, 18, 21] }); { "acknowledged": true, "insertedId": ...

Read More

Add a field to an embedded document in an array in MongoDB?

Nishtha Thakur
Nishtha Thakur
Updated on 15-Mar-2026 560 Views

To add a field to an embedded document in an array in MongoDB, use the $set operator combined with the $ positional operator and $elemMatch to target the specific array element. Syntax db.collection.update( { "arrayField": { $elemMatch: { "matchField": "value" } } }, { $set: { "arrayField.$.newField": "newValue" } } ); Sample Data Let us create a collection with documents ? db.addAFieldDemo.insertOne({ "ClientName": "Larry", "ClientCountryName": "US", "ClientOtherDetails": [ ...

Read More
Showing 771–780 of 5,468 articles
« Prev 1 76 77 78 79 80 547 Next »
Advertisements