MongoDB Articles

Page 75 of 111

How to remove white spaces (leading and trailing) from string value in MongoDB?

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

To remove white spaces (leading and trailing) from string values in MongoDB, you can use the forEach() method combined with JavaScript's trim() function to iterate through documents and update them. Syntax db.collection.find().forEach(function(doc) { doc.fieldName = doc.fieldName.trim(); db.collection.update( { "_id": doc._id }, { "$set": { "fieldName": doc.fieldName } } ); }); Sample Data Let us first create a collection with documents containing white spaces ? ...

Read More

How to add a field with static value to MongoDB find query?

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

To add a field with static value to MongoDB find query, use the $literal operator with the aggregation framework. The $literal operator returns a value without parsing, making it perfect for adding constant values to query results. Syntax db.collection.aggregate([ { $project: { field1: 1, field2: 1, "staticFieldName": { $literal: ...

Read More

How to get tag count in MongoDB query results based on list of names?

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

To get tag count in MongoDB query results based on a list of names, use the $in operator to match documents containing any of the specified names in an array field, then apply count() to get the total number of matching documents. Syntax db.collection.find({"arrayField": {$in: ["value1", "value2"]}}).count(); Sample Data db.tagCountDemo.insertMany([ {"ListOfNames": ["John", "Sam", "Carol"]}, {"ListOfNames": ["Bob", "David", "John"]}, {"ListOfNames": ["Mike", "Robert", "Chris"]}, {"ListOfNames": ["James", "Carol", "Jace"]} ]); { "acknowledged": true, ...

Read More

Get documents expired before today in MongoDB?

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

To get documents expired before today in MongoDB, use the $lte operator with new Date() to find all documents where the date field is less than or equal to the current date. Syntax db.collection.find({ "dateField": { $lte: new Date() } }); Sample Data db.getDocumentsExpiredDemo.insertMany([ { "ArrivalDate": new ISODate("2019-05-11") }, { "ArrivalDate": new ISODate("2019-01-01") }, { "ArrivalDate": new ISODate("2019-05-10") }, { "ArrivalDate": new ISODate("2019-02-01") } ]); { "acknowledged": true, ...

Read More

Update array in MongoDB document by variable index?

Anvi Jain
Anvi Jain
Updated on 15-Mar-2026 441 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 349 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
Showing 741–750 of 1,106 articles
« Prev 1 73 74 75 76 77 111 Next »
Advertisements