George John

George John

789 Articles Published

Articles by George John

Page 27 of 79

Get the size of a collection in MongoDB using conditions?

George John
George John
Updated on 15-Mar-2026 249 Views

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 More

Compare multiple properties in MongoDB?

George John
George John
Updated on 15-Mar-2026 215 Views

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 More

Update key value where another key equals some value in MongoDB?

George John
George John
Updated on 15-Mar-2026 280 Views

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 More

Display the last two values from field with MongoDB

George John
George John
Updated on 15-Mar-2026 132 Views

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 More

Removing an array element from MongoDB collection using update() and $pull

George John
George John
Updated on 15-Mar-2026 231 Views

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 More

Get number of updated documents in MongoDB?

George John
George John
Updated on 15-Mar-2026 328 Views

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 More

How to project specific fields from a document inside an array in Mongodb?

George John
George John
Updated on 15-Mar-2026 387 Views

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 More

How to query a key having space in its name with MongoDB?

George John
George John
Updated on 15-Mar-2026 2K+ Views

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 More

Get Absolute value with MongoDB aggregation framework?

George John
George John
Updated on 15-Mar-2026 309 Views

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 More

How to prevent MongoDB from returning the object ID while finding a document?

George John
George John
Updated on 15-Mar-2026 681 Views

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
Showing 261–270 of 789 articles
« Prev 1 25 26 27 28 29 79 Next »
Advertisements