Nishtha Thakur

Nishtha Thakur

398 Articles Published

Articles by Nishtha Thakur

Page 10 of 40

Can MongoDB return result of increment?

Nishtha Thakur
Nishtha Thakur
Updated on 15-Mar-2026 164 Views

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 More

How to access subdocument value when the key is a number in MongoDB?

Nishtha Thakur
Nishtha Thakur
Updated on 15-Mar-2026 184 Views

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 More

Conditional $first in MongoDB aggregation ignoring NULL?

Nishtha Thakur
Nishtha Thakur
Updated on 15-Mar-2026 621 Views

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 More

Project specific array field in a MongoDB collection?

Nishtha Thakur
Nishtha Thakur
Updated on 15-Mar-2026 267 Views

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 More

How to keep two "columns" in MongoDB unique?

Nishtha Thakur
Nishtha Thakur
Updated on 15-Mar-2026 1K+ Views

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 More

MongoDB query to count the size of an array distinctly?

Nishtha Thakur
Nishtha Thakur
Updated on 15-Mar-2026 302 Views

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 More

How do I search according to fields in inner classes using MongoDB db.coll.find()?

Nishtha Thakur
Nishtha Thakur
Updated on 15-Mar-2026 201 Views

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 More

Get at least one match in list querying with MongoDB?

Nishtha Thakur
Nishtha Thakur
Updated on 15-Mar-2026 474 Views

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 More

How to filter array elements in MongoDB?

Nishtha Thakur
Nishtha Thakur
Updated on 15-Mar-2026 355 Views

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 More

Retrieve values from nested JSON array in MongoDB?

Nishtha Thakur
Nishtha Thakur
Updated on 15-Mar-2026 1K+ Views

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
Showing 91–100 of 398 articles
« Prev 1 8 9 10 11 12 40 Next »
Advertisements