MongoDB Articles

Page 84 of 111

How to remove key fields in MongoDB?

Nishtha Thakur
Nishtha Thakur
Updated on 15-Mar-2026 2K+ Views

To remove key fields in MongoDB, you can use the $unset operator. This operator deletes specified fields from documents in a collection. Syntax db.collection.updateMany( { query }, { $unset: { fieldName: 1 } } ); Sample Data Let us first create a collection with sample documents ? db.removeKeyFieldsDemo.insertMany([ { "StudentFirstName": "John", "StudentLastName": "Doe", "StudentAge": 23 ...

Read More

Retrieve the position in an Array in MongoDB?

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

To retrieve the position of an element in an array in MongoDB, you can use the map-reduce approach with JavaScript's indexOf() method. This technique allows you to find the index position of any array element. Syntax db.collection.mapReduce( function() { emit(this._id, { "IndexValue": this.arrayField.indexOf("searchValue") }); }, function() {}, { "out": { "inline": 1 }, "query": { "arrayField": "searchValue" ...

Read More

MongoDB multidimensional array projection?

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

For MongoDB multidimensional array projection, you need to use the aggregate framework. This allows you to access and project specific elements from nested arrays using operators like $unwind, $project, $skip, and $limit. Syntax db.collection.aggregate([ { $unwind: "$arrayField" }, { $project: { _id: 0, arrayField: 1 } }, { $unwind: "$arrayField" }, { $skip: index }, { $limit: 1 } ]); Sample Data db.multiDimensionalArrayProjection.insertOne({ "StudentFirstName": "Chris", ...

Read More

How to maintain the top count of array elements in MongoDB?

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

To maintain the top count of array elements in MongoDB, use the aggregation framework with $unwind to flatten arrays, $group to count frequencies, $sort to order by count, and $limit to get the top N elements. Syntax db.collection.aggregate([ { $unwind: "$arrayField" }, { $group: { _id: "$arrayField", count: { $sum: 1 } } }, { $sort: { count: -1 } }, { $limit: N } ]); Sample Data db.topCountArrayDemo.insertMany([ { "StudentId": 101, "StudentSubject": ...

Read More

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
Showing 831–840 of 1,106 articles
« Prev 1 82 83 84 85 86 111 Next »
Advertisements