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 get the matching document inside an array in MongoDB?
To get the matching document inside an array in MongoDB, use the $elemMatch operator. This 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: { "field1": "value1", "field2": "value2" } } }); ...
Read MoreHow to add an extra field in a sub document in MongoDB?
To add an extra field to a subdocument in MongoDB, use the $set operator with dot notation to target specific elements within an array. Use the array index to specify which subdocument to update. Syntax db.collection.update( { "field": "value" }, { "$set": { "arrayField.index.newField": "newValue" } } ); Sample Data db.addExtraFieldDemo.insertOne({ "_id": 1, "UserName": "Larry", "UserOtherDetails": [ { ...
Read MoreHow to use $regex in MongoDB?
The $regex operator in MongoDB allows you to perform pattern matching on string fields using regular expressions. It's useful for text searches, filtering data based on patterns, and implementing case-insensitive queries. Syntax db.collection.find({ fieldName: { $regex: "pattern", $options: "options" } }); Common options include i for case-insensitive matching, m for multiline, and x for extended syntax. Sample Data db.regularExpressionDemo.insertMany([ {"UserName": "John"}, ...
Read MoreFind the exact match in array without using the $elemMatch operator in MongoDB?
To find an exact match in an array without using the $elemMatch operator in MongoDB, use the $eq operator to match the entire array structure or query specific array elements directly. Syntax // Match entire array exactly db.collection.find({"arrayField": {$eq: ["value1", "value2"]}}); // Match specific element in array db.collection.find({"arrayField": "specificValue"}); Sample Data db.equalDemo.insertMany([ {_id: 1, "StudentFriendNames": ["John", "Carol", "Sam"]}, {_id: 2, "StudentFriendNames": null}, {_id: 3, "StudentFriendNames": ["Carol"]}, {_id: 4, "StudentFriendNames": ["Sam"]} ]); { "_id" ...
Read MoreMongoDB query for Partial Object in an array
To query for a partial object in a MongoDB array, use dot notation to match specific fields within array elements or use the $elemMatch operator for more complex conditions. Syntax // Dot notation for single field db.collection.find({"arrayName.fieldName": "value"}); // $elemMatch for multiple conditions db.collection.find({"arrayName": {$elemMatch: {"field1": "value1", "field2": "value2"}}}); Sample Data db.queryForPartialObjectDemo.insertMany([ { "StudentDetails": [ {"StudentId": 1, "StudentName": "Chris"} ] ...
Read MoreGet the size of a collection in MongoDB using conditions?
To get the size of a MongoDB collection using specific conditions, you can filter documents with a query and then calculate the total size of matching documents using Object.bsonSize() within a forEach() loop. Syntax var collectionSize = 0; db.collection.find({condition}).forEach(function(document) { var size = Object.bsonSize(document); collectionSize = collectionSize + size; }); print(collectionSize); Sample Data Let us create a collection with sample documents ? db.mongoDBCollectionSizeDemo.insertMany([ {"Name": "John", "Age": 23}, {"Name": "Chris", "Age": 24}, {"Name": ...
Read MoreFind documents that contains specific field in MongoDB?
To find documents that contain a specific field in MongoDB, use the $exists operator. This operator checks whether a field exists in a document, regardless of its value. Syntax db.collection.find({ "fieldName": { $exists: true } }); Sample Data Let us create a collection with sample documents − db.findDocumentContainsSpecificFieldDemo.insertMany([ { "ProductPrices": { "Product1": 10, ...
Read MoreGetting only the first item for an array property in MongoDB?
To get only the first item from an array property in MongoDB, use the $slice operator in the projection stage. This operator limits the number of array elements returned in the query result. Syntax db.collection.find( { query }, { arrayField: { $slice: 1 } } ); Sample Data db.gettingFirstItemInArrayDemo.insertOne({ "UserId": 101, "UserName": "Carol", "UserOtherDetails": [ {"UserFriendName": "Sam"}, ...
Read MoreHow to limit the amount of characters returned from a field in a MongoDB query?
To limit the amount of characters returned from a field in a MongoDB query, use the $substr operator within an aggregation pipeline. This operator extracts a substring from a string field based on starting position and character count. Syntax db.collection.aggregate([ { $project: { fieldName: { $substr: ["$originalField", startIndex, numberOfCharacters] } } } ]); Create Sample Data db.limitTheNumberOfCharactersDemo.insertMany([ ...
Read MoreHow to pop a single value in MongoDB?
To pop a single value in MongoDB, use the $pop operator to remove the first or last element from an array field. The $pop operator takes a value of 1 to remove the last element or -1 to remove the first element. Syntax db.collection.updateOne( { query }, { $pop: { "arrayField": 1 } } // Remove last element ); db.collection.updateOne( { query }, { $pop: { "arrayField": -1 } } // Remove first element ); ...
Read More