MongoDB Articles

Page 88 of 111

Remove null element from MongoDB array?

Samual Sam
Samual Sam
Updated on 15-Mar-2026 1K+ Views

To remove null elements from a MongoDB array, use the $pull operator. This operator removes all instances of specified values from an array field, making it perfect for eliminating null entries. Syntax db.collection.update( { /* query criteria */ }, { $pull: { "arrayField": null } } ); Sample Data Let us create a collection with documents containing null values in arrays ? db.removeNullDemo.insertMany([ { "_id": 1, ...

Read More

Why SHOW DBS does not show my databases in MongoDB?

karthikeya Boyini
karthikeya Boyini
Updated on 15-Mar-2026 4K+ Views

The SHOW DBS command in MongoDB will not display a database until that database contains at least one collection with documents. MongoDB only allocates disk space for databases that actually contain data, which is why empty databases remain invisible in the database list. Syntax show dbs; // OR show databases; Example: Creating a Database Let's create a database named "web" and see why it doesn't appear in the database list ? use web; switched to db web Now let's check all databases ? show dbs; ...

Read More

Return a specific field in MongoDB?

Samual Sam
Samual Sam
Updated on 15-Mar-2026 454 Views

To return a specific field in MongoDB, use the projection parameter in the find() method. Set the desired field to 1 to include it and _id to 0 to exclude the default ID field. Syntax db.collection.find({}, {fieldName: 1, _id: 0}); Sample Data db.specificFieldDemo.insertMany([ {"FirstName": "John", "LastName": "Doe"}, {"FirstName": "John", "LastName": "Smith"}, {"FirstName": "David", "LastName": "Miller"}, {"FirstName": "Sam", "LastName": "Williams"} ]); { "acknowledged": true, "insertedIds": [ ...

Read More

How to drop a numeric collection from MongoDB?

Ankith Reddy
Ankith Reddy
Updated on 15-Mar-2026 213 Views

In MongoDB, numeric collection names cannot be accessed using standard dot notation. To drop a numeric collection, use the getCollection() method followed by drop(). Syntax db.getCollection("numericCollectionName").drop(); Create Sample Data First, create a numeric collection ? db.createCollection("2536464"); { "ok" : 1 } Now insert some documents in the collection ? db.getCollection("2536464").insertMany([ {"Record": 1}, {"Record": 2}, {"Record": 3} ]); { "acknowledged" : true, "insertedIds" ...

Read More

Get number of updated documents in MongoDB?

George John
George John
Updated on 15-Mar-2026 334 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

Finding highest value from sub-arrays in MongoDB documents?

Chandu yadav
Chandu yadav
Updated on 15-Mar-2026 302 Views

To find the highest value from sub-arrays in MongoDB documents, use the aggregation framework with $unwind, $sort, and $limit operators to flatten arrays, sort by the target field, and return the top result. Syntax db.collection.aggregate([ { $unwind: "$arrayField" }, { $sort: { "arrayField.valueField": -1 } }, { $limit: 1 } ]); Sample Data db.findHighestValueDemo.insertMany([ { _id: 10001, "StudentDetails": [ ...

Read More

MongoDB order by two fields sum?

Arjun Thakur
Arjun Thakur
Updated on 15-Mar-2026 534 Views

To order documents by the sum of two fields in MongoDB, use the aggregation framework with $project to calculate the sum and $sort to order the results. Syntax db.collection.aggregate([ { $project: { field1: 1, field2: 1, sumField: { $add: ["$field1", "$field2"] } } }, { $sort: { sumField: 1 } } // 1 for ascending, -1 for descending ]); Sample Data db.orderByTwoFieldsDemo.insertMany([ { "Value1": 10, "Value2": 35 }, { "Value1": 12, "Value2": 5 }, ...

Read More

MongoDB equivalent of WHERE IN(1,2,...)?

Ankith Reddy
Ankith Reddy
Updated on 15-Mar-2026 288 Views

The MongoDB equivalent of SQL's WHERE IN(1, 2, ...) clause is the $in operator. This operator allows you to match documents where a field value equals any value in a specified array. Syntax db.collectionName.find({ fieldName: { $in: [value1, value2, value3, ...] } }); Sample Data Let us create a collection with student documents ? db.whereInDemo.insertMany([ { "StudentName": "John", "StudentMathScore": 57 }, { "StudentName": "Larry", "StudentMathScore": 89 }, { "StudentName": "Chris", "StudentMathScore": 98 }, ...

Read More

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

George John
George John
Updated on 15-Mar-2026 391 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
Showing 871–880 of 1,106 articles
« Prev 1 86 87 88 89 90 111 Next »
Advertisements