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
Getting 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 MoreHow to list all users in the Mongo shell?
To list all users in MongoDB shell, you can use the getUsers() method or the show users command. Both methods display user information including usernames, database assignments, roles, and authentication mechanisms. Syntax db.getUsers() Or using the show command: show users Method 1: Using getUsers() The getUsers() method returns all users as an array with complete user details ? db.getUsers(); The following is the output ? [ { "_id" : "test.John", "user" : "John", ...
Read MoreDeleting all records of a collection in MongoDB Shell?
To delete all records of a collection in MongoDB shell, use the remove() method with an empty filter document. This operation removes all documents from the collection but keeps the collection structure intact. Syntax db.collectionName.remove({}); Create Sample Data Let us create a collection with sample documents ? db.deleteAllRecordsDemo.insertMany([ {"StudentName": "John"}, {"StudentName": "Carol", "StudentAge": 21}, {"StudentName": "Mike", "StudentAge": 23, "Hobby": ["Learning", "Photography"]} ]); { "acknowledged": true, "insertedIds": [ ...
Read MoreOpposite of addToSet to 'removeFromSet' in MongoDB?
To get the opposite of $addToSet (which adds elements to arrays without duplicates), use the $pull operator. The $pull operator removes all instances of a value from an existing array, effectively working as a "removeFromSet" operation. Syntax db.collection.update( { matchCriteria }, { $pull: { "arrayField": "valueToRemove" } } ); Sample Data db.oppositeAddToSetDemo.insertMany([ { "StudentName": "John", "StudentHobby": ["Cricket", "Cooking", "Drawing"] }, ...
Read MoreFind documents with arrays not containing a document with a particular field value in MongoDB?
To find documents with arrays not containing a document with a particular field value in MongoDB, use the $nin operator with dot notation to specify the array field and value to exclude. Syntax db.collection.find({"arrayField.fieldName": {$nin: [value]}}) Sample Data Let us create a collection with student documents containing nested arrays ? db.documentWithAParticularFieldValueDemo.insertMany([ { "StudentId": 101, "StudentDetails": [ { ...
Read More