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 75 of 111
How to remove white spaces (leading and trailing) from string value in MongoDB?
To remove white spaces (leading and trailing) from string values in MongoDB, you can use the forEach() method combined with JavaScript's trim() function to iterate through documents and update them. Syntax db.collection.find().forEach(function(doc) { doc.fieldName = doc.fieldName.trim(); db.collection.update( { "_id": doc._id }, { "$set": { "fieldName": doc.fieldName } } ); }); Sample Data Let us first create a collection with documents containing white spaces ? ...
Read MoreHow to add a field with static value to MongoDB find query?
To add a field with static value to MongoDB find query, use the $literal operator with the aggregation framework. The $literal operator returns a value without parsing, making it perfect for adding constant values to query results. Syntax db.collection.aggregate([ { $project: { field1: 1, field2: 1, "staticFieldName": { $literal: ...
Read MoreHow to get tag count in MongoDB query results based on list of names?
To get tag count in MongoDB query results based on a list of names, use the $in operator to match documents containing any of the specified names in an array field, then apply count() to get the total number of matching documents. Syntax db.collection.find({"arrayField": {$in: ["value1", "value2"]}}).count(); Sample Data db.tagCountDemo.insertMany([ {"ListOfNames": ["John", "Sam", "Carol"]}, {"ListOfNames": ["Bob", "David", "John"]}, {"ListOfNames": ["Mike", "Robert", "Chris"]}, {"ListOfNames": ["James", "Carol", "Jace"]} ]); { "acknowledged": true, ...
Read MoreGet documents expired before today in MongoDB?
To get documents expired before today in MongoDB, use the $lte operator with new Date() to find all documents where the date field is less than or equal to the current date. Syntax db.collection.find({ "dateField": { $lte: new Date() } }); Sample Data db.getDocumentsExpiredDemo.insertMany([ { "ArrivalDate": new ISODate("2019-05-11") }, { "ArrivalDate": new ISODate("2019-01-01") }, { "ArrivalDate": new ISODate("2019-05-10") }, { "ArrivalDate": new ISODate("2019-02-01") } ]); { "acknowledged": true, ...
Read MoreUpdate array in MongoDB document by variable index?
To update array in MongoDB document by variable index, use a JavaScript variable to dynamically construct the field path. This allows you to target specific array positions using a variable instead of hardcoding the index value. Syntax var indexVariable = yourIndexValue, updateObject = { "$set": {} }; updateObject["$set"]["arrayFieldName." + indexVariable] = "newValue"; db.collectionName.update({ "_id": ObjectId("...") }, updateObject); Sample Data Let us first create a collection with documents ? db.updateByVariableDemo.insertOne({ "StudentSubjects": ["MySQL", "Java", "SQL Server", "PL/SQL"] }); { ...
Read MoreDelete partial data in MongoDB?
To delete partial data in MongoDB, you can combine find(), skip(), and map() methods to select specific documents, then use remove() with the $in operator to delete them based on their IDs. Syntax var ids = db.collection.find({}, {_id: 1}).skip(N).toArray().map(function(doc) { return doc._id; }); db.collection.remove({_id: {$in: ids}}); Sample Data db.deleteDemo.insertMany([ {"Name": "John"}, {"Name": "Carol"}, {"Name": "Sam"}, {"Name": "David"}, {"Name": "Robert"}, {"Name": "Chris"}, ...
Read MoreSelecting only a single field from MongoDB?
To select only a single field from MongoDB, use the projection parameter in the find() method. Set the desired field to 1 to include it, and set _id to 0 to exclude the default ID field. Syntax db.collection.find( { query }, { fieldName: 1, _id: 0 } ); Sample Data db.selectingASingleFieldDemo.insertMany([ {"StudentFirstName": "John", "StudentAge": 23, "StudentCountryName": "US"}, {"StudentFirstName": "Carol", "StudentAge": 21, "StudentCountryName": "UK"}, {"StudentFirstName": "David", "StudentAge": 24, "StudentCountryName": "AUS"}, ...
Read MoreMongoDB divide aggregation operator?
The $divide operator in MongoDB performs division between two numbers during aggregation. It takes an array of exactly two numeric expressions and returns the result of dividing the first by the second. Syntax { $divide: [ , ] } Sample Data Let us first create a collection with documents ? db.aggregationOperatorDemo.insertOne({ "FirstValue": 392883, "SecondValue": 10000000000 }); { "acknowledged": true, "insertedId": ObjectId("5cd541452cba06f46efe9f01") } Following is the query to display all documents ...
Read MoreSearch an array of hashes in MongoDB?
To search an array of hashes in MongoDB, use dot notation to access fields within the hash objects stored in the array. This allows you to query specific key-value pairs within nested hash structures. Syntax db.collection.find({ "arrayName.fieldName": "value" }) Sample Data db.searchAnArrayDemo.insertMany([ { _id: 1, "TechnicalDetails": [{ "Language": "MongoDB" }] }, { _id: 2, "TechnicalDetails": [{ "Language": "MySQL" }] }, { _id: 3, "TechnicalDetails": [{ "Language": "MongoDB" }] }, { _id: 4, "TechnicalDetails": [{ "Language": "MongoDB" }] ...
Read MoreWant to update inner field in a MongoDB
To update an inner field in MongoDB, use the $set operator with dot notation to target the nested field within a document. This allows you to modify specific fields inside embedded documents without affecting other fields. Syntax db.collectionName.update( {"_id": ObjectId}, {$set: {"outerField.innerField": newValue}} ); Sample Data Let us create a collection with a document containing nested fields ? db.updateDocumentDemo.insertOne({ "StudentDetails": { "StudentFirstName": "Adam", "StudentLastName": ...
Read More