If the MongoDB shutdown option is unavailable, you need to switch to the admin database first. The shutdownServer() method requires administrative privileges and can only be executed from the admin database context. Syntax use admin db.shutdownServer() Step 1: Switch to Admin Database First, connect to the admin database ? use admin switched to db admin Step 2: Execute Shutdown Command Now run the shutdown command ? db.shutdownServer() server should be down... 2019-04-22T19:11:40.949+0530 I NETWORK [js] trying reconnect to 127.0.0.1:27017 failed 2019-04-22T19:11:42.197+0530 ... Read More
To match each element in a document's array to a condition in MongoDB, use the $where operator with JavaScript's every() method. This approach allows you to verify that all array elements satisfy a specific condition. Syntax db.collection.find({ $where: "return this.arrayField.every(function(element) { return (condition); })" }); Sample Data db.arrayConditionDemo.insertMany([ {"Name": "John", "Marks": [40, 43, 45]}, {"Name": "Mike", "Marks": [45]}, {"Name": "Chris", "Marks": [43, 45, ... Read More
You can create users in MongoDB using the createUser() method. This method allows you to define username, password, and specific roles for database access control. Syntax db.createUser( { user: "yourUserName", pwd: "yourPassword", roles: [ { role: "roleType", db: "databaseName" } ] } ); Example Let us create a user named "David" with read access to the "test" database ? db.createUser( ... Read More
To select only numeric strings in MongoDB, use regular expressions with the pattern /^\d+$/ which matches strings containing only digits from start to end. Syntax db.collection.find({ "field": /^\d+$/ }); Sample Data db.selectOnlyNumericDemo.insertMany([ { "UserId": "User101" }, { "UserId": "102" }, { "UserId": "User103" }, { "UserId": "104" } ]); { "acknowledged": true, "insertedIds": [ ObjectId("5cbdb711de8cc557214c0e16"), ... Read More
To update and increment two fields in one command in MongoDB, use the $inc operator with multiple field-value pairs in a single update operation. This allows you to increment multiple numeric fields atomically in one database call. Syntax db.collection.update( { query }, { $inc: { field1: value1, field2: value2 } } ); Create Sample Data Let us first create a collection with documents ? db.incrementDemo.insertOne({ "Value1": 10, "Value2": 20 }); { ... Read More
To find all objects created before a specified date in MongoDB, use the $lt (less than) operator with ISODate() to compare date values in your query condition. Syntax db.collection.find({ "dateField": { $lt: ISODate("YYYY-MM-DD") } }); Sample Data db.beforeSpecifyDateDemo.insertMany([ { "UserLoginDate": new ISODate('2016-03-21') }, { "UserLoginDate": new ISODate('2016-05-11') }, { "UserLoginDate": new ISODate('2017-01-31') }, { "UserLoginDate": new ISODate('2018-05-15') }, { "UserLoginDate": new ISODate('2019-04-01') } ]); { ... Read More
To save new Date() in MongoDB, use new ISODate() or new Date() for date and datetime values. MongoDB automatically stores dates in BSON Date format, which is represented as ISODate in the shell. Syntax // Using ISODate db.collection.insertOne({ "field": new ISODate("YYYY-MM-DD HH:MM:SS") }); // Using Date db.collection.insertOne({ "field": new Date("YYYY-MM-DD HH:MM:SS") }); Create Sample Data Insert documents with date fields using new ISODate() ? db.saveDateDemo.insertMany([ { "UserName": "John", ... Read More
You can use the toArray() method with MongoDB queries to convert query results into an array format. This method transforms the cursor returned by find() into a JavaScript array containing all matching documents. Syntax db.collectionName.find({}, {fieldName: 1}).toArray(); Sample Data Let us create a collection with sample documents ? db.createArrayDemo.insertMany([ {"UserName": "Chris"}, {"UserName": "David"}, {"UserName": "Robert"}, {"UserName": "Sam"} ]); { "acknowledged": true, "insertedIds": [ ... Read More
To count distinct values in MongoDB, use the distinct() method combined with the .length property. This returns the number of unique values for a specified field across all documents in a collection. Syntax db.collectionName.distinct("fieldName").length; Create Sample Data Let us create a collection with documents that contain duplicate values ? db.countDistinctDemo.insertMany([ {"StudentName": "John"}, {"StudentName": "Chris"}, {"StudentName": "Chris"}, {"StudentName": "Carol"}, {"StudentName": "David"}, {"StudentName": "Carol"} ]); { ... Read More
To get the index of a given element in an array field in MongoDB, use the $indexOfArray operator within an aggregation pipeline. This operator returns the zero-based index of the first occurrence of the specified element in the array. Syntax db.collection.aggregate([ { $project: { "fieldName": { $indexOfArray: [ "$arrayField", "searchElement" ] ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Economics & Finance