Big Data Analytics Articles

Page 39 of 135

Working with MongoDB concatArrays in project on existing multi-array field

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 432 Views

The $concatArrays operator is used to concatenate arrays and return the concatenated array. When working with multi-dimensional arrays, you can combine it with $reduce to flatten and concatenate nested arrays. Syntax { "$project": { "newFieldName": { "$reduce": { "input": "$arrayField", "initialValue": [], "in": { "$concatArrays": ["$$this", "$$value"] } } } ...

Read More

How to calculate sum in MongoDB with aggregate()?

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

To calculate sum in MongoDB, use the $sum operator along with the aggregate() method. The $sum operator is used within a $group stage to calculate the total of numeric field values across documents. Syntax db.collection.aggregate([ { $group: { _id: null, totalFieldName: { $sum: "$fieldName" } } } ]); ...

Read More

How to update only one property in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 507 Views

To update only one property in MongoDB, use the $set operator to modify a specific field without affecting other properties of the document. For arrays, use $addToSet or $push operators. Syntax db.collection.updateOne( { "field": "value" }, { $set: { "propertyToUpdate": "newValue" } } ); Sample Data db.demo336.insertMany([ { "Name": "Chris", "Score": [45, 67, 78] }, { "Name": "David", "Score": [89, 93, 47] } ]); { "acknowledged": true, ...

Read More

MongoDB query to convert a string with commas to double

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 850 Views

To convert a string with commas to a double value in MongoDB, use the aggregate pipeline with $split, $reduce, and $convert operators to remove commas and convert the result to a numeric type. Syntax db.collection.aggregate([ { $project: { convertedField: { $convert: { ...

Read More

MongoDB query to add matched key to list after query?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 158 Views

To add matched elements to a list after querying in MongoDB, use the $filter operator within an aggregation pipeline. This allows you to filter array elements based on specific conditions and return only the matching elements. Syntax db.collection.aggregate([ { $addFields: { arrayField: { $filter: { ...

Read More

Create an index for text search in MongoDB

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 220 Views

To create an index for text search in MongoDB, use the createIndex() method with the "text" index type. This enables full-text search capabilities on string fields using the $text operator. Syntax db.collection.createIndex({ fieldName: "text" }); db.collection.find({ $text: { $search: "searchTerm" } }); Sample Data Let us create a collection with sample documents: db.demo331.insertMany([ { "Words": "This is a MySQL" }, { "Words": "THIS is a MongoDB" } ]); { "acknowledged": true, "insertedIds": [ ...

Read More

Restrict returned data in MongoDB to display only specific values from documents

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 447 Views

To restrict returned data in MongoDB, use the projection parameter in the find() method. Use values 1 to include fields and 0 to exclude fields from the query results. Syntax db.collection.find(query, projection) Where: query − Filter criteria (empty {} for all documents) projection − Fields to include (1) or exclude (0) Sample Data Let us create a collection with documents − db.demo330.insertMany([ {"Id": 101, "Name": "Chris", "Age": 21}, {"Id": 102, "Name": "Sam", "Age": 24}, {"Id": 103, "Name": ...

Read More

Using findOneAndUpdate () to update in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 386 Views

The findOneAndUpdate() method updates a single document based on the filter criteria and returns the document (either the original or modified version based on options). Syntax db.collection.findOneAndUpdate(filter, update, options) filter − Query criteria to match the document update − Update operations to perform options − Optional parameters like returnNewDocument, sort, etc. Sample Data Let us create a collection with sample documents − db.demo328.insertMany([ {Name: "Chris", Marks: 67}, {Name: "David", Marks: 78}, {Name: "Bob", Marks: 97} ]); ...

Read More

MongoDB query to set user defined variable into query?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 519 Views

To set user-defined variables in MongoDB queries, use the var keyword to declare variables and reference them directly in your query conditions. Syntax var variableName = "value"; db.collection.find({"fieldName": variableName}); Sample Data db.demo327.insertMany([ {"FirstName": "Chris", "LastName": "Brown"}, {"FirstName": "David", "LastName": "Miller"}, {"FirstName": "John", "LastName": "Doe"}, {"FirstName": "John", "LastName": "Smith"} ]); { "acknowledged": true, "insertedIds": [ ObjectId("5e516952f8647eb59e562076"), ...

Read More

Accessing array values in a MongoDB collection to fetch a specific document

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 294 Views

To access array values in MongoDB and fetch a specific document, use dot notation to query nested fields within arrays. This allows you to search for documents containing specific values in their array elements. Syntax db.collection.find({ "arrayName.fieldName": "value" }); Sample Data Let us create a collection with documents containing ProductDetails arrays ? db.demo326.insertMany([ { "id": 101, "ProductDetails": [ { "ProductId": ...

Read More
Showing 381–390 of 1,348 articles
« Prev 1 37 38 39 40 41 135 Next »
Advertisements