Database Articles

Page 13 of 547

MongoDB - how can I access fields in a document?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 416 Views

To access fields in a MongoDB document, use the find() method with optional field projection to specify which fields to return. You can access all fields or select specific ones using projection syntax. Syntax // Access all fields db.collection.find(query); // Access specific fields using projection db.collection.find(query, {field1: 1, field2: 1, _id: 0}); Sample Data db.demo565.insertMany([ { id: 101, Name: "David", CountryName: "US" ...

Read More

Make MongoDB replace single array value with string?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 732 Views

To replace a single array value with a string in MongoDB, 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.updateMany( { "arrayField": "valueToMatch" }, { "$set": { "arrayField.$": "newValue" } } ); Sample Data db.demo564.insertOne({ "StudentName": ["Chris", "David", "Mike", "Sam"] }); { "acknowledged": true, "insertedId": ObjectId("5e90880a39cfeaaf0b97b576") } ...

Read More

How to get items with a specific value from documents using MongoDB shell?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 505 Views

To get documents with a specific field value in MongoDB, use the find() method with a query filter. This allows you to retrieve only the documents that match your specified criteria. Syntax db.collection.find( { "field": "value" }, { "field1": 1, "field2": 1 } ) The first parameter is the query filter, and the second optional parameter is the projection to specify which fields to include or exclude. Sample Data db.demo563.insertMany([ { "Name": "Chris", "Age": 21, "isMarried": true }, ...

Read More

How to search for a record (field) and then delete it in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 507 Views

To search for a field and then delete it in MongoDB, use $exists to find documents containing the field, then $unset to remove it. The $unset operator deletes a particular field from documents. Syntax db.collection.update( { "fieldName": { $exists: true } }, { $unset: { "fieldName": "" } }, { multi: true } ); Sample Data Let us create a collection with documents ? db.demo562.insertMany([ { "Name": "Chris", "Age": 21 }, { ...

Read More

Randomizing unique data with MongoDB and placing values for emailid with wordnJohn in the beginning

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 118 Views

To randomize unique data with MongoDB and generate email addresses starting with "John", use Math.random() within JavaScript functions to create unique values for each document. Syntax db.collection.find().forEach(function(doc){ db.collection.update( {_id : doc._id}, {$set: {EmailId: 'John' + Math.random() * multiplier + '@domain.com'}} ) }) Sample Data First, create a collection with null EmailId fields ? db.demo561.insertMany([ {EmailId: null}, {EmailId: null}, ...

Read More

Fetch data between two dates and with a specific value in MongoDB. Group and get the sum with count?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 688 Views

To fetch data between two dates and with a specific value in MongoDB, use $match to filter documents and $group to aggregate results. The $gte and $lte operators handle date range filtering. Syntax db.collection.aggregate([ { $match: { "fieldName": "specificValue", "dateField": { "$gte": ISODate("startDate"), ...

Read More

GROUP BY array of document to get the count of repeated Age values

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 183 Views

To GROUP BY array of document to get count of repeated Age values, use the aggregation pipeline with $unwind to flatten the array and $group to count occurrences by Age. Syntax db.collection.aggregate([ { $unwind: "$arrayField" }, { $group: { _id: "$arrayField.groupingField", count: { $sum: 1 } } }, { $sort: { _id: 1 } } ]); Sample Data db.demo559.insertOne({ details: [ { Name: "Chris", Age: 21 }, ...

Read More

MongoDB aggregate to get the count of field values of corresponding duplicate names?

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

To count field values for corresponding duplicate names in MongoDB, use the aggregation pipeline with $unwind, $group, and $project stages to flatten arrays, group by names, and calculate counts. Syntax db.collection.aggregate([ { $unwind: "$arrayField" }, { $group: { _id: "$arrayField.nameField", count: { $sum: 1 }, otherFields: { $push: "$arrayField.otherField" } }}, { $project: { ...

Read More

How to run MongoDB query to update only a specific field value?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 251 Views

To update only a specific field value in MongoDB, use the $set operator with the update() method. This allows you to modify particular fields without affecting other document data. Syntax db.collection.update( { "field": "matchValue" }, { $set: { "field": "newValue" } } ); Create Sample Data Let us create a collection with sample documents ? db.demo557.insertMany([ { Name: "Chris" }, { Name: "David" } ]); { "acknowledged": true, ...

Read More

Fetch records from a subdocument array wherein id begins from 234 in MongoDB

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 140 Views

To fetch records from a subdocument array where id begins with specific digits like "234", use MongoDB's aggregation pipeline with $unwind, $match, and $regex operators. The $unwind deconstructs the array, $regex filters by pattern, and $group with $push reconstructs matching elements. Syntax db.collection.aggregate([ { $match: { "_id": documentId } }, { $unwind: "$arrayField" }, { $match: { "arrayField.id": { $regex: /^pattern/ } } }, { $group: { _id: "$_id", "result": { $push: "$arrayField" } } } ]); Sample Data ...

Read More
Showing 121–130 of 5,468 articles
« Prev 1 11 12 13 14 15 547 Next »
Advertisements