Big Data Analytics Articles

Page 63 of 135

Get substring in MongoDB aggregate

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 674 Views

To extract substring in MongoDB aggregation pipelines, use the $substr operator within the $project stage. This operator extracts a portion of a string starting from a specified position and length. Syntax db.collection.aggregate([ { $project: { fieldName: { $substr: ["$sourceField", startIndex, length] } } } ]) Sample Data db.demo176.insertMany([ {"ProductName": "PRODUCT-1"}, ...

Read More

How do I use MongoDB to count only collections that match two fields?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 154 Views

To count documents in MongoDB that match multiple field conditions, use the count() method with a query document containing all required field−value pairs. MongoDB will return the count of documents where all specified conditions are satisfied. Syntax db.collection.count({ "field1": "value1", "field2": "value2" }); Sample Data db.demo175.insertMany([ {"EmployeeName": "Bob", "isMarried": "YES"}, {"EmployeeName": "David", "isMarried": "NO"}, {"EmployeeName": "Mike", "isMarried": "YES"}, {"EmployeeName": "Sam", "isMarried": "NO"} ]); { ...

Read More

Evaluate one of more values from a MongoDB collection with documents

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 160 Views

To evaluate one or more values from a MongoDB collection, use the $or operator with the find() method. The $or operator performs a logical OR operation on an array of expressions and returns documents that match at least one of the conditions. Syntax db.collection.find({ $or: [ { "field1": "value1" }, { "field2": "value2" }, { "fieldN": "valueN" } ] }); Sample Data ...

Read More

Find a value in lowercase from a MongoDB collection with documents

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

To find a value in lowercase from a MongoDB collection, use the toLowerCase() method in JavaScript when constructing your query. This allows you to search for documents where a field matches a specific value in lowercase format. Syntax db.collection.find({"fieldName": "VALUE".toLowerCase()}); Sample Data Let us create a collection with documents containing subject names in different cases ? db.demo172.insertMany([ {"SubjectName": "MySQL"}, {"SubjectName": "mongodb"}, {"SubjectName": "MongoDB"} ]); { "acknowledged": true, "insertedIds": [ ...

Read More

How to re-map the fields of a MongoDB collection?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 298 Views

To re-map the fields of a MongoDB collection, use $rename operator with update() or updateMany(). This operator allows you to rename field names across documents in your collection. Syntax db.collection.updateMany( {}, { $rename: { "oldFieldName": "newFieldName" } } ); Sample Data Let us create a collection with sample documents ? db.demo171.insertMany([ { "Name": "Chris", "Details": { ...

Read More

Count number of elements in an array with MongoDB?

Nishtha Thakur
Nishtha Thakur
Updated on 15-Mar-2026 1K+ Views

To count the number of elements in an array in MongoDB, use the $size operator within the aggregation framework. The $size operator returns the number of elements in the specified array field. Syntax db.collection.aggregate([ { $project: { fieldName: { $size: "$arrayField" } } } ]); Sample Data Let us create a sample collection with array data ? ...

Read More

Get distinct first word from a string with MongoDB?

Anvi Jain
Anvi Jain
Updated on 15-Mar-2026 270 Views

To get distinct first words from a string field in MongoDB, use the distinct() method combined with JavaScript's map() and split() functions to extract and return the first word from each string. Syntax db.collection.distinct("fieldName", query).map(function(str) { return str.split(" ")[0]; }); Sample Data db.distinctFirstWordDemo.insertMany([ { "_id": 100, "StudentName": "John", "StudentFeature": "John is a good player", ...

Read More

How to query all items in MongoDB?

Nishtha Thakur
Nishtha Thakur
Updated on 15-Mar-2026 371 Views

To query all items in a MongoDB collection, use the find() method. This method retrieves all documents from a collection when called without parameters, or you can specify query criteria and projection options to filter results. Syntax db.collection.find() db.collection.find(query, projection) Create Sample Data Let us first create a collection with documents − db.queryAllItemsDemo.insertMany([ { "StudentDetails": { "StudentName": "John", ...

Read More

Display a value with $addToSet in MongoDB with no duplicate elements?

Smita Kapse
Smita Kapse
Updated on 15-Mar-2026 247 Views

The $addToSet operator in MongoDB ensures that duplicate elements are not added to an array field. When used with aggregation pipeline, it creates a set of unique values from the specified field. Syntax db.collection.aggregate([ { $group: { _id: null, fieldName: { $addToSet: "$arrayField" } } } ]); ...

Read More

Extract particular element in MongoDB within a Nested Array?

Smita Kapse
Smita Kapse
Updated on 15-Mar-2026 693 Views

To extract particular elements in MongoDB within nested arrays, use the $elemMatch operator to match array elements and projection to return only specific fields from the matched documents. Syntax db.collection.find( { "arrayField": { $elemMatch: { "nestedArrayField": { ...

Read More
Showing 621–630 of 1,348 articles
« Prev 1 61 62 63 64 65 135 Next »
Advertisements