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
Database Articles
Page 12 of 547
MongoDB aggregation to get two documents with the least marks
To get the two documents with the least marks in MongoDB, use the aggregation pipeline with $sort to order by marks in ascending order and $limit: 2 to restrict the result to only two documents. Syntax db.collection.aggregate([ { $sort: { "field": 1 } }, { $limit: 2 } ]); Sample Data db.demo709.insertMany([ { "Name": "John", "Marks": 75 }, { "Name": "Chris", "Marks": 45 }, { "Name": "David", "Marks": 54 }, ...
Read MoreFind MongoDB records based on a condition?
To find MongoDB records based on a condition, use the find() method with a query document that specifies the filtering criteria. The query document contains field-value pairs that define the conditions to match. Syntax db.collection.find({ field: value }); db.collection.find({ field: { $operator: value } }); Sample Data Let us create a collection with student documents ? db.students.insertMany([ { "Name": "John", "Marks": 54 }, { "Name": "Chris", "Marks": 35 }, { "Name": "David", "Marks": 45 }, { ...
Read MoreSet server status to inactive in a MongoDB collection with server records?
To set a server status to inactive in a MongoDB collection, use the positional operator $ with the $set update operator to target specific array elements based on matching criteria. Syntax db.collection.update( { "arrayField.matchField": "matchValue" }, { $set: { "arrayField.$.updateField": "newValue" } } ); Sample Data Let us create a collection with server records − db.demo707.insertOne({ id: 101, "serverInformation": [ { ...
Read MoreDisplay MongoDB with document and subdocument example and update
MongoDB allows storing documents with subdocuments using nested arrays and objects. This structure enables complex data relationships within a single document, making it ideal for hierarchical data. Syntax db.collectionName.insertOne({ fieldName: "value", subdocumentArray: [ { nestedField1: "value1", nestedField2: "value2" } ] }); Sample Data ...
Read MoreMongoDB query to match documents with array values greater than a specific value
To match documents with array values greater than a specific value in MongoDB, use the $elemMatch operator. The $elemMatch operator matches documents that contain an array field with at least one element that matches all the specified query criteria. Syntax db.collection.find( { "arrayField": { $elemMatch: { $gt: value } } } ); Create Sample Data Let us create a collection with documents − db.demo701.insertMany([ { "ListOfValues": [100, 200, 300] }, { "ListOfValues": [500, 700, 1000] }, ...
Read MoreMongoDB query to display documents with a specific name irrespective of case
To display documents with a specific name irrespective of case in MongoDB, use the $regex operator with the i flag for case-insensitive matching. This allows you to find documents containing a specific string value regardless of uppercase or lowercase letters. Syntax db.collection.find({ "field": { $regex: /pattern/i } }); Sample Data db.demo700.insertMany([ { details: [{ Name: "david" }] }, { details: [{ Name: "Chris" }] }, { details: [{ Name: "DAVID" }] }, { ...
Read MoreMultiple atomic updates using MongoDB?
Multiple atomic updates in MongoDB allow you to update multiple documents that match a query condition in a single operation. Use the updateMany() method or the legacy update() method with the multi parameter set to true. Syntax // Modern approach db.collection.updateMany( { filter }, { $set: { field: "newValue" } } ); // Legacy approach db.collection.update( { filter }, { $set: { field: "newValue" } }, { multi: true } ); Sample Data ...
Read MoreHow do I get email-id from a MongoDB document and display with print()
To extract email-id values from MongoDB documents and display them using print(), use the forEach() method to iterate through documents and access the email field with dot notation. Syntax db.collection.find().forEach(function(document) { print(document.fieldName); }); Sample Data db.demo690.insertMany([ {"UserName": "John", "UserEmailId": "John@gmail.com"}, {"UserName": "Bob", "UserEmailId": "Bob@gmail.com"}, {"UserName": "David", "UserEmailId": "David@gmail.com"} ]); { "acknowledged": true, "insertedIds": [ ObjectId("5ea6db31551299a9f98c939c"), ...
Read MoreIncrement only a single value in MongoDB document?
To increment only a single value in a MongoDB document, use the $inc operator with the update() method. The $inc operator increases the value of a field by the specified amount. Syntax db.collection.update( { "field": "matchValue" }, { $inc: { "fieldToIncrement": incrementValue } } ); Create Sample Data Let us create a collection with documents containing Score values ? db.demo698.insertMany([ { Score: 78 }, { Score: 56 }, { Score: 65 }, ...
Read MoreBuild (escape) regexp in MongoDB?
To build and escape regular expressions in MongoDB, use the regex literal syntax with the /pattern/flags format or the $regex operator. The i flag enables case-insensitive matching, allowing you to find documents regardless of text case. Syntax // Method 1: Regex literal db.collection.find({ field: /pattern/flags }); // Method 2: $regex operator db.collection.find({ field: { $regex: "pattern", $options: "flags" } }); Sample Data db.demo696.insertMany([ { Message: "/Good/" }, { Message: "(good)" }, { Message: "/Bye/" }, { ...
Read More