Database Articles

Page 107 of 547

Find items that do not have a certain field in MongoDB?

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

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 More

Performing regex Queries with PyMongo?

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

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 More

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

Smita Kapse
Smita Kapse
Updated on 15-Mar-2026 149 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 stop MongoDB in a single command?

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

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 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

Return query based on date in MongoDB?

Anvi Jain
Anvi Jain
Updated on 15-Mar-2026 396 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 621 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 294 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 615 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
Showing 1061–1070 of 5,468 articles
« Prev 1 105 106 107 108 109 547 Next »
Advertisements