Big Data Analytics Articles

Page 53 of 135

Implement MongoDB Aggregate - unwind, group and project?

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

The MongoDB aggregation framework allows you to process data through a pipeline of stages. Three powerful operators - $unwind, $group, and $project - can be combined to deconstruct arrays, group documents, and shape the output. Syntax db.collection.aggregate([ { $unwind: "$arrayField" }, { $group: { _id: "$groupField", result: { $operator: "$field" } } }, { $project: { field1: 1, field2: 1, _id: 0 } } ]); Sample Data db.demo238.insertMany([ { ...

Read More

Remove all except a single field from a nested document via projection in MongoDB

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 423 Views

To remove all except a single field from a nested document in MongoDB, use projection with the find() method. Set unwanted fields to 0 to exclude them, keeping only the desired field visible. Syntax db.collection.find( {}, { "nestedDocument.unwantedField1": 0, "nestedDocument.unwantedField2": 0 } ); Sample Data db.demo237.insertOne({ _id: 101, Product: { ...

Read More

How to sort, select and query subdocument in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 335 Views

To sort, select and query subdocument in MongoDB, use the aggregation pipeline with $unwind, $project, and $sort stages. This approach extracts subdocument fields, selects specific fields, and sorts the results. Syntax db.collection.aggregate([ { $unwind: "$subdocumentField" }, { $project: { field1: "$subdocumentField.field1", field2: "$subdocumentField.field2" }}, { $sort: { field1: 1 } } ]); Sample Data db.demo236.insertMany([ {"details":{"Name":"Chris", ...

Read More

Is there any way in MongoDB to get the inner value of json data?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 249 Views

To get inner values of JSON data in MongoDB, use the find() method along with dot notation to access nested fields within documents and arrays. Syntax db.collection.find( {}, { "outerField.innerField": 1, "_id": 0 } ); Sample Data db.demo235.insertOne({ "id": 101, "details": [ { "Name": "Chris Brown", ...

Read More

Using MongoDB Aggregate and GroupBy to get the frequency of name record

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 213 Views

To get the frequency of name records in MongoDB, use the aggregation framework with $group stage to group documents by name and count occurrences using $sum. Syntax db.collection.aggregate([ { $group: { _id: "$fieldName", Frequency: { $sum: 1 } } } ]); Create Sample Data db.demo232.insertMany([ ...

Read More

Change a unique index to a sparse unique index in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 275 Views

To change a unique index to a sparse unique index in MongoDB, you must drop the existing index and recreate it with the sparse: true option. MongoDB doesn't support modifying index properties directly. Syntax // Drop existing unique index db.collection.dropIndex("indexName"); // Create sparse unique index db.collection.createIndex({"field": 1}, {unique: true, sparse: true}); Example: Converting Unique to Sparse Unique Index Step 1: Create Initial Unique Index db.demo229.createIndex({"ClientName": 1}, {unique: true}); { "createdCollectionAutomatically": true, "numIndexesBefore": 1, "numIndexesAfter": 2, ...

Read More

Find document in MongoDB where at least one item from an array is not in the other?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 213 Views

To find documents in MongoDB where at least one item from an array is not in another specified set, use the $nin operator or regex patterns to match arrays containing elements outside your target list. Syntax // Using $nin operator db.collection.find({ "arrayField": { $nin: ["value1", "value2"] } }); // Using regex for negative lookahead db.collection.find({ "arrayField": /^(?!value1|value2)/ }); Sample Data db.demo228.insertMany([ { "Subjects": ["MongoDB", "Java"] }, { "Subjects": ["MongoDB", "Java", "MySQL"] }, { "Subjects": ["Python", "JavaScript"] } ]); ...

Read More

Updating a set of documents from a list of key value pairs in MongoDB

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 489 Views

To update multiple documents from a list of key-value pairs in MongoDB, use bulk operations with initializeUnorderedBulkOp() to efficiently process multiple updates in a single operation. Syntax var bulkUpdateValue = [{"_id": "key1", "field": "value1"}, {"_id": "key2", "field": "value2"}]; var bulkUpdate = db.collection.initializeUnorderedBulkOp(); for (var i = 0; i < bulkUpdateValue.length; i++){ var updateItem = bulkUpdateValue[i]; bulkUpdate.find({_id: updateItem._id}).update({$set: {field: updateItem.field}}); } bulkUpdate.execute(); Sample Data db.demo227.insertMany([ {"_id": "101", "Name": "Chris"}, {"_id": "102", "Name": "Bob"} ]); { ...

Read More

MongoDB query to display all the values excluding the id?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 630 Views

To exclude the _id field from MongoDB query results, use the $project operator in an aggregation pipeline. The $project stage allows you to include or exclude fields, suppress the _id field, and rename existing fields. Syntax db.collection.aggregate([ { $project: { _id: 0, field1: 1, field2: 1 } } ]); Sample Data db.demo226.insertMany([ { "Name": "Chris", "Age": 21 }, { "Name": "Bob", "Age": 20 }, { "Name": "David", "Age": 22 } ]); { ...

Read More

How to compare multiple properties in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 358 Views

To compare multiple properties in MongoDB, you can use the $where operator or the $expr operator. The $where allows JavaScript expressions to compare fields, while $expr uses aggregation expressions for better performance. Syntax // Using $where (JavaScript expression) db.collection.find({ $where: "this.field1 > this.field2" }); // Using $expr (Aggregation expression - Recommended) db.collection.find({ $expr: { $gt: ["$field1", "$field2"] } }); Sample Data db.demo223.insertMany([ {"Scores": [56, 78]}, {"Scores": [88, 45]}, {"Scores": [98, 79]} ]); { ...

Read More
Showing 521–530 of 1,348 articles
« Prev 1 51 52 53 54 55 135 Next »
Advertisements