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
Database Articles
Page 87 of 547
How to create a user in MongoDB v3?
To create a user in MongoDB v3, use the createUser() method. This allows you to create a user by specifying the username, password, and roles that assign specific permissions to databases. Syntax use admin db.createUser( { user: "yourUserName", pwd: "yourPassword", roles: [ { role: "yourPermission", db: "yourDatabase" } ] } ); Example Let us create a user named "Robert" with readWrite permissions ...
Read MoreSelect two fields and return a sorted array with their distinct values in MongoDB?
To select two fields and return a sorted array with their distinct values in MongoDB, use the aggregation framework with the $setUnion operator. This operator combines arrays and automatically removes duplicates while maintaining sorted order. Syntax db.collection.aggregate([ { $group: { "_id": null, "field1Array": { $push: "$field1" }, "field2Array": { $push: "$field2" } }}, { $project: { ...
Read MoreHow do I check whether a field contains null value in MongoDB?
To check whether a field contains a null value in MongoDB, use the $type operator with type number 10 (which represents null) or compare the field directly with null. Syntax // Method 1: Using $type operator db.collection.find({ fieldName: { $type: 10 } }); // Method 2: Direct null comparison db.collection.find({ fieldName: null }); Sample Data Let us create a collection with documents, including one with a null field ? db.nullDemo.insertMany([ { "FirstName": "Chris" }, { "FirstName": null }, { ...
Read MoreUse result from MongoDB in shell script?
To use MongoDB query results in shell scripts, you can store the result in a JavaScript variable using the var keyword and then manipulate or access the data as needed. Syntax var variableName = db.collection.findOne(query, projection); variableName.fieldName Sample Data Let us first create a collection with a document − db.useResultDemo.insertOne({"StudentFirstName":"Robert"}); { "acknowledged" : true, "insertedId" : ObjectId("5cda70f5b50a6c6dd317adcd") } Display all documents from a collection with the help of find() method − db.useResultDemo.find(); { "_id" : ObjectId("5cda70f5b50a6c6dd317adcd"), ...
Read MoreGet attribute list from MongoDB object?
To get attribute list from MongoDB object, use a for...in loop to iterate through the document properties and extract each key and value along with their data types. Syntax var document = db.collection.findOne(); for (key in document) { var value = document[key]; print(key + "(" + typeof(value) + "): " + value); } Create Sample Data db.getAttributeListDemo.insertOne({ "StudentId": 101, "StudentName": "John", "StudentAdmissionDate": new ISODate('2019-01-12'), "StudentSubjects": ["MongoDB", "Java", "MySQL"] }); ...
Read MoreCheck for Existing Document in MongoDB?
In MongoDB, you can check if a document exists in a collection using the findOne() method. This method returns the first document that matches the query criteria, or null if no document is found. Syntax db.collectionName.findOne({fieldName: "value"}); Sample Data Let us create a collection with sample documents − db.checkExistingDemo.insertMany([ {"StudentName": "John"}, {"StudentName": "Carol"}, {"StudentName": "Sam"}, {"StudentName": "Mike"} ]); { "acknowledged": true, "insertedIds": [ ...
Read MoreDoes Mongo shell treats numbers as float by default.? How can we work it around explicitly?
Yes, MongoDB shell treats numbers as double (float) by default. To work with integers or other specific number types, you need to explicitly specify the type using MongoDB's type constructors like NumberInt(), NumberLong(), or NumberDecimal(). Syntax NumberInt("value") // 32-bit integer NumberLong("value") // 64-bit integer NumberDecimal("value") // 128-bit decimal Example: Creating Integer Array Let's create an array with explicit integer values instead of floats ? var integerArrayDemo = [ NumberInt("50"), NumberInt("60"), ...
Read MoreConvert NumberLong to String in MongoDB?
In MongoDB, you can convert NumberLong values to strings using two approaches: the toString() method for explicit string conversion, or valueOf() method for numeric value extraction. Syntax // Method 1: Convert to String NumberLong('value').valueOf().toString() // Method 2: Convert to Number NumberLong('value').valueOf() Method 1: Using toString() for String Conversion The toString() method explicitly converts the NumberLong to a string representation ? NumberLong('1000').valueOf().toString(); 1000 Method 2: Using valueOf() for Number Conversion The valueOf() method returns the numeric value of NumberLong ? NumberLong('1000').valueOf(); ...
Read MoreGet output of MongoDB shell script?
You can use printjson() or print() to get output of MongoDB shell script. The printjson() method displays JavaScript objects in a formatted JSON structure, while print() outputs plain text. Syntax printjson(object); print(value); Create Sample Data Following is the query to create an array of objects ? var studentDetails = [ {"StudentName": "John", "StudentAge": 21}, {"StudentName": "Carol", "StudentAge": 24}, {"StudentName": "David", "StudentAge": 25} ]; Example 1: Using printjson() Following is the query to get the output of MongoDB ...
Read MoreMongoDB 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 More