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 More
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, 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
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
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
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
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
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
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
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
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Economics & Finance