Big Data Analytics Articles

Page 10 of 135

Set condition in MongoDB nested document?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 369 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 715 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 431 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 705 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

How do I sort natural in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 916 Views

Use $natural to sort documents in MongoDB by their natural order (the order they were inserted into the database). This operator sorts documents based on their physical storage order on disk. Syntax db.collection.find().sort({ $natural: 1 }) // Ascending (insertion order) db.collection.find().sort({ $natural: -1 }) // Descending (reverse insertion order) Sample Data Let us create a collection with documents ? db.demo684.insertMany([ {Value: 10}, {Value: 50}, {Value: 60}, {Value: 40} ]); ...

Read More

What happens when we try to add a number to undefined value?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 853 Views

If you try to add a number to an undefined value, you will get NaN (Not a Number). This happens because JavaScript cannot perform mathematical operations when one operand is undefined. Understanding NaN NaN stands for "Not a Number" and is returned when a mathematical operation cannot produce a meaningful numeric result. When you add a number to undefined, JavaScript attempts type coercion but fails, resulting in NaN. Case 1: Direct Addition with Undefined Here's what happens when you directly add a number to undefined ? var result = 10 + undefined; console.log(result); console.log(typeof ...

Read More

Count the documents with a field value beginning with 13

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 137 Views

To count documents where a field value begins with "13", use MongoDB's $regex operator with the ^ anchor for pattern matching, combined with the count() method for efficient counting. Syntax db.collection.count({ "fieldName": { "$regex": "^13" } }); Sample Data db.demo570.insertMany([ {Information: {Value: "13675"}}, {Information: {Value: "14135"}}, {Information: {Value: "15113"}}, {Information: {Value: "13141"}} ]); { "acknowledged": true, "insertedIds": [ ...

Read More

MongoDB query to match documents whose _id is in an array as part of a subdocument?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 218 Views

To match documents whose _id is in an array stored as part of a subdocument, use the $in operator combined with distinct() to extract array values from the subdocument field. Syntax db.targetCollection.find({ "_id": { "$in": db.sourceCollection.distinct("arrayField.subField", {filter}) } }); Sample Data Create a collection with an array of subdocuments containing IDs ? db.demo568.insertOne({ _id: 101, details: [ { id: 101 }, ...

Read More

MongoDB collection query to exclude some fields in find()?

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

To exclude specific fields from MongoDB query results, set the unwanted fields to 0 in the projection parameter of the find() method. This allows you to retrieve documents while hiding sensitive or unnecessary data. Syntax db.collectionName.find( { query }, { fieldToExclude1: 0, fieldToExclude2: 0 } ); Sample Data db.demo567.insertMany([ { "Name": "Chris", "Age": 21 }, { "Name": "David", "Age": 23 }, { "Name": "Bob", "Age": 22 }, { "Name": ...

Read More

Grouping the array items in MongoDB and get the count the products with similar price?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 972 Views

To group array items in MongoDB and count products with similar prices, use the $unwind operator to flatten the array, then $group by price and $sum to count occurrences. Syntax db.collection.aggregate([ { $unwind: "$arrayField" }, { $group: { "_id": "$arrayField.fieldToGroupBy", "count": { $sum: 1 } }}, { $sort: { "_id": 1 } } ]) Sample Data db.products.insertOne({ ...

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