MongoDB Articles

Page 102 of 111

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

Find largest document size in MongoDB?

Anvi Jain
Anvi Jain
Updated on 15-Mar-2026 908 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

How to find through list of ids in MongoDB?

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

You can use the $in operator to find documents through a list of specific ids in MongoDB. The $in operator allows you to query multiple values in a single field, making it perfect for searching by multiple ObjectIds. Syntax db.collection.find({ _id: { $in: [ObjectId("id1"), ObjectId("id2"), ObjectId("id3")] } }); Sample Data Let's create a collection with sample student documents ? db.findListOfIdsDemo.insertMany([ {"StudentName": "Carol", "StudentAge": 21}, {"StudentName": "Bob", "StudentAge": 25}, {"StudentName": "David", "StudentAge": 22}, ...

Read More

MongoDB query with an 'or' condition?

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

The $or operator in MongoDB performs a logical OR operation on an array of expressions and returns documents that match at least one of the conditions. It's useful when you need to query documents based on multiple alternative criteria. Syntax db.collection.find({ $or: [ { field1: value1 }, { field2: value2 }, { field3: { $operator: value3 } } ] }); Sample Data ...

Read More

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

Anvi Jain
Anvi Jain
Updated on 15-Mar-2026 622 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

MongoDB 'count()' is very slow. How do we work around with it?

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

MongoDB's count() method can be slow on large collections because it performs a collection scan. To improve performance, create indexes on fields used in count queries and consider alternative methods like countDocuments() or estimatedDocumentCount(). Syntax // Create index to speed up count operations db.collection.createIndex({ "fieldName": 1 }); // Count with index support db.collection.countDocuments({ "fieldName": "value" }); // Fast estimated count (no query filter) db.collection.estimatedDocumentCount(); Sample Data db.countPerformanceDemo.insertMany([ { "StudentName": "John", "StudentCountryName": "US" }, { "StudentName": "Mike", "StudentCountryName": "UK" }, ...

Read More

Resolve 'multi update only works with $ operators' in MongoDb?

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

The "multi update only works with $ operators" error occurs when trying to update multiple documents without using MongoDB's update operators like $set. To resolve this, use update operators when the multi flag is set to true. Syntax db.collection.update( { /* query */ }, { $set: { "fieldName": "newValue" } }, { multi: true } ) Sample Data db.unconditionalUpdatesDemo.insertMany([ { "ClientName": "Larry", "ClientAge": 24 }, { "ClientName": "Mike", "ClientAge": 26 }, ...

Read More
Showing 1011–1020 of 1,106 articles
« Prev 1 100 101 102 103 104 111 Next »
Advertisements