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 by Smita Kapse
Page 10 of 39
Calculate average of ratings in array and then include the field to original document in MongoDB?
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 MoreInsert MongoDB document field only when it's missing?
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 MoreSearch a sub-field on MongoDB?
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 MoreHow to get all collections where collection name like '%2015%'?
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 MoreGet the size of all the documents in a MongoDB query?
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 MoreHow to pull an array element (which is a document) in MongoDB?
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 MoreHow to create a collection correctly in MongoDB to avoid "ReferenceError: Not defined" error?
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 MoreHow to unset a variable in MongoDB shell?
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 MoreHow to rename a username in MongoDB?
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 MoreWhat is the MongoDB Capped Collection maximum allowable size?
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