Smita Kapse

Smita Kapse

388 Articles Published

Articles by Smita Kapse

Page 15 of 39

MongoDB aggregation framework match OR is possible?

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

Yes, MongoDB aggregation framework supports the $or operator within the $match stage to match documents that satisfy at least one of multiple conditions. This allows you to create flexible filtering logic in your aggregation pipelines. Syntax db.collection.aggregate([ { $match: { $or: [ { field1: value1 }, ...

Read More

How to query for records where field is null or not set in MongoDB?

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

In MongoDB, you can query for records where a field is null or not set using different operators. There are two distinct cases: when a field exists but is set to null, and when a field doesn't exist in the document at all. Syntax // Field exists and is null db.collection.find({"fieldName": null}); // Field does not exist db.collection.find({"fieldName": {$exists: false}}); // Field is null OR does not exist db.collection.find({$or: [{"fieldName": null}, {"fieldName": {$exists: false}}]}); Sample Data db.fieldIsNullOrNotSetDemo.insertMany([ {"EmployeeName": "Larry", "EmployeeAge": null, "EmployeeSalary": 18500}, ...

Read More

Does MongoDB getUsers() and SHOW command fulfil the same purpose?

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

Both getUsers() method and show users command can be used to list all users in MongoDB, but they serve the same fundamental purpose with slightly different output formats. Syntax Using getUsers() method: db.getUsers(); Using show command: show users; Method 1: Using getUsers() The getUsers() method returns user information as an array of documents ? db.getUsers(); [ { "_id" : "test.John", "user" : "John", ...

Read More

How to work Date query with ISODate in MongoDB?

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

Use date comparison operators like $gte, $lt, and $between along with ISODate() to work with date queries in MongoDB. ISODate provides a standardized way to store and query dates in UTC format. Syntax db.collection.find({ "dateField": { "$gte": ISODate("YYYY-MM-DDTHH:mm:ssZ"), "$lt": ISODate("YYYY-MM-DDTHH:mm:ssZ") } }); Sample Data Let us create a collection with sample documents containing date fields ? db.dateDemo.insertMany([ { ...

Read More

How to delete everything in a MongoDB database?

Smita Kapse
Smita Kapse
Updated on 15-Mar-2026 620 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

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

Smita Kapse
Smita Kapse
Updated on 15-Mar-2026 611 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

Which characters are NOT allowed in MongoDB field names?

Smita Kapse
Smita Kapse
Updated on 15-Mar-2026 867 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

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

Smita Kapse
Smita Kapse
Updated on 15-Mar-2026 300 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

How do I make case-insensitive queries on MongoDB?

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

To make case-insensitive queries in MongoDB, use regular expressions with the i flag or the $regex operator. This allows you to match documents regardless of letter case variations. Syntax // Method 1: Regular Expression with i flag db.collection.find({ "field": /pattern/i }); // Method 2: $regex operator db.collection.find({ "field": { $regex: "pattern", $options: "i" } }); Sample Data db.caseInsensitiveDemo.insertMany([ {"UserName": "David"}, {"UserName": "DAVID"}, {"UserName": "david"}, {"UserName": "Carol"}, {"UserName": "Mike"}, ...

Read More

Find all duplicate documents in a MongoDB collection by a key field?

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

To find all duplicate documents in a MongoDB collection by a key field, use the aggregation framework with $group and $match stages to group by the field and filter groups with count greater than 1. Syntax db.collection.aggregate([ { $group: { _id: { fieldName: "$fieldName" }, documents: { $addToSet: "$_id" }, count: { $sum: 1 } }}, { $match: { count: { ...

Read More
Showing 141–150 of 388 articles
« Prev 1 13 14 15 16 17 39 Next »
Advertisements