aggregation-framework

Aggregate and reduce a nested array based upon an ObjectId

倖福魔咒の 提交于 2021-01-28 14:29:55
问题 I have an Event document structured like so and I'm trying to query against the employeeResponses array to gather all responses (which may or may not exist) for a single employee: [ { ... eventDate: 2019-10-08T03:30:15.000+00:00, employeeResponses: [ { _id:"5d978d372f263f41cc624727", response: "Available to work.", notes: "" }, ...etc ]; } ]; My current mongoose aggregation is: const eventResponses = await Event.aggregate([ { // find all events for a selected month $match: { eventDate: { $gte

How to Populate data using Mongodb Aggregate framework?

不打扰是莪最后的温柔 提交于 2021-01-28 11:32:30
问题 I have a current MongoDB Aggregate framework pipeline from a previous question and I am unable to add populate query to grab user profile by id. My code is below Product.aggregate([ { $group: { _id: { hub: "$hub", status: "$productStatus", }, count: { $sum: 1 }, }, }, { $group: { _id: "$_id.hub", counts: { $push: { k: "$_id.status", v: "$count", }, }, }, }, { $group: { _id: null, counts: { $push: { k: { $toString: "$_id" }, v: "$counts", }, }, }, }, { $addFields: { counts: { $map: { input: "

How to group data using mongo-template

只愿长相守 提交于 2021-01-28 11:27:17
问题 I have to filter data based on a criteria and set a field isObtained true or false. Then I am trying to group the data based on field grade . This is my data: { "school": "xyz", "studentName": "John Doe", "grade": "first", "Hobbies": [ "Painting", "Singing" ], "language": [ "English" ], "sport": "Badminton", "totalStudyHours": 85 }, { "school": xyz, "studentName": "Jane Doe", "grade": "third", "Hobbies": [ "Painting", ], "language": [ "Spanish" ], "sport": "Karate", "totalStudyHours": 65 }, {

Sort documents by value in the last element of an array that matches filter. Mongodb

假装没事ソ 提交于 2021-01-28 11:24:17
问题 I have a collection of evaluationGroups with the following documents structure: { "_id" : ObjectId("60073749694fd4d81e4d677d"), "AlertSettingId" : ObjectId("5ffddaaa0b1d2c30b191599a"), "CurrentStatus" : "success", "Evaluations" : [ { "EvaluatedAt" : ISODate("2021-01-19T19:47:18.850Z"), "ReferenceValue" : 1.0, "Status" : "success" }, { "EvaluatedAt" : ISODate("2021-01-19T19:52:16.423Z"), "ReferenceValue" : 1.0, "Status" : "triggered" }, { "EvaluatedAt" : ISODate("2021-01-19T21:47:16.400Z"),

How to return the variant values of each product if that product is a variant?

喜欢而已 提交于 2021-01-28 11:21:39
问题 I have a database in MongoDB like this {"productId" : 1, "isVariant": 1, "variantId" : 1, "attributeSet" : [ { "name" : "Capacity", "value" : "500 GB", "id" : 3 }, { "name" : "Form Factor", "value" : "5 inch", "id" : 4 }, { "id" : 5, "name" : "Memory Components", "value" : "3D NAND" } ] }, {"productId" : 2, "isVariant": 1, "variantId" : 1, "attributeSet" : [ { "name" : "Capacity", "value" : "1 TB", "id" : 3 }, { "name" : "Form Factor", "value" : "5 inch", "id" : 4 }, { "id" : 5, "name" :

$fitler nested array using $lte $gte

我只是一个虾纸丫 提交于 2021-01-28 11:06:34
问题 I am implementing a backend using Mongoose and MongoDB with "user" documents such as this: > db.users.find().pretty() { "_id" : ObjectId("5c46eb642c825626124b1e3c"), "username" : "admin", "searches" : [ ISODate("2019-01-30T14:52:07Z"), ISODate("2019-01-30T14:53:40Z"), ISODate("2019-01-30T14:54:48Z"), ISODate("2019-02-03T17:11:57Z"), ISODate("2019-02-04T06:40:00Z") ] } The searches field is an array logging the time when the user run some search functionality. I want to select a username and

Mongo. Query documents with an array whose childs ALL have to match a query

为君一笑 提交于 2021-01-28 10:45:59
问题 I am trying to write a query by using the mongo aggregation framework. What I want to achive is to select the orders where ALL shipments have been delivered more than a month ago . currently I am able to select the orders where AT LEAST ONE shipment has been delivered more than a month ago. Here's what I have: db['shop.orders'].aggregate( { $match: { shipments: { $elemMatch: { status: "Delivered", deliveredAt: {"$lte":new Date("2018-07-28")}} } } }) How do I have to alter my query to only

Updating an object property in a nested collection with another property of this object in MongoDB with c# driver

只愿长相守 提交于 2021-01-28 10:00:12
问题 I need to update property in nested collection. All examples describes about setting concrete value, but not about value from another field. MongoDB version: 4.2 C# driver version: 2.10.4 Product example: { _id: "1", Title: "Some title", Price: 20, OriginalPrice: 10, ... other props ... Variants: [ { Price: 21, OriginalPrice: 11 ... other props ... }, { Price: 22, OriginalPrice: 13 ... other props ... }, ] } I know how to update Price var pipeline = PipelineDefinition<Product, Product>.Create

Mongodb compare two fields with matched fields value aggregate

六眼飞鱼酱① 提交于 2021-01-28 09:00:58
问题 I am having problems in my query, The parameter I am passing are voucherId, and userId, I need to check if that user exceeded the limitedQuantityPerUser. If yes, exclude this voucher. But I am having problems comparing the matching fields, example if voucherId is 'aaa' , check if the quantityBrought > limitedQuantityPerUser. I have tried this vouchers.find({ 'status': { $in: ['OP', 'WG', 'LO', 'SO'] }, 'details.userReachedLimitedQuantity': { $ne: userId }, }, fields, { sort: { 'order': 1 } }

MongoDB converts number to string in scientific notation

半城伤御伤魂 提交于 2021-01-28 05:00:20
问题 I want to get full number as a String , but instead "1490650000000" it returns scientific notation "1.49065e+12" Here's how I try to convert it: {$substr: ["$myNumber", 0, -1]}; Any ideas how to prevent it? Note: I'm using v3.6 and can't upgrade to use $toString (thanks mlab). 回答1: If you have mongodb 4.0 then you can use $toLong aggregation db.collection.aggregate([ { "$project": { "myNumber": { "$toLong": "$myNumber" } } } ]) 回答2: You can try as below. db.collectionName.aggregate([ {