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
Big Data Analytics Articles
Page 9 of 135
Display 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 MoreHow can I find documents in MongoDB based on the number of matched objects within an array?
In MongoDB, you can find documents based on the number of matched objects within an array using aggregation pipelines with $filter and $size operators, or the $where operator for JavaScript-based queries. Syntax // Method 1: Using Aggregation Pipeline db.collection.aggregate([ { $match: { $expr: { $gte: [ { $size: { $filter: { input: "$arrayField", ...
Read MoreIgnore first 4 values in MongoDB documents and display the next 3?
To ignore the first 4 values in MongoDB documents and display the next 3, use the $slice operator with [skip, limit] array notation in the projection stage. Syntax db.collection.find( {}, { arrayField: { $slice: [skipCount, limitCount] } } ); Sample Data db.demo693.insertMany([ { Values: [10, 746, 736, 283, 7363, 424, 3535] }, { Values: [100, 200, 300, 100, 500, 700, 900, 30000, 40003, 45999] } ]); { "acknowledged": true, ...
Read MoreMongoDB query to find documents with specific FirstName and LastName
To find documents with specific FirstName and LastName in MongoDB, use the $and operator along with $in to match multiple values for each field. This allows you to query for documents that match any combination of the specified first and last names. Syntax db.collection.find({ $and: [ {FirstName: {$in: ["name1", "name2"]}}, {LastName: {$in: ["surname1", "surname2"]}} ] }); Create Sample Data db.demo692.insertMany([ {FirstName: "Chris", LastName: "Brown"}, ...
Read More