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
MongoDB Articles
Page 101 of 111
How to get distinct list of sub-document field values in MongoDB?
To get distinct list of sub-document field values in MongoDB, use the distinct() method with dot notation to access nested fields within arrays or embedded documents. Syntax db.collection.distinct("outerField.innerField"); Sample Data Let us create a collection with student documents containing nested arrays ? db.getDistinctListOfSubDocumentFieldDemo.insertMany([ { "StudentId": 101, "StudentPersonalDetails": [ { ...
Read MorePerform aggregation sort in MongoDB?
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 MoreSelect MongoDB documents where a field either does not exist, is null, or is false?
To select MongoDB documents where a field either does not exist, is null, or is false, use the $in operator with an array containing false and null values. MongoDB treats non-existing fields as null when querying. Syntax db.collection.find({ "fieldName": { $in: [false, null] } }); Sample Data db.selectMongoDBDocumentsWithSomeCondition.insertMany([ {"StudentId": 1, "StudentName": "Larry"}, {"StudentId": 2, "StudentName": "Mike", "hasAgeGreaterThanOrEqualTo18": true}, {"StudentId": 3, "StudentName": "Carol", "hasAgeGreaterThanOrEqualTo18": false}, {"StudentId": 4, "StudentName": "Sam", "hasAgeGreaterThanOrEqualTo18": null}, ...
Read MoreMongoDB print JSON without whitespace i.e. unpretty JSON?
To print JSON without whitespace (unpretty JSON) in MongoDB, use the printjsononeline() function to compress documents into single-line format without pretty-print formatting. Syntax var cursor = db.collection.find().sort({_id:-1}).limit(10000); while(cursor.hasNext()) { printjsononeline(cursor.next()); } Sample Data db.unprettyJsonDemo.insertMany([ {"StudentName":"John", "StudentAge":21, "StudentTechnicalSkills":["C", "C++"]}, {"StudentName":"Carol", "StudentAge":22, "StudentTechnicalSkills":["MongoDB", "MySQL"]} ]); { "acknowledged" : true, "insertedIds" : [ ObjectId("5c900df25705caea966c557d"), ObjectId("5c900e085705caea966c557e") ...
Read MoreGetting the highest value of a column in MongoDB?
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 MoreIn MongoDB how do you use $set to update a nested value/embedded document?
In MongoDB, use the $set operator with dot notation to update values inside nested documents (embedded documents). This allows you to modify specific fields within nested objects without replacing the entire document. Syntax db.collectionName.update( { "matchCondition": "value" }, { $set: { "outerField.innerField": "newValue" } } ); Sample Data Let us create a collection with a nested document ? db.updateNestedValueDemo.insertOne({ "CustomerName": "Chris", "CustomerDetails": { "CustomerAge": 25, ...
Read MoreList all values of a certain field in MongoDB?
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 MoreDelete all elements in an array field in MongoDB?
To delete all elements in an array field in MongoDB, use the $set operator to replace the array field with an empty array []. This effectively clears all elements while preserving the field structure. Syntax db.collection.updateMany( {}, { $set: { "arrayFieldName": [] } } ); Sample Data db.deleteAllElementsInArrayDemo.insertMany([ { "InstructorName": "Larry", "InstructorTechnicalSubject": ["Java", "MongoDB"] }, { ...
Read MoreHow to find minimum value in MongoDB?
To find the minimum value in MongoDB, you can use sort() along with limit(1). This sorts documents in ascending order and returns only the first document containing the minimum value. Syntax db.yourCollectionName.find().sort({yourFieldName: 1}).limit(1); Create Sample Data Let us create a collection with student marks to demonstrate finding the minimum value ? db.findMinValueDemo.insertMany([ {"StudentMarks": 78}, {"StudentMarks": 69}, {"StudentMarks": 79}, {"StudentMarks": 59}, {"StudentMarks": 91} ]); { "acknowledged": ...
Read MoreHow do I display the indexes of a collection in MongoDB?
To display the indexes of a collection in MongoDB, use the getIndexes() method. This method returns information about all indexes defined on the specified collection, including the default _id index. Syntax db.collectionName.getIndexes(); Sample Data Let's create a collection with sample documents ? db.indexDemo.insertMany([ { "StudentName": "Larry", "StudentAge": 21 }, { "StudentName": "Mike", "StudentAge": 24 }, { "StudentName": "Carol", "StudentAge": 20 } ]); { "acknowledged": true, "insertedIds": [ ...
Read More