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
MongoDB Articles
Page 88 of 111
Remove null element from MongoDB array?
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 MoreWhy SHOW DBS does not show my databases in MongoDB?
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 MoreReturn a specific field in MongoDB?
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 MoreHow to drop a numeric collection from MongoDB?
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 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 MoreFinding highest value from sub-arrays in MongoDB documents?
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 MoreMongoDB order by two fields sum?
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 MoreMongoDB equivalent of WHERE IN(1,2,...)?
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 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 More