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 George John
Page 27 of 79
Get the size of a collection in MongoDB using conditions?
To get the size of a MongoDB collection using specific conditions, you can filter documents with a query and then calculate the total size of matching documents using Object.bsonSize() within a forEach() loop. Syntax var collectionSize = 0; db.collection.find({condition}).forEach(function(document) { var size = Object.bsonSize(document); collectionSize = collectionSize + size; }); print(collectionSize); Sample Data Let us create a collection with sample documents ? db.mongoDBCollectionSizeDemo.insertMany([ {"Name": "John", "Age": 23}, {"Name": "Chris", "Age": 24}, {"Name": ...
Read MoreCompare multiple properties in MongoDB?
To compare multiple properties in MongoDB, use the $where operator which allows JavaScript expressions to evaluate conditions between different fields or array elements within the same document. Syntax db.collection.find({ $where: "this.field1 > this.field2" }); Sample Data Let us create a collection with sample documents − db.comparingMultiplePropertiesDemo.insertOne({ "Values": [10, 70, 60] }); { "acknowledged": true, "insertedId": ObjectId("5cf228fcb64a577be5a2bc0a") } Display the document to verify insertion − db.comparingMultiplePropertiesDemo.find().pretty(); { ...
Read MoreUpdate key value where another key equals some value in MongoDB?
To update a key value where another key equals a specific value in MongoDB arrays, use $elemMatch to match the array element and $set with the positional operator $ to update the matched element. Syntax db.collection.update( { "arrayField": { "$elemMatch": { "matchKey": "matchValue" } } }, { "$set": { "arrayField.$.updateKey": "newValue" } } ); Sample Data Let us first create a collection with documents − db.keyValueDemo.insertOne({ "_id": new ObjectId(), "CustomerDetails": [ ...
Read MoreDisplay the last two values from field with MongoDB
To display the last two values from a field in MongoDB, use the $slice operator with a negative value in the projection. The negative value indicates retrieving elements from the end of the array. Syntax db.collection.find({}, { "fieldName": { "$slice": -N } }); Sample Data Let us first create a collection with documents ? db.numberOfValuesDemo.insertOne({ "Values": [100, 200, 300, 900, 1000, 98] }); { "acknowledged": true, "insertedId": ObjectId("5cefb736ef71edecf6a1f6ab") } Display all documents from a collection ...
Read MoreRemoving an array element from MongoDB collection using update() and $pull
To remove a specific element from an array in MongoDB, use the $pull operator with the update() method. This operator removes all instances of a value that match the specified condition from an array field. Syntax db.collection.update( { query }, { $pull: { "arrayField": "valueToRemove" } } ); Create Sample Data Let us first create a collection with documents ? db.removingAnArrayElementDemo.insertOne({ "UserMessage": ["Hi", "Hello", "Bye"] }); { "acknowledged": true, ...
Read MoreGet number of updated documents in MongoDB?
To get the number of updated documents in MongoDB, you can use the WriteResult returned by update operations or the modern modifiedCount property in newer MongoDB versions. Syntax // Legacy update() method db.collection.update(query, update, options); WriteResult({ "nMatched": X, "nModified": Y }) // Modern updateMany() method db.collection.updateMany(query, update); { "modifiedCount": Y } Sample Data db.getNumberOfUpdatedDocumentsDemo.insertMany([ {"StudentName": "David"}, {"StudentName": "Chris"}, {"StudentName": "Robert"}, {"StudentName": "Ramit"}, {"StudentName": "Adam"} ]); { ...
Read MoreHow to project specific fields from a document inside an array in Mongodb?
To project specific fields from a document inside an array in MongoDB, use the positional operator ($) in the projection parameter. This returns only the first matching array element that satisfies the query condition. Syntax db.collection.find( { "arrayField.subField": "matchValue" }, { "arrayField.$": 1 } ); Sample Data Let us create a collection with sample documents ? db.projectSpecificFieldDemo.insertMany([ { "UniqueId": 101, "StudentDetails": [ ...
Read MoreHow to query a key having space in its name with MongoDB?
To query a key having space in its name in MongoDB, wrap the field name with spaces in quotes and use dot notation for nested fields. Syntax db.collection.find({"field name with spaces": "value"}); db.collection.find({"parent.field name with spaces": "value"}); Create Sample Data First, let's create a document with a field that has spaces in its name ? var myValues = {}; myValues["Details"] = {}; myValues["Details"]["Student Name"] = "John"; myValues["Details"]["StudentAge"] = 26; db.keyHavingSpaceDemo.insertOne(myValues); { "acknowledged": true, "insertedId": ObjectId("5ca27e3b6304881c5ce84ba4") } Verify Sample ...
Read MoreGet Absolute value with MongoDB aggregation framework?
To get absolute values in MongoDB aggregation framework, use the $abs operator within the $project stage. The $abs operator returns the absolute value of a number, converting negative numbers to positive while keeping positive numbers unchanged. Syntax db.collection.aggregate([ { $project: { fieldName: { $abs: "$numberField" } } } ]); Sample Data Let us create a collection with positive, ...
Read MoreHow to prevent MongoDB from returning the object ID while finding a document?
To prevent MongoDB from returning the Object ID while finding a document, you need to set _id to 0 in the projection parameter of the find() method. This excludes the _id field from the query results. Syntax db.collection.find( { query }, { "_id": 0, "field1": 1, "field2": 1 } ); Sample Data Let us first create a collection with a sample document ? db.preventObjectIdDemo.insertOne({ "StudentName": "Chris", "StudentDetails": [ ...
Read More