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 56 of 135
MongoDB Aggregate JSON array field for the matching field of other collection?
To aggregate JSON array field for matching fields from another collection in MongoDB, use $lookup combined with $unwind to join collections based on array elements, then reconstruct the result using $group. Syntax db.collection1.aggregate([ { $unwind: "$arrayField" }, { $lookup: { from: "collection2", localField: "arrayField.joinField", foreignField: "_id", as: "matchedData" }}, ...
Read MoreMongoDB query to implement nor query to fetch documents except a specific document
To fetch documents except a specific document, use the $nor operator in MongoDB. The $nor operator performs a logical NOR operation, returning documents that do not match any of the conditions in the array. Syntax db.collection.find({ $nor: [ { field1: value1 }, { field2: value2 } ] }); Sample Data Let's create a collection with sample documents ? db.demo100.insertMany([ { "Name": "Chris", "Age": 21 ...
Read MoreHow to group nested fields in MongoDB aggregation with count value in array?
To group nested fields in MongoDB aggregation with count values from arrays, use the $objectToArray operator to convert nested objects into key-value pairs, then use $unwind and $group to aggregate the count values. Syntax db.collection.aggregate([ { $project: { fieldName: { $objectToArray: "$nestedObject" } } }, { $unwind: "$fieldName" }, { $group: { _id: "$fieldName.k", count: { $sum: "$fieldName.v.countField" } } } ]) Sample Data db.demo99.insertMany([ { "Details": { ...
Read MoreIncrement a property value of an element in array object with MongoDB
To increment a property value of an element in a MongoDB array, use the $inc operator combined with the $ positional operator. The $ operator identifies the matched array element, while $inc increases the specified field by a given value. Syntax db.collection.update( { "arrayField.property": "matchValue" }, { $inc: { "arrayField.$.targetProperty": incrementValue } } ); Sample Data Let us create a collection with student details ? db.demo97.insertOne({ "Details": [ { ...
Read MoreFind sum of fields inside array in MongoDB?
To find sum of fields inside array in MongoDB, use the $sum operator with the aggregation pipeline. The $sum operator can directly reference array fields using dot notation to calculate the total of numeric values. Syntax db.collection.aggregate([ { $project: { "fieldName": 1, "totalSum": { $sum: ...
Read MoreHow to update array with multiple conditions in MongoDB
To update array with multiple conditions in MongoDB, use $elemMatch to match array elements with specific criteria, combined with $push and the positional operator $ to add new elements to the matched array element. Syntax db.collection.updateOne( { "arrayField": { "$elemMatch": { "field1": "value1", "field2": "value2" } } }, { "$push": { "arrayField.$.targetField": newValue } } ); Sample Data db.demo94.insertOne({ "Details": [ { ...
Read MoreHow to convert date to timestamp in MongoDB
To convert date to timestamp in MongoDB, use the $toLong operator within an aggregation pipeline. This converts an ISODate object to its Unix timestamp representation in milliseconds. Syntax db.collection.aggregate([ { $addFields: { "timestampField": { $toLong: "$dateField" } } } ]); Sample Data db.demo93.insertMany([ {"UserName": "Chris", "ArrivalDate": new ISODate("2020-10-01")}, {"UserName": ...
Read MoreFind records on or after a specific date in MongoDB?
To find records on or after a specific date in MongoDB, use the $gte (greater than or equal) operator with ISODate to perform date comparisons. Syntax db.collection.find({ "dateField": { $gte: ISODate("YYYY-MM-DD") } }); Sample Data Let us create a collection with documents containing arrival dates ? db.demo91.insertMany([ { "ArrivalDate": new ISODate("2020-01-10") }, { "ArrivalDate": new ISODate("2019-12-14") }, { "ArrivalDate": new ISODate("2020-01-15") } ]); { "acknowledged": true, ...
Read MoreMongoDB query to find documents whose array contains a string that is a substring of a specific word
To find documents whose array contains a string that is a substring of a specific word, use MongoDB's aggregation pipeline with $match, $expr, and $anyElementTrue operators combined with $indexOfBytes to check substring matches. Syntax db.collection.aggregate([ { $match: { $expr: { $anyElementTrue: { ...
Read MoreMongoDB query to get array of nested string?
To get array of nested string in MongoDB, use dot notation in find() to query nested array fields. This allows you to search for documents containing specific string values within nested arrays. Syntax db.collection.find({ "parentArray.nestedField": "searchValue" }); Sample Data db.demo89.insertMany([ { id: 101, Details: [ { Name: "Chris", Marks: 45 }, ...
Read More