Articles on Trending Technologies

Technical articles with clear explanations and examples

How to search in array of object in MongoDB?

Anvi Jain
Anvi Jain
Updated on 15-Mar-2026 32K+ Views

To search in an array of objects in MongoDB, use the $elemMatch operator. This operator matches documents where at least one array element satisfies multiple query conditions within the same object. Syntax db.collection.find({ "arrayField": { $elemMatch: { "field1": "value1", "field2": "value2" } } }); Sample Data ...

Read More

How to copy a collection from one database to another in MongoDB?

Smita Kapse
Smita Kapse
Updated on 15-Mar-2026 2K+ Views

In MongoDB, there is no built-in command to copy a collection from one database to another. To achieve this, use the find().forEach() method combined with getSiblingDB() to iterate through documents and insert them into the destination database. Syntax db.collectionName.find().forEach(function(doc){ db.getSiblingDB('destinationDatabase')['collectionName'].insert(doc); }); Create Sample Data First, let's create a collection in the test database with some documents ? use test; db.userCollection.insertMany([ {"User_Id": 101, "UserName": "Larry"}, {"User_Id": 102, "UserName": "Maxwell"}, {"User_Id": 103, "UserName": "Robert"} ]); ...

Read More

How to perform descending order sort in MongoDB?

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

To sort documents in descending order in MongoDB, use the sort() method with -1 as the value for the field you want to sort by. Syntax db.yourCollectionName.find().sort({yourField: -1}); For ascending order, use 1 instead of -1 ? db.yourCollectionName.find().sort({yourField: 1}); Sample Data Let us create a collection with documents to demonstrate descending sort ? db.sortingDemo.insertMany([ {"Value": 100}, {"Value": 1}, {"Value": 150}, {"Value": 250}, {"Value": 5}, ...

Read More

How to perform ascending order sort in MongoDB?

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

To sort documents in ascending order in MongoDB, use the sort() method with a field value of 1. This arranges documents from lowest to highest based on the specified field. Syntax db.collectionName.find().sort({fieldName: 1}); Sample Data Let us create a collection with sample documents to demonstrate sorting ? db.sortingDemo.insertMany([ {"Value": 100}, {"Value": 1}, {"Value": 150}, {"Value": 250}, {"Value": 5}, {"Value": 199}, {"Value": 243}, ...

Read More

Get distinct record values in MongoDB?

Nishtha Thakur
Nishtha Thakur
Updated on 14-Mar-2026 565 Views

The distinct() method in MongoDB returns unique values for a specified field across all documents in a collection, automatically removing duplicates. Syntax db.collectionName.distinct("fieldName"); Sample Data db.distinctRecordDemo.insertMany([ {"StudentId": 1, "StudentName": "John", "StudentAge": 21}, {"StudentId": 2, "StudentName": "John", "StudentAge": 22}, {"StudentId": 3, "StudentName": "Carol", "StudentAge": 21}, {"StudentId": 4, "StudentName": "Carol", "StudentAge": 26}, {"StudentId": 5, "StudentName": "Sam", "StudentAge": 24}, {"StudentId": 6, "StudentName": "Mike", "StudentAge": 27}, {"StudentId": 7, "StudentName": ...

Read More

MongoDB SELECT COUNT GROUP BY?

Anvi Jain
Anvi Jain
Updated on 14-Mar-2026 344 Views

To perform a SELECT COUNT GROUP BY operation in MongoDB, use the $group stage in the aggregation pipeline with the $sum accumulator to count documents grouped by a specific field. Syntax db.collection.aggregate([ { $group: { _id: "$fieldName", count: { $sum: 1 } } } ]); Sample Data Let's create a collection with student records to demonstrate counting by StudentId ? db.countGroupByDemo.insertMany([ {"StudentId": 10, "StudentName": "John"}, {"StudentId": 10, "StudentName": "Carol"}, {"StudentId": 20, "StudentName": "Sam"}, ...

Read More

Find MongoDB records where array field is not empty?

Smita Kapse
Smita Kapse
Updated on 14-Mar-2026 3K+ Views

To find MongoDB records where an array field is not empty, use the $ne (not equal) operator with an empty array []. You can also combine it with $exists to ensure the field exists and is not empty. Syntax db.collection.find({ "arrayField": { $exists: true, $ne: [] } }); Sample Data db.arrayFieldIsNotEmptyDemo.insertMany([ {"StudentName": "Larry", "StudentTechnicalSubject": ["Java", "C"]}, {"StudentName": "Mike", "StudentTechnicalSubject": []}, {"StudentName": "Sam", "StudentTechnicalSubject": ["MongoDB"]}, {"StudentName": "Carol", "StudentTechnicalSubject": []}, {"StudentName": ...

Read More

Check that Field Exists with MongoDB?

Nishtha Thakur
Nishtha Thakur
Updated on 14-Mar-2026 307 Views

In MongoDB, you can check if a field exists in documents using the $exists operator. To ensure the field is not null, combine it with the $ne operator. Syntax db.collection.find({ "fieldName": { $exists: true, $ne: null } }) Sample Data db.checkFieldExistDemo.insertMany([ { "EmployeeId": 1, "EmployeeName": "John", "isMarried": true, "EmployeeSalary": 4648585 }, ...

Read More

Get MongoDB documents with max attribute per group in a collection?

Anvi Jain
Anvi Jain
Updated on 14-Mar-2026 443 Views

To get documents with the maximum attribute per group in a MongoDB collection, use the $sort operator combined with the $group stage in an aggregation pipeline. This approach sorts documents by the grouping field and the target attribute, then uses $first to select the document with the highest value per group. Sample Data db.maxAttributePerGroup.insertMany([ { "StudentFirstName": "John", "StudentLastName": "Smith", "StudentAge": 29, ...

Read More

How to replace substring in MongoDB document?

Smita Kapse
Smita Kapse
Updated on 14-Mar-2026 1K+ Views

In MongoDB, you can replace substrings in documents using JavaScript's replace() function within a forEach() loop. This approach finds documents, modifies the substring, and updates them back to the collection. Sample Data db.replaceSubstringDemo.insertOne({ "WebsiteURL": "www.gogle.com" }); { "acknowledged": true, "insertedId": ObjectId("5c76eaf21e9c5dd6f1f78276") } Let's verify the document was inserted ? db.replaceSubstringDemo.find().pretty(); { "_id": ObjectId("5c76eaf21e9c5dd6f1f78276"), "WebsiteURL": "www.gogle.com" } Replace Substring Using forEach() To replace "gogle" with ...

Read More
Showing 23831–23840 of 61,297 articles
Advertisements