Big Data Analytics Articles

Page 44 of 135

Fetching all documents from MongoDB Collection in a beautified form

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 397 Views

To fetch all documents from a MongoDB collection in a beautified, readable format, use the find() method combined with the pretty() method. The pretty() method formats the JSON output with proper indentation and line breaks. Syntax db.collectionName.find().pretty(); Create Sample Data Let us create a collection with some sample documents ? db.demo306.insertMany([ {"Name": "Robert", "Age": 21}, {"Name": "David", "Age": 23}, {"Name": "Mike", "Age": 25}, {"Name": "Carol", "Age": 26} ]); { ...

Read More

How to get the child of a MongoDB collection by the key?

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

To get the child of a collection in MongoDB by a specific key, use the find() method with projection. This allows you to retrieve only specific nested fields from documents that match your query criteria. Syntax db.collection.find( { "field": "value" }, { "nestedObject.key": 1 } ); Sample Data db.demo305.insertMany([ { _id: 101, FirstName: "Chris", details: { ...

Read More

Find values with OR operator in MongoDB and format the result.?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 172 Views

Use $or operator to fetch values matching any of multiple conditions and pretty() to format the result with proper indentation and readability. Syntax db.collection.find({ $or: [ { field1: value1 }, { field2: value2 } ] }).pretty(); Sample Data Let us create a collection with documents: db.demo304.insertMany([ { "StudentName": "Chris", "StudentAge": 23 }, { "StudentName": "David", "StudentAge": 22 }, ...

Read More

MongoDB query to get distinct FirstName values from documents

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 358 Views

To get distinct FirstName values from documents in MongoDB, use the distinct() method. This method returns an array of unique values for the specified field across all documents in the collection. Syntax db.collection.distinct("fieldName") Sample Data db.demo303.insertMany([ {FirstName:"Chris", LastName:"Brown"}, {FirstName:"John", LastName:"Doe"}, {FirstName:"Chris", LastName:"Smith"}, {FirstName:"John", LastName:"Smith"}, {FirstName:"David", LastName:"Miller"} ]); { "acknowledged": true, "insertedIds": [ ObjectId("5e4ea0f6f8647eb59e56202f"), ...

Read More

Field selection within MongoDB query using dot notation?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 533 Views

Field selection in MongoDB using dot notation allows you to access and filter nested fields within embedded documents or arrays. Use dot notation to specify which fields to include or exclude in query results. Syntax db.collection.find( {"parentField.nestedField": "value"}, {"parentField.specificField": 1/0} ); Sample Data db.demo302.insertMany([ {"Id": 101, "details": [{"Name": "Chris", "Age": 21, "Subject": "MySQL"}]}, {"Id": 102, "details": [{"Name": "Bob", "Age": 23, "Subject": "MongoDB"}]}, {"Id": 103, "details": [{"Name": "David", "Age": 20, "Subject": "Java"}]} ]); ...

Read More

MongoDB query to change order of array elements?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 645 Views

To change the order of array elements in MongoDB, you can use JavaScript operations with the forEach() method to swap elements or use aggregation pipeline with array operators for more complex reordering. Syntax // Method 1: Using forEach with JavaScript swap db.collection.find().forEach(function(doc) { // Swap elements using temporary variable var temp = doc.arrayField[index1]; doc.arrayField[index1] = doc.arrayField[index2]; doc.arrayField[index2] = temp; db.collection.update({_id: doc._id}, {$set: {arrayField: doc.arrayField}}); }); // Method 2: Using $set with explicit array db.collection.update( ...

Read More

MongoDB query to access an object in an array

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 587 Views

To access an object in an array in MongoDB, use dot notation to specify the field path. This allows you to query nested objects within arrays and retrieve documents that match specific criteria. Syntax db.collection.find({"arrayField.objectField": "value"}) Sample Data db.demo299.insertMany([ { "id": 100, "Name": "Robert", "details": [ { ...

Read More

MongoDB transaction & indexes for duplicate values

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 344 Views

To prevent duplicate values within array fields in MongoDB, use createIndex() with the unique option. When a unique index is applied to an array field, MongoDB treats each array element as a separate index entry, preventing any value from appearing in multiple documents. Syntax db.collection.createIndex( { "arrayField": 1 }, { "unique": true } ); Sample Data Let us create a collection with documents ? db.demo298.insertMany([ { Name: "Chris", Marks: [46, 79] }, { Name: "David", Marks: ...

Read More

MongoDB query to insert but limit the total records

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 297 Views

To insert documents while limiting the total records in a MongoDB collection, use capped collections with the capped: true option and set the max parameter to define the maximum number of documents. Syntax db.createCollection("collectionName", { capped: true, size: sizeInBytes, max: maxDocuments }); Create Capped Collection Let us create a capped collection that allows a maximum of 4 documents ? db.createCollection("demo297", {capped: true, size: 4, max: 4}); { "ok" : 1 } Insert Documents Now ...

Read More

Native Querying MongoDB inside array and get the count

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 174 Views

To query inside array and check for existence to get the count, use $exists. This operator checks whether a specific field exists within array elements and returns documents that match the condition. Syntax db.collection.count({ "arrayField.nestedField": { $exists: true } }); Sample Data db.demo296.insertMany([ { "id": 101, "Name": "Chris", "details": [ ...

Read More
Showing 431–440 of 1,348 articles
« Prev 1 42 43 44 45 46 135 Next »
Advertisements