Articles on Trending Technologies

Technical articles with clear explanations and examples

Return query based on date in MongoDB?

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

How to delete everything in a MongoDB database?

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

You can delete everything in a MongoDB database using the dropDatabase() method. This operation permanently removes the entire database and all its collections. Syntax use databaseName; db.dropDatabase(); Example: Delete Entire Database Let's first display all existing databases ? show dbs admin 0.000GB config 0.000GB flightInformation 0.000GB local 0.000GB sample 0.000GB sampleDemo ...

Read More

Prettyprint in MongoDB shell as default?

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

You can call pretty() function on cursor object to format MongoDB query output in a readable, indented JSON format. This is especially useful for documents with nested arrays and objects. Syntax db.collectionName.find().pretty(); Sample Data Let us create a collection with sample documents ? db.prettyDemo.insertMany([ { "ClientName": "Larry", "ClientAge": 27, "ClientFavoriteCountry": ["US", "UK"] }, { ...

Read More

How to count number of distinct values per field/ key in MongoDB?

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

To count the number of distinct values per field in MongoDB, use the distinct() method combined with the .length property. The distinct() method returns an array of unique values from a specified field across all documents. Syntax db.collection.distinct("fieldName").length Sample Data Let's create a collection with student documents containing favorite subjects ? db.distinctCountValuesDemo.insertMany([ { "StudentFirstName": "John", "StudentFavouriteSubject": ["C", "C++", "Java", "MySQL", "C", "C++"] }, { ...

Read More

Find duplicate records in MongoDB?

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

To find duplicate records in MongoDB, use the aggregate framework with $group, $match, and $project stages. The aggregation pipeline groups documents by field values, counts occurrences, and filters results where count is greater than 1. Syntax db.collection.aggregate([ { $group: { "_id": "$fieldName", "count": { $sum: 1 } } }, { $match: { "_id": { $ne: null }, "count": { $gt: 1 } } }, { $project: { "fieldName": "$_id", "_id": 0 } } ]); Sample Data db.findDuplicateRecordsDemo.insertMany([ ...

Read More

How can I use 'Not Like' operator in MongoDB?

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

In MongoDB, there is no direct 'NOT LIKE' operator as found in SQL. Instead, use the $not operator combined with regular expressions to achieve similar functionality for pattern matching exclusion. Syntax db.collection.find({ fieldName: { $not: /pattern/ } }); Sample Data db.notLikeOperatorDemo.insertMany([ { "StudentName": "John Doe" }, { "StudentName": "John Smith" }, { "StudentName": "John Taylor" }, { "StudentName": "Carol Taylor" }, { "StudentName": "David Miller" } ]); ...

Read More

Which characters are NOT allowed in MongoDB field names?

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

MongoDB field names have specific restrictions. You cannot use the $ symbol at the beginning of field names or the period (.) character anywhere in field names, as these are reserved for MongoDB's internal operations and dot notation. Syntax // Invalid field names { "$fieldName": "value" } // Cannot start with $ { "field.name": "value" } // Cannot contain dots { "field$name": "value" } // $ allowed in middle/end // Valid field names { "fieldName": "value" } { "field_name": "value" } { ...

Read More

Difference between count() and find().count() in MongoDB?

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

In MongoDB, count() and find().count() both return the number of documents in a collection, but they have subtle differences in how they handle query conditions and performance characteristics. Syntax db.collection.count(query, options) db.collection.find(query).count() Sample Data db.countDemo.insertMany([ {"UserId": 1, "UserName": "John"}, {"UserId": 2, "UserName": "Carol"}, {"UserId": 3, "UserName": "Bob"}, {"UserId": 4, "UserName": "Mike"} ]); { "acknowledged": true, "insertedIds": [ ObjectId("5c7f9d278d10a061296a3c5d"), ...

Read More

Querying an array of arrays in MongoDB?

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

Use the $elemMatch operator with nested $in to query an array of arrays in MongoDB. This approach allows you to search for specific values within nested array structures. Syntax db.collection.find({ "arrayField": { $elemMatch: { $elemMatch: { $in: ["searchValue"] } ...

Read More

Matching an array field that contains any combination of the provided array in MongoDB?

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

To match documents where an array field contains any combination of the provided array values in MongoDB, use the $not operator combined with $elemMatch and $nin. This approach finds documents where the array contains only elements from the specified set. Syntax db.collection.find({ arrayField: { $not: { $elemMatch: { $nin: ["value1", "value2", "value3"] } } } }); Sample Data ...

Read More
Showing 1–10 of 61,284 articles
« Prev 1 2 3 4 5 6129 Next »
Advertisements