Database Articles

Page 74 of 547

MongoDB find() to operate on recursive search?

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

Use dot notation with find() to perform recursive search within nested arrays and embedded documents. This allows querying deep fields without complex operators. Syntax db.collection.find({"arrayName.fieldName": value}); db.collection.find({"nestedDocument.field": value}); Sample Data db.findOperationDemo.insertMany([ { "ClientDetails": [ {"ClientId": 101, "ClientName": "Chris"}, {"ClientId": 102, "ClientName": "Robert"} ] }, ...

Read More

I want to create a new field in an already created document. How can this be done using MongoDB query?

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

To create a new field in an already existing MongoDB document, use the $set operator with the update() method. The $set operator adds new fields or modifies existing field values. Syntax db.collection.update( { "matchingField": "value" }, { $set: { "newFieldName": "newValue" } } ); Create Sample Data Let us first create a collection with documents − db.createFieldDemo.insertMany([ { "StudentFirstName": "John", "StudentAge": 21 }, { "StudentFirstName": "Larry", "StudentAge": 23 }, { "StudentFirstName": "Chris", ...

Read More

How to store MongoDB result in an array?

Anvi Jain
Anvi Jain
Updated on 15-Mar-2026 1K+ Views

To store MongoDB result in an array, use the toArray() method. This converts the cursor returned by MongoDB queries into a JavaScript array that can be manipulated and accessed using array methods. Syntax var arrayVariable = db.collectionName.find().toArray(); Create Sample Data Let us first create a collection with documents ? db.mongoDbResultInArrayDemo.insertMany([ {"CustomerName": "David Miller", "CustomerAge": 24, "isMarried": false}, {"CustomerName": "Sam Williams", "CustomerAge": 46, "isMarried": true}, {"CustomerName": "Carol Taylor", "CustomerAge": 23, "isMarried": false} ]); { ...

Read More

Get all fields names in a MongoDB collection?

Nishtha Thakur
Nishtha Thakur
Updated on 15-Mar-2026 3K+ Views

To get all field names in a MongoDB collection, you can use Map-Reduce with the distinct() method. This approach extracts all unique field names across all documents in the collection. Syntax db.runCommand({ "mapreduce": "collectionName", "map": function() { for (var key in this) { emit(key, null); } }, "reduce": function(key, values) { return null; }, "out": "temp_collection" }); db.temp_collection.distinct("_id"); Sample Data Let us create a collection ...

Read More

How can I update all elements in an array with a prefix string?

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

To update all elements in an array with a prefix string in MongoDB, use forEach() combined with map() to transform each array element and $set to update the array. This approach processes each document individually and applies the prefix to all array elements. Syntax db.collection.find().forEach(function (doc) { var prefixedArray = doc.arrayField.map(function (element) { return "PREFIX" + element; }); db.collection.update( {_id: doc._id}, ...

Read More

MongoDB query to remove array elements from a document?

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

Use the $pull operator to remove array elements from a MongoDB document. This operator removes all instances of a value from an existing array field. Syntax db.collection.update( { }, { $pull: { fieldName: value } }, { multi: true } ); Sample Data Let us first create a collection with documents − db.removeArrayElementsDemo.insertMany([ { "AllPlayerName": ["John", "Sam", "Carol", "David"] }, { "AllPlayerName": ["Chris", "Robert", "John", "Mike"] } ]); { ...

Read More

Can MongoDB find() function display avoiding _id?

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

Yes, MongoDB find() function can display results while excluding the _id field by using projection. Set _id: 0 in the projection parameter to hide it from the output. Syntax db.collectionName.find({}, { _id: 0 }); Sample Data Let us first create a collection with sample documents ? db.excludeIdDemo.insertMany([ { "CustomerName": "Larry" }, { "CustomerName": "Chris" }, { "CustomerName": "Mike" }, { "CustomerName": "Bob" } ]); { "acknowledged": true, ...

Read More

MongoDB query to replace value in an array?

Anvi Jain
Anvi Jain
Updated on 15-Mar-2026 1K+ Views

To replace a specific value in a MongoDB array, use the $set operator combined with the $ positional operator. The positional operator identifies the array element that matches the query condition and allows you to update it. Syntax db.collection.update( { "arrayField": "valueToReplace" }, { $set: { "arrayField.$": "newValue" } } ); Sample Data db.replaceValueInArrayDemo.insertMany([ { "StudentScores": [45, 56, 78] }, { "StudentScores": [33, 90, 67] } ]); { "acknowledged": true, ...

Read More

MongoDB regex to display records whose first five letters are uppercase?

Nishtha Thakur
Nishtha Thakur
Updated on 15-Mar-2026 1K+ Views

To find MongoDB documents whose first five letters are uppercase, use the $regex operator with the pattern /^[A-Z]{5}/. The ^ ensures matching from the string beginning, and {5} specifies exactly five consecutive uppercase letters. Syntax db.collection.find({ fieldName: { $regex: /^[A-Z]{5}/ } }); Sample Data db.upperCaseFiveLetterDemo.insertMany([ { "StudentFullName": "JOHN Smith" }, { "StudentFullName": "SAM Williams" }, { "StudentFullName": "CAROL Taylor" }, { "StudentFullName": "Bob Taylor" }, { "StudentFullName": "DAVID Miller" ...

Read More

How to perform $gt on a hash in a MongoDB document?

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

The $gt operator selects documents where the value of a field is greater than a specified value. When working with embedded documents (hash/object structures), use dot notation to access nested fields within the $gt query. Syntax db.collection.find({ "parentField.nestedField": { $gt: value } }); Sample Data db.performQueryDemo.insertMany([ { "PlayerDetails": { "PlayerScore": 1000, "PlayerLevel": 2 ...

Read More
Showing 731–740 of 5,468 articles
« Prev 1 72 73 74 75 76 547 Next »
Advertisements