Big Data Analytics Articles

Page 54 of 135

MongoDB query to add new array element in document

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 544 Views

To add new array element in a MongoDB document, use the $ positional operator along with $set in an update operation. This allows you to add a new field to a specific element within an array. Syntax db.collection.update( {"arrayField.existingField": "matchValue"}, {"$set": {"arrayField.$.newField": "newValue"}} ); Sample Data db.demo222.insertOne({ "details": [ { "StudentName": "Chris", ...

Read More

MongoDB regular expression to fetch record with specific name "John", instead of "john

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 134 Views

To fetch records with a specific case-sensitive name using MongoDB regular expressions, use the exact pattern match syntax /searchWord/. By default, MongoDB regex is case-sensitive, so /John/ will match "John" but not "john". Syntax db.collection.find({"field": /exactPattern/}); Sample Data db.demo221.insertMany([ {"Details": {"StudentName": "Chris", "StudentAge": 21}}, {"Details": {"StudentName": "John", "StudentAge": 20}}, {"Details": {"StudentName": "Bob", "StudentAge": 22}}, {"Details": {"StudentName": "john", "StudentAge": 24}} ]); { "acknowledged": true, "insertedIds": [ ...

Read More

Get all embedded documents with "isMarried" status in a MongoDB collection

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 256 Views

To get all embedded documents with specific fields in MongoDB, use the $project aggregation operator. This allows you to extract and display specific fields from embedded documents. Syntax db.collection.aggregate([ { $project: { "fieldName": "$embeddedDocument.fieldName", "anotherField": "$embeddedDocument.anotherField" } } ]); Sample Data db.demo220.insertMany([ ...

Read More

Searching a specific domain name from URL records in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 343 Views

To search for a specific domain name from URL records in MongoDB, use the regex pattern with the /i flag for case-insensitive matching. This allows you to find URLs containing specific domain names regardless of their capitalization. Syntax db.collection.find({ "fieldName": /domainName/i }); Sample Data Let us create a collection with URL documents − db.demo219.insertMany([ {"details": {"WebsiteURL": "www.EXAMPLE.com"}}, {"details": {"WebsiteURL": "www.gmail.com"}}, {"details": {"WebsiteURL": "www.example.com"}} ]); { "acknowledged": true, ...

Read More

Using aggregation pipeline to fetch records in MongoDB

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 248 Views

The MongoDB aggregation pipeline processes documents through multiple stages, where each stage transforms the documents as they pass through the pipeline. It's a powerful framework for data analysis and transformation operations. Syntax db.collection.aggregate([ { $stage1: { /* stage operations */ } }, { $stage2: { /* stage operations */ } }, { $stageN: { /* stage operations */ } } ]) Sample Data db.demo218.insertMany([ {"Name": "Chris", "Branch": "CS", "Marks": [65, 78, 36, 90]}, ...

Read More

How to trim spaces from field in MongoDB query?

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

To trim spaces from field in MongoDB, use the $trim operator within an aggregation pipeline. The $trim operator removes whitespace characters from the beginning and end of a string. Syntax db.collection.aggregate([ { $project: { fieldName: { $trim: { input: "$sourceField" } } }} ]) Sample Data db.demo217.insertMany([ {"FullName": " Chris Brown"}, {"FullName": " David Miller"}, {"FullName": " John Doe"} ]); { ...

Read More

Projection of one column in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 241 Views

In MongoDB, projection allows you to display only specific fields from documents instead of returning all fields. To project a single column (field), use the find() method with a projection document that specifies which fields to include or exclude. Syntax db.collection.find({}, {fieldToShow: 1, _id: 0}); Or exclude unwanted fields: db.collection.find({}, {fieldToHide: 0, anotherField: 0}); Sample Data Let us first create a collection with documents − db.demo216.insertMany([ {"ClientName": "John", "ClientAge": 34}, {"ClientName": "Bob", "ClientAge": 32}, {"ClientName": "Mike", ...

Read More

How to insert a boolean field in MongoDB?

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

To insert a boolean field in MongoDB, use the true or false keywords directly in your document. MongoDB natively supports boolean data types, making it simple to store yes/no, on/off, or enabled/disabled states. Syntax db.collection.insertOne({ "fieldName": true, "anotherField": false }); Sample Data Let us create a collection with employee documents containing boolean fields − db.demo215.insertMany([ {"EmployeeDetails": [{"EmployeeName": "David", "isMarried": false, "Salary": 56000}]}, {"EmployeeDetails": [{"EmployeeName": "Bob", "isMarried": true, "Salary": 60000}]}, {"EmployeeDetails": [{"EmployeeName": "Chris", ...

Read More

Aggregate multiple arrays into one huge array with MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 420 Views

To aggregate multiple arrays into a single array in MongoDB, use the $concatArrays operator within the $project stage of an aggregation pipeline. This combines arrays from different fields into one unified array. Syntax db.collection.aggregate([ { $project: { combinedArray: { $concatArrays: ["$array1", "$array2", "$array3"] } ...

Read More

Using addFields pipeline and run with the MongoDB filter operator

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

The $addFields pipeline stage combined with the $filter operator allows you to add new fields or modify existing ones by filtering array elements based on specific conditions. Syntax db.collection.aggregate([ { $addFields: { "fieldName": { $filter: { input: ...

Read More
Showing 531–540 of 1,348 articles
« Prev 1 52 53 54 55 56 135 Next »
Advertisements