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 by Nishtha Thakur
Page 10 of 40
Can MongoDB return result of increment?
Yes, MongoDB can return the result of an increment operation using the findAndModify() method. This allows you to atomically increment a value and retrieve the updated document in a single operation. Syntax db.collection.findAndModify({ query: { /* match criteria */ }, update: { $inc: { "field": incrementValue } }, new: true }); Sample Data Let us first create a collection with a sample document: db.returnResultOfIncementDemo.insertOne({"PlayerScore": 98}); { "acknowledged": true, "insertedId": ...
Read MoreHow to access subdocument value when the key is a number in MongoDB?
To access subdocument values when the key is a number in MongoDB, use bracket notation with quotes around the numeric key. This is necessary because numeric keys are stored as strings in MongoDB subdocuments. Syntax db.collection.findOne().fieldName["numericKey"] db.collection.find({"fieldName.numericKey.subfield": value}) Sample Data db.accessSubDocumentDemo.insertOne({ "Details": { "1": { "StudentLowerScore": "33", "StudentHighScore": "55" ...
Read MoreConditional $first in MongoDB aggregation ignoring NULL?
To get the first non-NULL value in MongoDB aggregation, use the $match operator to filter out NULL values before applying $first in the $group stage. Syntax db.collection.aggregate([ { $match: { "fieldName": { $ne: null } } }, { $group: { "_id": "$groupField", "result": { $first: "$fieldName" } }} ]); Sample Data db.conditionalFirstDemo.insertMany([ {_id:100, "StudentName":"Chris", "StudentSubject":null}, ...
Read MoreProject specific array field in a MongoDB collection?
To project specific fields from an array in MongoDB, use dot notation in the projection document to select only the desired fields from nested array elements. This allows you to return specific fields from array objects while excluding unwanted data. Syntax db.collection.find( {}, { "fieldName": 1, "arrayField.nestedField": 1 } ); Create Sample Data db.projectionAnElementDemo.insertOne( { ...
Read MoreHow to keep two "columns" in MongoDB unique?
To keep two fields unique together in MongoDB, create a compound unique index on both fields. This ensures that the combination of both field values must be unique across the collection. Syntax db.collection.createIndex( {"field1": 1, "field2": 1}, {unique: true} ); Example: Create Compound Unique Index Create a unique index on StudentFirstName and StudentLastName fields ? db.keepTwoColumnsUniqueDemo.createIndex( {"StudentFirstName": 1, "StudentLastName": 1}, {unique: true} ); { "createdCollectionAutomatically": true, ...
Read MoreMongoDB query to count the size of an array distinctly?
To count the size of an array distinctly in MongoDB, use the distinct() method to get unique elements and then apply .length to count them. This eliminates duplicate values and returns only the count of unique elements. Syntax db.collection.distinct('fieldName').length; Sample Data Let us create a collection with duplicate student names ? db.countOrSizeDemo.insertMany([ {"StudentFirstName": "John"}, {"StudentFirstName": "David"}, {"StudentFirstName": "David"}, {"StudentFirstName": "Carol"}, {"StudentFirstName": "Sam"}, {"StudentFirstName": "John"} ]); ...
Read MoreHow do I search according to fields in inner classes using MongoDB db.coll.find()?
Use dot notation (.) to search in inner classes (nested objects) using MongoDB. This allows you to query specific fields within embedded documents efficiently. Syntax db.collection.find({"outerField.innerField": "value"}) Sample Data db.searchInInnerDemo.insertMany([ { "StudentFirstName": "Robert", "StudentTechnicalDetails": { "StudentBackEndTechnology": "MongoDB", "StudentLanguage": "Java" } ...
Read MoreGet at least one match in list querying with MongoDB?
To get at least one match in list querying with MongoDB, use the $in operator. This operator returns documents where the array field contains at least one of the specified values. Syntax db.collection.find({ "arrayField": { "$in": ["value1", "value2", "value3"] } }); Sample Data Let us create a collection with sample documents − db.atleastOneMatchDemo.insertMany([ {"StudentFavouriteSubject": ["MySQL", "MongoDB"]}, {"StudentFavouriteSubject": ["Java", "C", "MongoDB"]}, {"StudentFavouriteSubject": ["Python", "C++", "SQL Server"]}, {"StudentFavouriteSubject": ["Ruby", "Javascript", "C#", "MySQL"]} ]); ...
Read MoreHow to filter array elements in MongoDB?
To filter array elements in MongoDB, you can use the $setIntersection operator within the aggregation framework. This operator returns elements that exist in both the original array and your filter criteria array. Syntax db.collection.aggregate([ { $project: { fieldName: { $setIntersection: ["$arrayField", [filterValues]] } }} ]); Sample Data db.filterArrayElementsDemo.insertOne({ "Scores": [10, 45, 67, 78, ...
Read MoreRetrieve values from nested JSON array in MongoDB?
To retrieve values from nested JSON array in MongoDB, use dot notation to navigate through the nested structure. This allows you to query specific fields within arrays and embedded documents. Syntax db.collectionName.find({ "outerField.arrayField.nestedField": "value" }); Sample Data Let us create a collection with nested JSON array documents ? db.nestedJSONArrayDemo.insertMany([ { "ClientDetails": { "ClientPersonalDetails": [ ...
Read More