aggregation-framework

How do I filter to return a subset of a mongoose schema?

时光总嘲笑我的痴心妄想 提交于 2020-07-18 21:29:04
问题 My objective is to query mongoDB for a subset of an array within an array. For example, in the 'whole Data Set' (below), I would like to get all of the chorePerson records that have a personID of '5c6e3c74b9f5ed0016b00577' Whole Data Set [ { "_id": "5c7464a26b47a13470411031", "affiliation": "liss_family", "year": 2019, "weekNumber": 9, "chart": [ { "chorePerson": [ { "_id": "5c7464a26b47a13470411054", "person": "emily", "personID": "5c6e3c74b9f5ed0016b00577", "chore": "Catbox", "choreID":

Combine multiple groups in an aggregation in mongodb

不羁的心 提交于 2020-07-15 11:50:31
问题 If I have a collection like this: { "store" : "XYZ", "total" : 100 }, { "store" : "XYZ", "total" : 200 }, { "store" : "ABC", "total" : 300 }, { "store" : "ABC", "total" : 400 } I can get the $sum of orders in the collection by aggregation: db.invoices.aggregate([{$group: { _id: null, total: { $sum: "$total"}}}]) { "result": [{ "_id": null, "total": 1000 } ], "ok": 1 } And I can get the $sum of orders grouped by store: db.invoices.aggregate([{$group: { _id: "$store", total: { $sum: "$total"}}}

Combine multiple groups in an aggregation in mongodb

强颜欢笑 提交于 2020-07-15 11:49:30
问题 If I have a collection like this: { "store" : "XYZ", "total" : 100 }, { "store" : "XYZ", "total" : 200 }, { "store" : "ABC", "total" : 300 }, { "store" : "ABC", "total" : 400 } I can get the $sum of orders in the collection by aggregation: db.invoices.aggregate([{$group: { _id: null, total: { $sum: "$total"}}}]) { "result": [{ "_id": null, "total": 1000 } ], "ok": 1 } And I can get the $sum of orders grouped by store: db.invoices.aggregate([{$group: { _id: "$store", total: { $sum: "$total"}}}

Combine multiple groups in an aggregation in mongodb

对着背影说爱祢 提交于 2020-07-15 11:49:10
问题 If I have a collection like this: { "store" : "XYZ", "total" : 100 }, { "store" : "XYZ", "total" : 200 }, { "store" : "ABC", "total" : 300 }, { "store" : "ABC", "total" : 400 } I can get the $sum of orders in the collection by aggregation: db.invoices.aggregate([{$group: { _id: null, total: { $sum: "$total"}}}]) { "result": [{ "_id": null, "total": 1000 } ], "ok": 1 } And I can get the $sum of orders grouped by store: db.invoices.aggregate([{$group: { _id: "$store", total: { $sum: "$total"}}}

MongoDB assymetrical return of data, first item in array returned in full, the rest with certain properties omitted?

蹲街弑〆低调 提交于 2020-07-10 10:24:46
问题 I'm new to MongoDB and getting to grips with its syntax and capabilities. To achieve the functionality described in the title I believe I can create a promise that will run 2 simultaneous queries on the document - one to get the full content of one item in the array (or at least the data that is omitted in the other query, to re-add after), searched for by most recent date, the other to return the array minus specific properties. I have the following document: { _id : ObjectId(

MongoDB assymetrical return of data, first item in array returned in full, the rest with certain properties omitted?

回眸只為那壹抹淺笑 提交于 2020-07-10 10:23:44
问题 I'm new to MongoDB and getting to grips with its syntax and capabilities. To achieve the functionality described in the title I believe I can create a promise that will run 2 simultaneous queries on the document - one to get the full content of one item in the array (or at least the data that is omitted in the other query, to re-add after), searched for by most recent date, the other to return the array minus specific properties. I have the following document: { _id : ObjectId(

Aggregate query with where condition

三世轮回 提交于 2020-07-05 05:47:08
问题 How could the following SQL query be converted for Mongo: SELECT KeyID, SUM(Qty) FROM Table WHERE Date <= '1998-12-01' 回答1: Something like: db.myTable.aggregate([ { $match: { mydate: { $lte: new Date('12/01/1998') } } }, { $group: { _id: "$keyid", totalqty: { $sum: "$qty" } }} ]) 回答2: Can use this - db.schoolexamregistration.aggregate([ { $match: {"exam_session_id":"1523850138707"} },{ $lookup: { from: "student", localField: "nic_ppt", foreignField: "nic_ppt", as: "student_detail" } } ]) 来源:

Mongo db c# driver - how to join by id in collection?

无人久伴 提交于 2020-07-04 19:55:51
问题 I'm using Mongo DB c# driver 2, I'm trying to join 2 collections by ID (Many to Many) Class A { public string id; public string name; public list<string> classBReferenceid; // <--- I want use this as the "keys" for the join public list<B> myBs; } Class B { public string id; // <--- I use this as the "key" for the join public string name; } In my DB class "A" is saved without the data of "myBs" and I want to pull it from mongo in one call. I tried to use the Lookup function: IMongoCollection<A

Mongo db c# driver - how to join by id in collection?

拥有回忆 提交于 2020-07-04 19:53:39
问题 I'm using Mongo DB c# driver 2, I'm trying to join 2 collections by ID (Many to Many) Class A { public string id; public string name; public list<string> classBReferenceid; // <--- I want use this as the "keys" for the join public list<B> myBs; } Class B { public string id; // <--- I use this as the "key" for the join public string name; } In my DB class "A" is saved without the data of "myBs" and I want to pull it from mongo in one call. I tried to use the Lookup function: IMongoCollection<A

Mongodb using $text in aggregate query

大城市里の小女人 提交于 2020-06-29 03:38:13
问题 I'm trying to search in text with aggregate and I have the following code.Here is my aggregate query, but doesn't work. I expect to search first for the text and if find posts with that words in it to find user and then to query by user.name and the topic. const posts = await Post.aggregate([ { $match: { $text: { $search: postWords ? `${postWords}` : /.*/, $caseSensitive: false, }, }, }, { $lookup: { from: "users", localField: "user", foreignField: "_id", as: "user", }, }, { $unwind: "$user",