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
Big Data Analytics Articles
Page 33 of 135
Convert date parts to date in MongoDB
To convert separate date parts (year, month, day) into a complete date in MongoDB, use the $dateFromParts operator within an aggregation pipeline. This operator combines individual date components into a proper date object. Syntax db.collection.aggregate([ { $project: { dateField: { $dateFromParts: { ...
Read MoreUpdate multiple elements in an array in MongoDB?
To update multiple elements in an array in MongoDB, use the $[] all positional operator. This operator modifies all elements in the specified array field that match the update criteria. Syntax db.collection.update( { "matchCondition": "value" }, { $set: { "arrayField.$[]": "newValue" } } ); Sample Data Let us first create a collection with documents ? db.demo385.insertOne({ "ServerLogs": [ { "status": ...
Read MoreDisplay only a single field from all the documents in a MongoDB collection
To display only a single field from all documents in a MongoDB collection, use projection in the find() method. Set the desired field to 1 to include it, or set unwanted fields to 0 to exclude them. Syntax db.collection.find({}, {fieldName: 1, _id: 0}); // OR db.collection.find({}, {unwantedField1: 0, unwantedField2: 0}); Sample Data db.demo384.insertMany([ {"StudentName": "Chris Brown", "StudentCountryName": "US"}, {"StudentName": "David Miller", "StudentCountryName": "AUS"}, {"StudentName": "John Doe", "StudentCountryName": "UK"} ]); { "acknowledged": true, ...
Read MoreMongoDB query to filter only the logs containing the "work" word in the content
To filter logs containing the word "work" in MongoDB, use the $filter operator with $indexOfBytes to perform case-insensitive substring matching within an aggregation pipeline. Syntax db.collection.aggregate([ { "$addFields": { "arrayField": { "$filter": { "input": "$arrayField", ...
Read MoreMongoDB aggregate $slice to get the length of the array
To get the length of an array in MongoDB using aggregation, use the $size operator within a $project stage. The $size operator returns the number of elements in the specified array field. Syntax db.collection.aggregate([ { $project: { "fieldName": { $size: "$arrayField" } } } ]); Sample Data Let us create a collection with documents ? db.demo382.insertOne({ "Name": "David", "details": [ { ...
Read MoreHow to search an array for values present in another array and output the indexes of values found into a new array in MongoDB?
To search an array for values present in another array and output the indexes of values found into a new array in MongoDB, use the $indexOfArray operator combined with $map within an aggregation pipeline. Syntax db.collection.aggregate([ { "$project": { "Result": { "$map": { ...
Read MoreMongoDB $addToSet to add a deep nested array of object?
The $addToSet operator adds a value to an array unless the value is already present, in which case $addToSet does nothing to that array. When working with deep nested arrays, combine $addToSet with the positional operator $ to target specific array elements. Syntax db.collection.update( { "parentArray.field": "matchValue" }, { $addToSet: { "parentArray.$.nestedArray": newObject } } ); Sample Data Let us first create a collection with documents − db.demo380.insertOne({ "details": [ { ...
Read MoreMongoDB projection on specific nested properties?
To project specific nested properties in MongoDB, use the aggregation pipeline with $addFields, $objectToArray, $filter, and $arrayToObject operators to selectively include nested fields based on conditions. Syntax db.collection.aggregate([ { "$addFields": { "nested.path": { "$arrayToObject": { "$filter": { ...
Read MoreManipulating subdocuments in MongoDB
To manipulate subdocuments in MongoDB, use dot notation to target specific fields within embedded documents or arrays. The $ positional operator helps update elements that match query conditions. Syntax db.collection.update( { "arrayField.field": "matchValue" }, { $operation: { "arrayField.$.targetField": value } } ); Create Sample Data db.demo378.insertOne({ Name: "Chris", details: [ { id: 101, Score: 56 }, { id: 102, Score: 78 ...
Read MoreHow can I concatenate an array of integer in MongoDB aggregation method?
To concatenate an array of integers in MongoDB aggregation, use the $reduce operator with $concat to combine array elements into a single string. Since integers must be converted to strings for concatenation, use $toString for type conversion. Syntax db.collection.aggregate([ { $project: { concatenatedField: { $reduce: { ...
Read More