Database Articles

Page 87 of 547

How to create a user in MongoDB v3?

Smita Kapse
Smita Kapse
Updated on 15-Mar-2026 252 Views

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 More

Select two fields and return a sorted array with their distinct values in MongoDB?

Anvi Jain
Anvi Jain
Updated on 15-Mar-2026 464 Views

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 More

How do I check whether a field contains null value in MongoDB?

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

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 More

Use result from MongoDB in shell script?

Smita Kapse
Smita Kapse
Updated on 15-Mar-2026 599 Views

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 More

Get attribute list from MongoDB object?

Samual Sam
Samual Sam
Updated on 15-Mar-2026 1K+ Views

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 More

Check for Existing Document in MongoDB?

karthikeya Boyini
karthikeya Boyini
Updated on 15-Mar-2026 187 Views

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 More

Does Mongo shell treats numbers as float by default.? How can we work it around explicitly?

Samual Sam
Samual Sam
Updated on 15-Mar-2026 305 Views

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 More

Convert NumberLong to String in MongoDB?

karthikeya Boyini
karthikeya Boyini
Updated on 15-Mar-2026 2K+ Views

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 More

Get output of MongoDB shell script?

Samual Sam
Samual Sam
Updated on 15-Mar-2026 888 Views

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 More

MongoDB shutdown option is unavailable? How to get it?

karthikeya Boyini
karthikeya Boyini
Updated on 15-Mar-2026 122 Views

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
Showing 861–870 of 5,468 articles
« Prev 1 85 86 87 88 89 547 Next »
Advertisements