MongoDB Articles

Page 10 of 111

How do I get email-id from a MongoDB document and display with print()

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 491 Views

To extract email-id values from MongoDB documents and display them using print(), use the forEach() method to iterate through documents and access the email field with dot notation. Syntax db.collection.find().forEach(function(document) { print(document.fieldName); }); Sample Data db.demo690.insertMany([ {"UserName": "John", "UserEmailId": "John@gmail.com"}, {"UserName": "Bob", "UserEmailId": "Bob@gmail.com"}, {"UserName": "David", "UserEmailId": "David@gmail.com"} ]); { "acknowledged": true, "insertedIds": [ ObjectId("5ea6db31551299a9f98c939c"), ...

Read More

Increment only a single value in MongoDB document?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 181 Views

To increment only a single value in a MongoDB document, use the $inc operator with the update() method. The $inc operator increases the value of a field by the specified amount. Syntax db.collection.update( { "field": "matchValue" }, { $inc: { "fieldToIncrement": incrementValue } } ); Create Sample Data Let us create a collection with documents containing Score values ? db.demo698.insertMany([ { Score: 78 }, { Score: 56 }, { Score: 65 }, ...

Read More

Build (escape) regexp in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 397 Views

To build and escape regular expressions in MongoDB, use the regex literal syntax with the /pattern/flags format or the $regex operator. The i flag enables case-insensitive matching, allowing you to find documents regardless of text case. Syntax // Method 1: Regex literal db.collection.find({ field: /pattern/flags }); // Method 2: $regex operator db.collection.find({ field: { $regex: "pattern", $options: "flags" } }); Sample Data db.demo696.insertMany([ { Message: "/Good/" }, { Message: "(good)" }, { Message: "/Bye/" }, { ...

Read More

How can I find documents in MongoDB based on the number of matched objects within an array?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 191 Views

In MongoDB, you can find documents based on the number of matched objects within an array using aggregation pipelines with $filter and $size operators, or the $where operator for JavaScript-based queries. Syntax // Method 1: Using Aggregation Pipeline db.collection.aggregate([ { $match: { $expr: { $gte: [ { $size: { $filter: { input: "$arrayField", ...

Read More

Ignore first 4 values in MongoDB documents and display the next 3?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 165 Views

To ignore the first 4 values in MongoDB documents and display the next 3, use the $slice operator with [skip, limit] array notation in the projection stage. Syntax db.collection.find( {}, { arrayField: { $slice: [skipCount, limitCount] } } ); Sample Data db.demo693.insertMany([ { Values: [10, 746, 736, 283, 7363, 424, 3535] }, { Values: [100, 200, 300, 100, 500, 700, 900, 30000, 40003, 45999] } ]); { "acknowledged": true, ...

Read More

MongoDB query to find documents with specific FirstName and LastName

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

To find documents with specific FirstName and LastName in MongoDB, use the $and operator along with $in to match multiple values for each field. This allows you to query for documents that match any combination of the specified first and last names. Syntax db.collection.find({ $and: [ {FirstName: {$in: ["name1", "name2"]}}, {LastName: {$in: ["surname1", "surname2"]}} ] }); Create Sample Data db.demo692.insertMany([ {FirstName: "Chris", LastName: "Brown"}, ...

Read More

Set condition in MongoDB nested document?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 364 Views

To set conditions in MongoDB nested documents, use dot notation to navigate through the nested structure and apply comparison operators like $gt, $lt, or $eq to filter documents based on nested field values. Syntax db.collection.find({ "parentField.nestedField.deepField": { $operator: value } }); Sample Data db.demo688.insertMany([ { information: { id: 1, details: [ ...

Read More

Accessing inner element of JSON array in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 711 Views

To access inner element of JSON array in MongoDB, use dot notation to traverse through nested objects and arrays. This allows you to query and filter documents based on deeply nested field values. Syntax db.collection.find({"outerObject.arrayField.nestedObject.fieldName": value}) Sample Data db.demo687.insertMany([ { CountryName: 'US', info: { id: 101, details: [ ...

Read More

How to query MongoDB similar to "like" ?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 411 Views

To implement similar to "like" functionality in MongoDB, use regular expressions with the find() method. MongoDB supports regex patterns using the /pattern/flags syntax to perform case-sensitive and case-insensitive searches. Syntax db.collection.find({ fieldName: /pattern/flags }); // Common patterns: db.collection.find({ fieldName: /searchText/i }); // Case-insensitive db.collection.find({ fieldName: /^searchText/ }); // Starts with db.collection.find({ fieldName: /searchText$/ }); // Ends with db.collection.find({ fieldName: /searchText/ }); // Contains (case-sensitive) Sample ...

Read More

MongoDB aggregation to fetch documents with specific field value?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 701 Views

To fetch documents with a specific field value using MongoDB aggregation, use the aggregate() method with the $match stage to filter documents based on field criteria. Syntax db.collection.aggregate([ { $unwind: "$arrayField" }, { $match: { "arrayField.fieldName": value } }, { $project: { _id: 0 } } ]); Sample Data db.demo685.insertOne({ "details": [ { "Name": "Chris", ...

Read More
Showing 91–100 of 1,106 articles
« Prev 1 8 9 10 11 12 111 Next »
Advertisements