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
Big Data Analytics Articles
Page 50 of 135
How to update % printed to Console from MongoDB?
To update and print percentage values to console from MongoDB script, create a variable with your numeric value and use the print() method with toFixed() to format decimal places. Syntax var variableName = numericValue; print(variableName.toFixed(decimalPlaces) + " %"); Example Create a variable with a percentage value and format it to 2 decimal places ? var amount = 10.58945; print(amount.toFixed(2) + " %"); The output of the above code is ? 10.59 % Key Points Use toFixed() to control the number of decimal places displayed. ...
Read MoreInvoke convertToCapped and convert an existing collection to capped in MongoDB
To convert an existing collection to capped in MongoDB, use the convertToCapped command with runCommand(). A capped collection has a fixed size and automatically removes old documents when the size limit is reached. Syntax db.runCommand({ convertToCapped: "collectionName", size: sizeInBytes }); Sample Data Let us create a regular collection with some documents ? db.demo260.insertMany([ {"Name": "Chris"}, {"Name": "Bob"}, {"Name": "David"} ]); { "acknowledged": true, ...
Read MoreMongoDB inverse of query to return all items except specific documents?
To get documents except some specific documents, use $nor along with $and. The $nor operator returns documents that do not match any of the specified conditions, making it perfect for inverse queries. Syntax db.collection.find({ $nor: [ { $and: [{ field1: value1 }, { field2: value2 }] } ] }); Sample Data Let us first create a collection with sample student documents — db.demo1.insertMany([ { "StudentName": "Chris", "StudentMarks": 38 }, ...
Read MoreHow to specify the order in which the query returns matching documents in MongoDB
To specify the order in which the query returns matching documents, use cursor.sort() in MongoDB. The cursor is obtained from db.collectionName.find(). Syntax db.collection.find().sort({ field: 1 }); // Ascending order db.collection.find().sort({ field: -1 }); // Descending order Where 1 means ascending order and -1 means descending order. Sample Data Let us create a collection with documents − db.demo259.insertMany([ { "Subject": "MySQL" }, { "Subject": "Java" }, { "Subject": "MongoDB" } ]); { ...
Read MoreHow to pull value from array of ObjectIDs in MongoDB?
To pull value from array of ObjectIDs in MongoDB, use the $pull operator with the $in operator to match and remove specific ObjectID values from the array. Syntax db.collection.update( { /* query */ }, { $pull: { arrayField: { $in: [ObjectId("id1"), ObjectId("id2")] } } } ); Sample Data Let us create a collection with documents ? db.demo258.insertOne({ "arrayOfObjectsId": [ ObjectId("5e47a5e81627c0c63e7dba92"), ObjectId("5e47a5e51627c0c63e7dba91") ...
Read MoreSet MongoDB compound index with a fixed value field
A MongoDB compound index with a fixed value field ensures uniqueness across multiple fields, including fields that may be missing (null) in some documents. When you create a unique compound index, MongoDB treats missing fields as null values. Syntax db.collection.createIndex( { "field1": 1, "field2": 1 }, { unique: true } ); Create Sample Data First, let's create a unique compound index on StudentName and StudentAge ? db.compoundIndexDemo.createIndex( {"StudentName": 1, "StudentAge": 1}, {unique: true} ); ...
Read MoreUpdate MongoDB variable value with variable itself?
To update a MongoDB field value using its current value as part of the new value, use the $set operator with string concatenation or the $concat aggregation operator within an update pipeline. Syntax // Using aggregation pipeline (recommended) db.collection.updateMany( {}, [{ $set: { fieldName: { $concat: ["$fieldName", " additional text"] } } }] ); // Using $set with literal string (replaces with literal text) db.collection.update( {}, { $set: { fieldName: "fieldName additional text" } } ); Sample Data ...
Read MoreMongoDB bulk insert for documents
MongoDB provides multiple methods for bulk inserting documents efficiently. The most common approaches are using initializeUnorderedBulkOp() for legacy bulk operations and insertMany() for modern bulk inserts. Syntax // Legacy Bulk Operation var bulkOp = db.collection.initializeUnorderedBulkOp(); bulkOp.insert(document1); bulkOp.insert(document2); bulkOp.execute(); // Modern Approach db.collection.insertMany([document1, document2, document3]); Method 1: Using initializeUnorderedBulkOp() Create a bulk operation and insert multiple documents ? var manyDocument = db.demo255.initializeUnorderedBulkOp(); manyDocument.insert({ "Name": "Chris", "Age": 24 }); manyDocument.insert({ "Name": "Bob", "Age": 22 }); manyDocument.insert({ "Name": "David", "Age": 23 }); manyDocument.execute(); BulkWriteResult({ "writeErrors" : [ ], ...
Read MoreSplit a string during MongoDB aggregate
To split a string during MongoDB aggregation, use the $split operator which divides a string into an array based on a delimiter. For more complex operations, you can combine it with other aggregation operators like $project and $arrayElemAt. Syntax { $split: ["$fieldName", "delimiter"] } Sample Data db.splitString.insertOne({ "StudentName": "John Smith" }); { "acknowledged": true, "insertedId": ObjectId("5e0849d925ddae1f53b62206") } Method 1: Using $split in Aggregation Pipeline Split the StudentName field and extract the last name using the ...
Read MoreMongoDB query to change simple field into an object?
To change a simple field into an object in MongoDB, use the $rename operator with a temporary field approach. This involves renaming the field to a temporary name, then renaming it back with the desired object structure using dot notation. Syntax db.collection.updateMany( {}, { $rename: { "fieldName": "tempField" } } ); db.collection.updateMany( {}, { $rename: { "tempField": "objectName.nestedField" } } ); Sample Data db.changeSimpleFieldDemo.insertOne({ "StudentMarks": 58, "StudentSubject": "MySQL" ...
Read More