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
Database Articles
Page 78 of 547
Update array in MongoDB document by variable index?
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 MoreDelete partial data in MongoDB?
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 MoreSelecting only a single field from MongoDB?
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 MoreMongoDB divide aggregation operator?
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 MoreSearch an array of hashes in MongoDB?
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 MoreWant to update inner field in a MongoDB
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 MoreQuery Array for 'true' value at index n in MongoDB?
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 MoreHow to find MongoDB documents in a collection with a filter on multiple combined fields?
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 MoreHow to pull even numbers from an array in MongoDB?
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 MoreAdd a field to an embedded document in an array in MongoDB?
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