Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Articles on Trending Technologies
Technical articles with clear explanations and examples
How to search in array of object in MongoDB?
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 MoreHow to copy a collection from one database to another in MongoDB?
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 MoreHow to perform descending order sort in MongoDB?
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 MoreHow to perform ascending order sort in MongoDB?
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 MoreGet distinct record values in MongoDB?
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 MoreMongoDB SELECT COUNT GROUP BY?
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 MoreFind MongoDB records where array field is not empty?
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 MoreCheck that Field Exists with MongoDB?
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 MoreGet MongoDB documents with max attribute per group in a collection?
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 MoreHow to replace substring in MongoDB document?
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