Big Data Analytics Articles

Page 16 of 135

Sort the MongoDB documents in ascending order with aggregation?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 272 Views

To sort MongoDB documents in ascending order with aggregation, use the $sort stage in the aggregation pipeline with field: 1 (ascending) or field: -1 (descending). Syntax db.collection.aggregate([ { $sort: { fieldName: 1 } } // 1 for ascending, -1 for descending ]); Sample Data db.demo652.insertMany([ { value: 10, "details": [ { ...

Read More

Avoid MongoDB performance issues while using regex

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 514 Views

To avoid MongoDB performance issues while using regex, create appropriate text indexes on the fields you'll be searching. Text indexes enable efficient text search operations and significantly improve query performance. Syntax db.collection.createIndex({"field1": "text", "field2": "text"}); db.collection.find({"field": {"$regex": "pattern", "$options": "i"}}); Create Text Index First, create a text index on the fields you'll be searching ? db.demo531.createIndex({"CountryName": "text", "Name": "text"}); { "createdCollectionAutomatically": true, "numIndexesBefore": 1, "numIndexesAfter": 2, "ok": 1 } Sample Data ...

Read More

How to use ORDERBY in MongoDB if there are possible null values?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 782 Views

When sorting documents containing null values in MongoDB, use the sort() method. MongoDB treats null values as the smallest value, so they appear first in ascending order and last in descending order. Note − Starting in MongoDB v3.2, the $orderby operator is deprecated in the mongo shell. Use cursor.sort() instead. Syntax db.collection.find().sort({ "fieldName": 1 }); // Ascending (nulls first) db.collection.find().sort({ "fieldName": -1 }); // Descending (nulls last) db.collection.aggregate([ { $match: { "fieldName": { ...

Read More

MongoDB query to group by _id

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 973 Views

To group by _id in MongoDB, use the $group stage in an aggregation pipeline. This allows you to group documents by the _id field or set _id: null to group all documents together. Syntax db.collection.aggregate([ { $group: { _id: "$fieldName", aggregatedField: { $operator: "expression" } } } ]); ...

Read More

Matching MongoDB collection items by id?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 475 Views

To match collection items by id, use the $in operator in MongoDB. This allows you to query multiple documents by their specific ObjectId values in a single operation. Syntax db.collection.find({ _id: { $in: [ObjectId("id1"), ObjectId("id2"), ObjectId("id3")] } }); Create Sample Data db.demo528.insertMany([ {"Name": "Chris", "Age": 21}, {"Name": "Bob", "Age": 22}, {"Name": "David", "Age": 20} ]); { "acknowledged": true, "insertedIds": [ ...

Read More

Perform Group in MongoDB and sum the price record of documents

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 152 Views

To sum price records across all documents in MongoDB, use the $group stage in an aggregation pipeline with the $sum operator. Set _id: null to group all documents together. Syntax db.collection.aggregate([ { $group: { _id: null, totalFieldName: { $sum: "$fieldName" } } } ]); Sample Data ...

Read More

Updating an array with $push in MongoDB

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 496 Views

To update an array with $push in MongoDB, use the updateOne() method. The $push operator adds elements to an array field, and when combined with the positional operator $, it can target specific array elements. Syntax db.collection.updateOne( { "arrayField": { "$elemMatch": { "field": "value" } } }, { "$push": { "arrayField.$.targetField": newElement } } ); Sample Data Let us create a collection with documents ? db.demo526.insertOne({ "CountryName": "US", "TeacherName": "Bob", "StudentInformation": [ ...

Read More

MongoDB query to find multiple matchings inside array of objects?

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

To find multiple matching conditions inside an array of objects in MongoDB, use $and operator combined with $elemMatch and $regex. The $and performs a logical AND operation on multiple expressions, while $elemMatch matches documents containing an array field with at least one element matching all criteria. Syntax db.collection.find({ "$and": [ { "arrayField": { "$elemMatch": { "field1": { $regex: /pattern/i } } } }, { "arrayField": { "$elemMatch": { "field2": { $regex: /pattern/i } } } } ...

Read More

How to search date between two dates in MongoDB?

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

To search date between two dates in MongoDB, use $gte and $lt operators. The $gte operator matches dates greater than or equal to the start date, while $lt matches dates less than the end date. Syntax db.collection.find({ dateField: { $gte: new ISODate("start-date"), $lt: new ISODate("end-date") } }); Sample Data Let us create a collection with documents ? db.demo524.insertMany([ {"EndDate": new ISODate("2020-01-19")}, ...

Read More

Why does my MongoDB group query return always 0 in float conversion? How to fix it?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 179 Views

When MongoDB group queries return 0 during float conversion, it's typically because string values aren't being properly converted to numbers. Use parseFloat() or MongoDB's $toDouble operator to convert string numbers to actual numeric types. Syntax // Using parseFloat() in forEach db.collection.find({}).forEach(function(doc) { doc.field = parseFloat(doc.field); db.collection.save(doc); }); // Using $toDouble in aggregation db.collection.aggregate([ { $addFields: { "field": { $toDouble: "$field" } } } ]); Sample Data db.demo523.insertOne({ "details": { ...

Read More
Showing 151–160 of 1,348 articles
« Prev 1 14 15 16 17 18 135 Next »
Advertisements