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 on Trending Technologies
Technical articles with clear explanations and examples
Find items that do not have a certain field in MongoDB?
To find documents that do not have a certain field in MongoDB, use the $exists operator with the value false. This operator checks whether a field is present in the document, regardless of its value. Syntax db.collection.find({"fieldName": {$exists: false}}) Sample Data Let's create a collection with documents where some have a specific field and others don't ? db.findDocumentDoNotHaveCertainFields.insertMany([ {"UserId": 101, "UserName": "John", "UserAge": 21}, {"UserName": "David", "UserAge": 22, "UserFavouriteSubject": ["C", "Java"]}, {"UserName": "Bob", "UserAge": 24, "UserFavouriteSubject": ["MongoDB", "MySQL"]} ]); ...
Read MorePerforming regex Queries with PyMongo?
PyMongo is a Python distribution containing tools for working with MongoDB. To perform regex queries with PyMongo, you use the $regex operator to match documents where field values match a specified regular expression pattern. Syntax db.collection.find({ "fieldName": { "$regex": "pattern", "$options": "flags" } }) Sample Data Let us create a collection with sample documents ? db.performRegex.insertMany([ { ...
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 stop MongoDB in a single command?
To stop MongoDB in a single command, use the shutdown command with the --eval option to execute the shutdown operation directly from the command line without entering the MongoDB shell. Syntax mongo --eval "db.getSiblingDB('admin').shutdownServer()" Example: Stopping MongoDB Server Execute the shutdown command from your system's command prompt or terminal ? mongo --eval "db.getSiblingDB('admin').shutdownServer()" The output shows the MongoDB server shutting down ? MongoDB shell version v4.0.5 connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb Implicit session: session { "id" : UUID("c0337c02-7ee2-45d9-9349-b22d6b1ffe85") } MongoDB server version: 4.0.5 server should be down... 2019-03-14T21:56:10.327+0530 I NETWORK ...
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 MoreReturn 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 More