Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Articles by Smita Kapse
Page 15 of 39
MongoDB aggregation framework match OR is possible?
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 MoreHow to query for records where field is null or not set in MongoDB?
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 MoreDoes MongoDB getUsers() and SHOW command fulfil the same purpose?
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 MoreHow to work Date query with ISODate in MongoDB?
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 MoreHow to delete everything in a MongoDB database?
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 MoreHow to count number of distinct values per field/ key in MongoDB?
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 MoreWhich characters are NOT allowed in MongoDB field names?
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 MoreMatching an array field that contains any combination of the provided array in MongoDB?
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 MoreHow do I make case-insensitive queries on MongoDB?
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 MoreFind all duplicate documents in a MongoDB collection by a key field?
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