Articles on Trending Technologies

Technical articles with clear explanations and examples

Is it possible to use variable for collection name using PyMongo?

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

Yes, it is possible to use variables for collection names in PyMongo. You can store the collection name as a string variable and access the collection dynamically using bracket notation or the get_collection() method. Syntax # Method 1: Using bracket notation collection_name = "your_collection_name" collection = db[collection_name] # Method 2: Using get_collection() method collection = db.get_collection(collection_name) MongoDB Shell Example First, let's see how to use variables for collection names in MongoDB shell ? var storeCollectionName = "new_Collection"; db[storeCollectionName].insertMany([ {"UserName": "John", "UserAge": 21}, ...

Read More

Get database data size in MongoDB?

Nishtha Thakur
Nishtha Thakur
Updated on 15-Mar-2026 1K+ Views

To get the database data size in MongoDB, you can use the db.stats() method. This method returns detailed statistics about the current database including data size, storage size, and other useful metrics. Syntax db.stats() Example 1: Check Current Database Statistics First, check which database you are currently using ? db test Now get the database statistics for the 'test' database ? db.stats() { "db" : "test", "collections" : 114, "views" ...

Read More

Find a document with ObjectID in MongoDB?

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

To find a document with ObjectId in MongoDB, use the ObjectId() constructor in your query to match the specific _id field value. Syntax db.collectionName.find({"_id": ObjectId("yourObjectIdValue")}).pretty(); Sample Data Let us create a collection with sample documents to demonstrate finding by ObjectId ? db.findDocumentWithObjectIdDemo.insertMany([ {"UserName": "Larry"}, {"UserName": "Mike", "UserAge": 23}, {"UserName": "Carol", "UserAge": 26, "UserHobby": ["Learning", "Photography"]} ]); { "acknowledged": true, "insertedIds": [ ...

Read More

Avoid duplicate entries in MongoDB?

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

To avoid duplicate entries in MongoDB, create a unique index on the field that should have unique values. This prevents insertion of documents with duplicate values and throws an error if attempted. Syntax db.collectionName.createIndex({"fieldName": 1}, {unique: true}); Example: Create Unique Index Let's create a unique index on the UserName field to prevent duplicate usernames ? db.avoidDuplicateEntriesDemo.createIndex({"UserName": 1}, {unique: true}); { "createdCollectionAutomatically": true, "numIndexesBefore": 1, "numIndexesAfter": 2, "ok": 1 } Test with ...

Read More

Querying array elements with MongoDB?

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

MongoDB provides simple and efficient ways to query documents that contain specific values within array fields. You can search for documents where an array contains a particular element using standard find() operations. Syntax db.collection.find({arrayFieldName: "value"}); This syntax returns all documents where the specified array field contains the given value. Sample Data Let us create a collection with sample documents to demonstrate array querying ? db.queryArrayElementsDemo.insertMany([ { "StudentName": "John", "StudentFavouriteSubject": ["C", "Java"] ...

Read More

How to know which storage engine is used in MongoDB?

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

To know which storage engine is used in MongoDB, you can use the db.serverStatus().storageEngine command. This returns basic information about the current storage engine including its name and capabilities. Syntax db.serverStatus().storageEngine; For detailed configuration statistics of the storage engine ? db.serverStatus().storageEngineName; Example 1: Check Storage Engine db.serverStatus().storageEngine; { "name" : "wiredTiger", "supportsCommittedReads" : true, "supportsSnapshotReadConcern" : true, "readOnly" : false, "persistent" : true } This output shows that WiredTiger ...

Read More

How to get distinct list of sub-document field values in MongoDB?

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

To get distinct list of sub-document field values in MongoDB, use the distinct() method with dot notation to access nested fields within arrays or embedded documents. Syntax db.collection.distinct("outerField.innerField"); Sample Data Let us create a collection with student documents containing nested arrays ? db.getDistinctListOfSubDocumentFieldDemo.insertMany([ { "StudentId": 101, "StudentPersonalDetails": [ { ...

Read More

Perform aggregation sort in MongoDB?

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

To perform aggregation sort in MongoDB, use the aggregate() method with the $sort stage. The $sort stage orders documents by specified fields in ascending (1) or descending (-1) order. Syntax db.collection.aggregate([ { $group: { ... } }, { $sort: { field: 1 } } // 1 = ascending, -1 = descending ]); Sample Data db.aggregationSortDemo.insertMany([ {"StudentId": 98, "StudentFirstName": "John", "StudentLastName": "Smith"}, {"StudentId": 128, "StudentFirstName": "Carol", "StudentLastName": "Taylor"}, {"StudentId": 110, "StudentFirstName": ...

Read More

Select MongoDB documents where a field either does not exist, is null, or is false?

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

To select MongoDB documents where a field either does not exist, is null, or is false, use the $in operator with an array containing false and null values. MongoDB treats non-existing fields as null when querying. Syntax db.collection.find({ "fieldName": { $in: [false, null] } }); Sample Data db.selectMongoDBDocumentsWithSomeCondition.insertMany([ {"StudentId": 1, "StudentName": "Larry"}, {"StudentId": 2, "StudentName": "Mike", "hasAgeGreaterThanOrEqualTo18": true}, {"StudentId": 3, "StudentName": "Carol", "hasAgeGreaterThanOrEqualTo18": false}, {"StudentId": 4, "StudentName": "Sam", "hasAgeGreaterThanOrEqualTo18": null}, ...

Read More

MongoDB print JSON without whitespace i.e. unpretty JSON?

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

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
Showing 23761–23770 of 61,297 articles
Advertisements