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
Articles on Trending Technologies
Technical articles with clear explanations and examples
Return query based on date in MongoDB?
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 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 MorePrettyprint in MongoDB shell as default?
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 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 MoreFind duplicate records in MongoDB?
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 MoreHow can I use 'Not Like' operator in MongoDB?
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 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 MoreDifference between count() and find().count() in MongoDB?
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 MoreQuerying an array of arrays in MongoDB?
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 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 More