Smita Kapse

Smita Kapse

388 Articles Published

Articles by Smita Kapse

Page 10 of 39

Calculate average of ratings in array and then include the field to original document in MongoDB?

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

To calculate the average of ratings in an array and include this field in the original document in MongoDB, use the $addFields stage with $avg operator in an aggregation pipeline. This adds a computed field without modifying the original document structure. Syntax db.collection.aggregate([ { $addFields: { averageField: { $avg: "$arrayField.numericField" } } } ]); Sample Data db.averageOfRatingsInArrayDemo.insertOne({ ...

Read More

Insert MongoDB document field only when it's missing?

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

To insert a MongoDB document field only when it's missing, use the $exists operator in the query condition to target documents where the field doesn't exist, then apply $set to add the field only to those documents. Syntax db.collection.update( { "fieldName": { "$exists": false } }, { "$set": { "fieldName": "value" } }, { "multi": true } ); Sample Data db.missingDocumentDemo.insertMany([ {"StudentFirstName": "Adam", "StudentLastName": "Smith"}, {"StudentFirstName": "Carol", "StudentLastName": "Taylor"}, ...

Read More

Search a sub-field on MongoDB?

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

To search a sub-field in MongoDB, use dot notation with the field path enclosed in double quotes. This allows you to query nested fields within embedded documents. Syntax db.collection.find({"parentField.subField": "value"}); Sample Data db.searchSubFieldDemo.insertMany([ { "UserDetails": { "UserEmailId": "John123@gmail.com", "UserAge": 21 } }, ...

Read More

How to get all collections where collection name like '%2015%'?

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

To get all collections where the collection name contains a specific pattern like '%2015%', you can use MongoDB's getCollectionNames() method combined with JavaScript's filter() function and regular expressions. Creating Sample Collections Let us first create some collections that contain year numbers like 2015, 2019, etc ? > use web; switched to db web > db.createCollection("2015-myCollection"); { "ok" : 1 } > db.createCollection("2019-employeeCollection"); { "ok" : 1 } > db.createCollection("2015-yourCollection"); { "ok" : 1 } Now you can display all the collections with the help of SHOW command ? > show collections; ...

Read More

Get the size of all the documents in a MongoDB query?

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

To get the size of all the documents in a MongoDB query, you need to loop through the documents and calculate their cumulative BSON size using Object.bsonsize() method. Syntax var cursor = db.collection.find(); var totalSize = 0; cursor.forEach(function(doc) { totalSize += Object.bsonsize(doc); }); print(totalSize); Sample Data db.sizeOfAllDocumentsDemo.insertMany([ { "StudentFirstName": "John", "StudentSubject": ["MongoDB", "Java"] }, { ...

Read More

How to pull an array element (which is a document) in MongoDB?

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

To pull (remove) an array element that is a document in MongoDB, use the $pull operator with specific field conditions to match and remove the target document from the array. Syntax db.collection.update( { query }, { $pull: { "arrayField": { "field": "value" } } } ); Sample Data db.pullAnArrayElementDemo.insertOne({ "StudentDetails": [ { "StudentFirstName": "Chris", ...

Read More

How to create a collection correctly in MongoDB to avoid "ReferenceError: Not defined" error?

Smita Kapse
Smita Kapse
Updated on 15-Mar-2026 2K+ Views

To create a collection correctly in MongoDB and avoid "ReferenceError: Not defined" errors, you must use the proper MongoDB database object syntax with createCollection() method. Syntax db.createCollection("collectionName"); Example: Creating a Collection Let us create a collection called "employeeInformation" in the "sample" database ? use sample; db.createCollection("employeeInformation"); switched to db sample { "ok" : 1 } Verify Collection Creation Display all collections in the current database to confirm the collection was created ? db.getCollectionNames(); [ "arraySizeErrorDemo", "atleastOneMatchDemo", ...

Read More

How to unset a variable in MongoDB shell?

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

Use the delete operator to unset (remove) a variable from the MongoDB shell environment. This permanently removes the variable from memory. Syntax delete variableName; Example 1: Check Undefined Variable First, let's check if a variable exists before defining it ? customerDetail; 2019-05-08T22:29:17.361+0530 E QUERY [js] ReferenceError: customerDetail is not defined : @(shell):1:1 Example 2: Define and Use Variable Now let's create the variable with a value ? customerDetail = {"CustomerFirstName": "Chris"}; { "CustomerFirstName" : "Chris" } Display the variable ...

Read More

How to rename a username in MongoDB?

Smita Kapse
Smita Kapse
Updated on 15-Mar-2026 2K+ Views

To rename a username in MongoDB, you need to update the user document in the system.users collection using the $set operator. This operation modifies the user field while preserving all other user properties like roles and authentication mechanisms. Syntax db.system.users.update( {"user": "oldUserName"}, {$set: {"user": "newUserName"}} ); Display Current Users First, let's view all existing users in the database ? use admin; db.getUsers(); [ { "_id": "admin.Chris", ...

Read More

What is the MongoDB Capped Collection maximum allowable size?

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

MongoDB capped collections can be set to any size you specify using the size parameter. There is no fixed maximum limit − the size is constrained only by your available disk space and system resources. Syntax db.createCollection('collectionName', { capped: true, size: sizeInBytes }); Example Create a capped collection with a large size limit ? db.createCollection('cappedCollectionMaximumSize', { capped: true, size: 1948475757574646 }); { "ok" : 1 } Verify Collection Properties Check ...

Read More
Showing 91–100 of 388 articles
« Prev 1 8 9 10 11 12 39 Next »
Advertisements