Nishtha Thakur

Nishtha Thakur

398 Articles Published

Articles by Nishtha Thakur

Page 13 of 40

Identify last document from MongoDB find() result set?

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

To identify the last document from a MongoDB find() result set, use the sort() method with descending order on the _id field, combined with limit(1) to get only the most recent document. Syntax db.collection.find().sort({ _id: -1 }).limit(1); Sample Data Let's create a collection with sample documents to demonstrate identifying the last document ? db.identifyLastDocumentDemo.insertMany([ {"UserName": "Larry", "UserAge": 24, "UserCountryName": "US"}, {"UserName": "Chris", "UserAge": 21, "UserCountryName": "UK"}, {"UserName": "David", "UserAge": 25, "UserCountryName": "AUS"}, {"UserName": "Sam", "UserAge": ...

Read More

Match between fields in MongoDB aggregation framework?

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

To match between fields in MongoDB aggregation framework, use the $cmp operator within a $project stage to compare field values, followed by a $match stage to filter results based on the comparison. Syntax db.collection.aggregate([ { $project: { comparisonResult: { $cmp: ["$field1", "$field2"] } } }, { $match: { comparisonResult: ...

Read More

How to hide _id from Aggregation?

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

To hide the _id field from aggregation results in MongoDB, use the $project stage with _id: 0 to exclude it while specifying other fields to include. Syntax db.collectionName.aggregate([ { $project: { _id: 0, fieldName1: 1, fieldName2: 1 } ...

Read More

How to retrieve a value from MongoDB by its key name?

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

To retrieve a value from MongoDB by its key name, use projection in the find() method to specify which fields to return while excluding others. Syntax db.collectionName.find({}, {"fieldName": 1}); Set the field value to 1 to include it, or 0 to exclude it from the result. Sample Data db.retrieveValueFromAKeyDemo.insertMany([ {"CustomerName": "Larry", "CustomerAge": 21, "CustomerCountryName": "US"}, {"CustomerName": "Chris", "CustomerAge": 24, "CustomerCountryName": "AUS"}, {"CustomerName": "Mike", "CustomerAge": 26, "CustomerCountryName": "UK"} ]); { "acknowledged": true, ...

Read More

How to count number of keys in a MongoDB document?

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

There is no built-in function to count the number of keys in a MongoDB document. You need to write JavaScript code in the MongoDB shell to iterate through the document and count its fields. Syntax myDocument = db.collection.findOne({}); numberOfKeys = 0; for(key in myDocument) { numberOfKeys++; } print("Total keys: " + numberOfKeys); Sample Data Let us create a collection with a document ? db.numberofKeysInADocumentDemo.insertOne({ "UserName": "John", "UserAge": 21, "UserEmailId": "john12@gmail.com", "UserCountryName": "US" }); ...

Read More

Get database data size in MongoDB?

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

To get the database data size in MongoDB, you can use the db.stats() method. This method returns detailed statistics about the current database including data size, storage size, and other useful metrics. Syntax db.stats() Example 1: Check Current Database Statistics First, check which database you are currently using ? db test Now get the database statistics for the 'test' database ? db.stats() { "db" : "test", "collections" : 114, "views" ...

Read More

Querying array elements with MongoDB?

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

MongoDB provides simple and efficient ways to query documents that contain specific values within array fields. You can search for documents where an array contains a particular element using standard find() operations. Syntax db.collection.find({arrayFieldName: "value"}); This syntax returns all documents where the specified array field contains the given value. Sample Data Let us create a collection with sample documents to demonstrate array querying ? db.queryArrayElementsDemo.insertMany([ { "StudentName": "John", "StudentFavouriteSubject": ["C", "Java"] ...

Read More

Perform aggregation sort in MongoDB?

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

To perform aggregation sort in MongoDB, use the aggregate() method with the $sort stage. The $sort stage orders documents by specified fields in ascending (1) or descending (-1) order. Syntax db.collection.aggregate([ { $group: { ... } }, { $sort: { field: 1 } } // 1 = ascending, -1 = descending ]); Sample Data db.aggregationSortDemo.insertMany([ {"StudentId": 98, "StudentFirstName": "John", "StudentLastName": "Smith"}, {"StudentId": 128, "StudentFirstName": "Carol", "StudentLastName": "Taylor"}, {"StudentId": 110, "StudentFirstName": ...

Read More

Getting the highest value of a column in MongoDB?

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

To get the highest value of a column in MongoDB, use the sort() method with limit(1) to retrieve the document containing the maximum value for a specific field. Syntax db.collection.find().sort({"fieldName": -1}).limit(1); The -1 parameter sorts in descending order, placing the highest value first. Sample Data Let's create sample documents to demonstrate finding the highest value ? db.gettingHighestValueDemo.insertMany([ {"Value": 1029}, {"Value": 3029}, {"Value": 1092}, {"Value": 18484}, {"Value": 37474}, ...

Read More

List all values of a certain field in MongoDB?

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

To get the list of all values of certain fields in MongoDB, you can use the distinct() method. This method returns an array of unique values for a specified field across all documents in a collection. Syntax db.collectionName.distinct("fieldName"); Create Sample Data Let us create a collection with sample documents to demonstrate the distinct() method ? db.listAllValuesDemo.insertMany([ {"ListOfValues": [10, 20, 30]}, {"ListOfValues": [40, 50, 60]}, {"ListOfValues": [10, 20, 30]}, {"ListOfValues": [40, 50, 70]} ]); ...

Read More
Showing 121–130 of 398 articles
« Prev 1 11 12 13 14 15 40 Next »
Advertisements