Database Articles

Page 104 of 547

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

In MongoDB how do you use $set to update a nested value/embedded document?

Smita Kapse
Smita Kapse
Updated on 15-Mar-2026 1K+ Views

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 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

Delete all elements in an array field in MongoDB?

Smita Kapse
Smita Kapse
Updated on 15-Mar-2026 2K+ Views

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 More

How to find minimum value in MongoDB?

Smita Kapse
Smita Kapse
Updated on 15-Mar-2026 1K+ Views

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 More

How do I display the indexes of a collection in MongoDB?

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

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

How to list all users in the Mongo shell?

Anvi Jain
Anvi Jain
Updated on 15-Mar-2026 13K+ Views

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 More

Deleting all records of a collection in MongoDB Shell?

Smita Kapse
Smita Kapse
Updated on 15-Mar-2026 817 Views

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 More

Opposite of addToSet to 'removeFromSet' in MongoDB?

Anvi Jain
Anvi Jain
Updated on 15-Mar-2026 1K+ Views

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 More

Find documents with arrays not containing a document with a particular field value in MongoDB?

Smita Kapse
Smita Kapse
Updated on 15-Mar-2026 975 Views

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
Showing 1031–1040 of 5,468 articles
« Prev 1 102 103 104 105 106 547 Next »
Advertisements