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
MongoDB shutdown option is unavailable? How to get it?
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 MoreMongoDB query to match each element in a documents array to a condition?
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 MoreHow can I use a script to create users in MongoDB?
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 MoreHow to select only numeric strings in MongoDB?
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 MoreHow can I update and increment two fields in one command in MongoDB?
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 MoreHow to find all objects created before specified Date in MongoDB?
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 MoreHow can I save new Date() in MongoDB?
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 MoreCreate array with MongoDB query?
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 MoreCount distinct value in MongoDB?
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 MoreGet index of given element in array field in MongoDB?
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