Anvi Jain

Anvi Jain

427 Articles Published

Articles by Anvi Jain

Page 13 of 43

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

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 largest document size in MongoDB?

Anvi Jain
Anvi Jain
Updated on 15-Mar-2026 916 Views

To find the largest document size in MongoDB, you need to iterate through all documents in a collection and use the Object.bsonsize() function to calculate each document's size in bytes. Syntax var largestSize = 0; db.collection.find().forEach(function(doc) { var currentSize = Object.bsonsize(doc); if (largestSize < currentSize) { largestSize = currentSize; } }); print("Largest Document Size = " + largestSize); Sample Data Let us create a collection with documents of varying sizes ? db.largestDocumentDemo.insertMany([ ...

Read More

The collection.find() always returns all fields with MongoDB?

Anvi Jain
Anvi Jain
Updated on 15-Mar-2026 625 Views

By default, collection.find() returns all fields from matching documents in MongoDB. However, you can use field projection to specify which fields to include or exclude in the result set. Syntax // Include specific fields (projection: 1) db.collection.find({}, {"fieldName": 1}); // Exclude specific fields (projection: 0) db.collection.find({}, {"fieldName": 0}); // Return all fields (default behavior) db.collection.find(); Sample Data db.returnFieldInFindDemo.insertMany([ { "StudentName": "John", "StudentAge": 23, ...

Read More

How to get element with max id in MongoDB?

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

To get the element with the maximum _id in MongoDB, use the sort() method with descending order and limit(1) to retrieve only the document with the highest _id value. Syntax db.collection.find().sort({_id: -1}).limit(1); Sample Data db.getElementWithMaxIdDemo.insertMany([ {"Name": "John", "Age": 21}, {"Name": "Larry", "Age": 24}, {"Name": "David", "Age": 23}, {"Name": "Chris", "Age": 20}, {"Name": "Robert", "Age": 25} ]); { "acknowledged": true, "insertedIds": [ ...

Read More

Check the current number of connections to MongoDB?

Anvi Jain
Anvi Jain
Updated on 15-Mar-2026 888 Views

To check the current number of connections to MongoDB, you can use the db.serverStatus().connections command. This returns detailed connection statistics including current, available, and total connections created. Syntax db.serverStatus().connections; Alternative syntax using a variable ? var connectionInfo = db.serverStatus(); connectionInfo.connections; Method 1: Direct Command The simplest way to check connection statistics ? db.serverStatus().connections; { "current" : 1, "available" : 999999, "totalCreated" : 1 } Method 2: Using a Variable Store the server status and access connection data ? var checkCurrentNumberOfConnections ...

Read More

Command to show the database currently being used in MongoDB?

Anvi Jain
Anvi Jain
Updated on 15-Mar-2026 660 Views

The command to show the database currently used in MongoDB is the following − Syntax db; This command returns the name of the currently active database in your MongoDB session. Check Available Databases Let us first check how many databases are present. The query is as follows − show dbs; The following is the output displaying all the databases − admin 0.000GB config 0.000GB local 0.000GB sample 0.000GB sampleDemo 0.000GB studentSearch 0.000GB test 0.003GB Example 1: Check Current Database Now, we have the list ...

Read More

How does MongoDB index arrays?

Anvi Jain
Anvi Jain
Updated on 15-Mar-2026 225 Views

MongoDB automatically indexes every value within an array field when you create an index on that field. This allows efficient querying of individual array elements without scanning the entire collection. Syntax db.collection.createIndex({"arrayField": 1}); db.collection.find({"arrayField": "specificValue"}); Sample Data Let us create a collection with a document containing an array field ? db.indexingForArrayElementDemo.insertOne({ "StudentFavouriteSubject": ["MongoDB", "MySQL"] }); { "acknowledged": true, "insertedId": ObjectId("5c8acdca6cea1f28b7aa0816") } Display the document to verify ? db.indexingForArrayElementDemo.find().pretty(); { ...

Read More

Performing regex Queries with PyMongo?

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

PyMongo is a Python distribution containing tools for working with MongoDB. To perform regex queries with PyMongo, you use the $regex operator to match documents where field values match a specified regular expression pattern. Syntax db.collection.find({ "fieldName": { "$regex": "pattern", "$options": "flags" } }) Sample Data Let us create a collection with sample documents ? db.performRegex.insertMany([ { ...

Read More

Return query based on date in MongoDB?

Anvi Jain
Anvi Jain
Updated on 15-Mar-2026 395 Views

To return query based on the date in MongoDB, use date range operators like $gte, $gt, $lte, and $lt with ISODate objects to filter documents by date fields. Syntax db.collection.find({ "dateField": { $gte: ISODate("YYYY-MM-DDTHH:mm:ssZ") } }); Sample Data Let us create a collection with passenger data containing arrival dates ? db.returnQueryFromDate.insertMany([ { "PassengerName": "John", "PassengerAge": 23, "PassengerArrivalTime": ISODate("2018-03-10T14:45:56Z") ...

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