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 55 of 135
MongoDB Aggregate sum of values in a list of dictionaries for all documents?
To aggregate sum of values in a list of dictionaries for all documents, use $unwind to flatten the array, then $group with $sum operators to calculate totals by grouping field. Syntax db.collection.aggregate([ { $unwind: "$arrayField" }, { $group: { _id: "$arrayField.groupByField", "fieldSum1": { $sum: "$arrayField.field1" }, ...
Read MoreHow to find only a single document satisfying the criteria in MongoDB?
To find only a single document satisfying specific criteria in MongoDB, use the findOne() method. This method returns the first matching document from the collection, even if multiple documents satisfy the query criteria. Syntax db.collection.findOne(query, projection) Sample Data db.demo116.insertMany([ {"EmployeeId": 101, "EmployeeName": "John"}, {"EmployeeId": 102, "EmployeeName": "Bob"}, {"EmployeeId": 103, "EmployeeName": "David"}, {"EmployeeId": 102, "EmployeeName": "Mike"} ]); { "acknowledged": true, "insertedIds": [ ...
Read MoreFind maximum score for duplicate Name values in MongoDB?
To find the maximum score for duplicate Name values in MongoDB, use the aggregation pipeline with $group and $max operators. This groups documents by name and calculates the highest score for each group. Syntax db.collection.aggregate([ { $group: { _id: "$fieldName", maxScore: { $max: "$scoreField" } } } ]); ...
Read MoreCreating views in MongoDB
To create views in MongoDB, use the createView() method. Views are read-only virtual collections that display data from underlying collections based on aggregation pipelines. Syntax db.createView( "viewName", "sourceCollection", [aggregationPipeline] ) Sample Data Let us create a collection with sample documents ? db.demo113.insertMany([ { _id: 1, StudentId: "101", "Details": { Name: "Chris", Age: 21 }, Subject: "MySQL" }, { _id: 2, StudentId: "102", "Details": { Name: "Alice", Age: 20 }, Subject: "MongoDB" }, ...
Read MoreHow to update a single field in a capped collection in MongoDB?
To update a single field in a capped collection in MongoDB, use the updateOne() or updateMany() method with the $set operator. Capped collections allow field updates but do not permit operations that would increase document size. Syntax db.collection.updateOne( { "field": "matchValue" }, { $set: { "field": "newValue" } } ); Create Sample Capped Collection First, create a capped collection with size and document limits ? db.createCollection("Demo112", { capped : true, size : 14, max : 3 } ); { "ok" : ...
Read MoreMongoDB Aggregation to slice array inside array
To slice an array inside another array in MongoDB, use the aggregate() method with $addFields, $map, and $slice operators. This allows you to limit the number of elements returned from nested arrays. Syntax db.collection.aggregate([ { $addFields: { "arrayField": { $map: { "input": "$arrayField", ...
Read MoreHow to only get the data of the nested JSON object in MongoDB?
To get the data of the nested JSON object in MongoDB, use findOne() with projection and the $elemMatch operator to filter and return only specific nested array elements. Syntax db.collection.findOne( { "matchField": "value" }, { "arrayField": { $elemMatch: { "nestedField": "value" } } } ); Sample Data db.demo109.insertOne({ "Name": "Chris", "Subjects": [ { "Id": "100", ...
Read MoreHow to work with variables in MongoDB query
To use variables in MongoDB queries, work with the var keyword to declare variables and reference them in your queries. This approach makes queries more flexible and reusable. Syntax var variableName = "value"; db.collection.find({"field": variableName}); Sample Data Let us create a collection with documents ? db.demo107.insertMany([ {"Name": "Chris"}, {"Name": "Bob"}, {"Name": "David"} ]); { "acknowledged": true, "insertedIds": [ ObjectId("5e2ee1b19fd5fd66da214471"), ...
Read MoreInsert array where element does not exist else update it (with multiple conditions)?
To insert array elements when they don't exist or update them when they do using multiple conditions, use bulkWrite() with multiple updateOne operations. This allows conditional updates and insertions in a single atomic operation. Syntax db.collection.bulkWrite([ { "updateOne": { "filter": { "field": "value", "array": { "$elemMatch": { conditions } } }, "update": { "$set": { "array.$.field": "newValue" } } ...
Read MoreImplement $dateToString on array items with MongoDB
To implement $dateToString on array items in MongoDB, use the $map operator inside an aggregation pipeline to transform date fields within array elements into formatted strings. Syntax db.collection.aggregate([ { $project: { "arrayField": { $map: { "input": "$arrayField", ...
Read More