aggregation-framework

Mongo db java unwind operation in aggregate query throwing exception

隐身守侯 提交于 2020-01-05 04:57:22
问题 When working with embedded mongo document I am trying to unwind the array but I am getting exception like org.springframework.data.mapping.model.MappingInstantiationException: Failed to instantiate java.util.List using constructor NO_CONSTRUCTOR with arguments. The query what I wrote is, Aggregation agg = newAggregation( unwind("recipients"), match(Criteria.where("recipients.userId").is("800").andOperator(Criteria.where("recipients.status").is(false) ))); Logs.java private String id; private

MongoDB Mongoose querying a deeply nested array of subdocuments by date range

最后都变了- 提交于 2020-01-05 04:18:11
问题 I have a question that is similar to this other question but not exactly the same because my data structure is more deeply nested, and the accepted answer did not resolve the issue. Technologies: MongoDB 3.6, Mongoose 5.5, NodeJS 12 I am trying to query a deeply nested array of objects. The query will accept a "Start Date" and an "End Date" from the user. Item Report is an array of subdocuments that contains another array of subdocuments "Work Done By". All WorkDoneBy objects that have a

MongoDB best practice to count related documents

淺唱寂寞╮ 提交于 2020-01-05 04:08:01
问题 I was wondering what is the best practice to get a count of related documents of a particular document in MongoDB. Scenario is as follows: I need to get posts that users shared and I need to get total number of comments related to those posts as well. I would like to use the MongoDB Aggregation way if possible (if that is the best way of doing it) I am aware how to perform this query using separate methods .count() and .find(). document in posts collection: { _id: ObjectId(

MongoDB best practice to count related documents

↘锁芯ラ 提交于 2020-01-05 04:07:07
问题 I was wondering what is the best practice to get a count of related documents of a particular document in MongoDB. Scenario is as follows: I need to get posts that users shared and I need to get total number of comments related to those posts as well. I would like to use the MongoDB Aggregation way if possible (if that is the best way of doing it) I am aware how to perform this query using separate methods .count() and .find(). document in posts collection: { _id: ObjectId(

How to use $slice a $filter result in MongoDB?

China☆狼群 提交于 2020-01-05 04:00:14
问题 I have a item collection with the following format: { "_id": 123, "items": [{ "name": "item1", "status" : "inactive", "created" : ISODate("2018-02-14T10:39:28.321Z") }, { "name": "item2", "status" : "active", "created" : ISODate("2018-02-14T10:39:28.321Z") }, { "name": "item3", "status" : "active", "created" : ISODate("2018-02-14T10:39:28.321Z") }, { "name": "item4", "status" : "inactive", "created" : ISODate("2018-02-14T10:39:28.321Z") }, { "name": "item5", "status" : "active", "created" :

Mongoose aggregate Lookup - How to filter by specific id

泪湿孤枕 提交于 2020-01-04 12:50:15
问题 I am trying to make a aggregate pipeline - $lookup to receive from another collection only items that are not equal to specific _id for example : ClinicsCollection : {_id:1,name:'some name1'} {_id:2,name:'some name2'} {_id:3,name:'some name3'} BusinessCollection : {_id:1,name:"some business name",clinics:[1,2,3]} My aggregate pipeline query : db.business.aggregate([ {$match: {_id: mongoose.Types.ObjectId(businessId)}}, {$lookup: {from: "ClinicsCollection", localField: "clinics", foreignField:

Mongoose aggregate Lookup - How to filter by specific id

不问归期 提交于 2020-01-04 12:50:01
问题 I am trying to make a aggregate pipeline - $lookup to receive from another collection only items that are not equal to specific _id for example : ClinicsCollection : {_id:1,name:'some name1'} {_id:2,name:'some name2'} {_id:3,name:'some name3'} BusinessCollection : {_id:1,name:"some business name",clinics:[1,2,3]} My aggregate pipeline query : db.business.aggregate([ {$match: {_id: mongoose.Types.ObjectId(businessId)}}, {$lookup: {from: "ClinicsCollection", localField: "clinics", foreignField:

MongoDB aggregate from adjustable foreign collections

僤鯓⒐⒋嵵緔 提交于 2020-01-04 07:43:21
问题 My documents in the orders collection has _client key, which is an ObjectId references to another entity in another collection. The collection could be organization and could be users - I mean - it's variable collection. I want to tell Mongo to lookup if the _client id is found in both collections. { $lookup: { from: "users", // could be "organizations" let: { "client": "$_client" }, // could be "_organization" pipeline: [ { $match: { $expr: { $eq: ["$_id", "$$client"] }}}, ], as: "client" }

MongoDB aggregate pipeline slow after first match step

♀尐吖头ヾ 提交于 2020-01-04 06:04:13
问题 I have a MongoDB aggregate pipeline that contains a number of steps (match on indexed fields, add fields, sort, collapse, sort again, page, project results.) If I comment out all of the steps except the first match step, the query executes super fast (.075 seconds), as it's leveraging the proper index. However, if I then try to perform ANY follow up step, even something as simple as getting the results count, the query then starts taking 27 seconds!!! Here is the query: (Don't get too caught

MongoDB aggregate pipeline slow after first match step

我的梦境 提交于 2020-01-04 06:03:15
问题 I have a MongoDB aggregate pipeline that contains a number of steps (match on indexed fields, add fields, sort, collapse, sort again, page, project results.) If I comment out all of the steps except the first match step, the query executes super fast (.075 seconds), as it's leveraging the proper index. However, if I then try to perform ANY follow up step, even something as simple as getting the results count, the query then starts taking 27 seconds!!! Here is the query: (Don't get too caught