aggregation-framework

Mongodb if then condition under filter how to make

余生长醉 提交于 2020-05-14 14:18:50
问题 db.main.aggregate([ { $lookup: { from: "history", localField: "history_id", foreignField: "history_id", as: "History" } }, { $project: { "History": { $filter: { input: "$History", as: "his", if: { $eq: [ "5e4a8d2d3952132a08ae5764", "$$his.user_id" ] }, then: { cond: { $and: [ { $lt: [ "$$his.date", "$date" ] }, { $eq: [ "5e4a8d2d3952132a08ae5764", "$$his.user_id" ] } ] } }, else: {} } }, data: 1, history_id: 1, sender_id: 1, text: 1, date: 1 } }, { $unwind: "$History" } ]) MongoPlayground

Filter nested array with conditions based on multi-level object values and update them - MongoDB aggregate + update

二次信任 提交于 2020-05-14 02:27:05
问题 Considering I have the following documents in a collection (ignoring the _id ) : [ { "Id": "OP01", "Sessions": [ { "Id": "Session01", "Conversations": [ { "Id": "Conversation01", "Messages": [ { "Id": "Message01", "Status": "read", "Direction": "inbound" }, { "Id": "Message02", "Status": "delivered", "Direction": "internal" }, { "Id": "Message03", "Status": "delivered", "Direction": "inbound" }, { "Id": "Message04", "Status": "sent", "Direction": "outbound" } ] }, { "Id": "Conversation02",

Terribly degraded performance with other join conditions in $lookup (using pipeline)

梦想的初衷 提交于 2020-05-13 22:55:29
问题 So during some code review I decided to improve existing query performance by improving one aggregation that was like this: .aggregate([ //difference starts here { "$lookup": { "from": "sessions", "localField": "_id", "foreignField": "_client", "as": "sessions" } }, { $unwind: "$sessions" }, { $match: { "sessions.deleted_at": null } }, //difference ends here { $project: { name: client_name_concater, email: '$email', phone: '$phone', address: addressConcater, updated_at: '$updated_at', } } ]);

Terribly degraded performance with other join conditions in $lookup (using pipeline)

徘徊边缘 提交于 2020-05-13 22:54:25
问题 So during some code review I decided to improve existing query performance by improving one aggregation that was like this: .aggregate([ //difference starts here { "$lookup": { "from": "sessions", "localField": "_id", "foreignField": "_client", "as": "sessions" } }, { $unwind: "$sessions" }, { $match: { "sessions.deleted_at": null } }, //difference ends here { $project: { name: client_name_concater, email: '$email', phone: '$phone', address: addressConcater, updated_at: '$updated_at', } } ]);

The field “$name” must be an accumulator object

醉酒当歌 提交于 2020-05-10 10:14:19
问题 I have a query and when I use a $group a error shows "the field "$name must be an accumulator object", if if remove the filed "$name" all works well and i have tried to use only "name" instead of "$name" and the error continues. User.aggregate([ { $match: { "storeKey": req.body.store } }, { $group: { "_id": "$_id", "name": "$name", "count": { "$sum": 1 }, "totalValue": { "$sum": "$value" } } }, { $sort: sort }, { $skip: req.body.limit * req.body.page }, { $limit: req.body.limit } ])... 回答1:

Mongoose: how to use aggregate and find together

若如初见. 提交于 2020-05-09 17:54:27
问题 How can I use aggregate and find together in Mongoose? i.e I have the following schema: const schema = new Mongoose.Schema({ created: { type: Date, default: Date.now() }, name: { type: String, default: 'development' } followers: [{ type: Mongoose.Schema.ObjectId, ref: 'Users'}] ... }) export default Mongoose.model('Locations', schema) How can I query the users with only the fields name and followers_count . followers_count : the length of followers . There, I know we can use select to get

MongoDB: how to parse date in 3.6 mongoDb version?

微笑、不失礼 提交于 2020-05-09 11:07:56
问题 I have created Mongo Playground here Current output is showing result based on 15min time interval. (grouping updatedAt value by 15mins and shows avg for some field) Currently $dateToString and $dateFromString is using format to parse the date. I need to make it work for mongo version 3.6 (3.6 is not supporting format for $dateFromString) parsedDate: { $dateFromString: { dateString: "$_id.dateHour", format: "%Y-%m-%dT%H" } } If I remove format field from both $dateToString and $dateFromString

MongoDB: how to parse date in 3.6 mongoDb version?

时间秒杀一切 提交于 2020-05-09 11:06:34
问题 I have created Mongo Playground here Current output is showing result based on 15min time interval. (grouping updatedAt value by 15mins and shows avg for some field) Currently $dateToString and $dateFromString is using format to parse the date. I need to make it work for mongo version 3.6 (3.6 is not supporting format for $dateFromString) parsedDate: { $dateFromString: { dateString: "$_id.dateHour", format: "%Y-%m-%dT%H" } } If I remove format field from both $dateToString and $dateFromString

MongoDB: how to parse date in 3.6 mongoDb version?

蹲街弑〆低调 提交于 2020-05-09 11:06:30
问题 I have created Mongo Playground here Current output is showing result based on 15min time interval. (grouping updatedAt value by 15mins and shows avg for some field) Currently $dateToString and $dateFromString is using format to parse the date. I need to make it work for mongo version 3.6 (3.6 is not supporting format for $dateFromString) parsedDate: { $dateFromString: { dateString: "$_id.dateHour", format: "%Y-%m-%dT%H" } } If I remove format field from both $dateToString and $dateFromString

MongoDb: Convert date string to specific format in mongodb 3.6

浪子不回头ぞ 提交于 2020-05-09 07:19:43
问题 I need to parse date value to specific format without using format field in dateFromString operator. Mongo Playground Current situation : in Mongodb 4.0 if I format dateString using below it code it give me mentioned output. parsedDate: { $dateFromString: { dateString: "$dateS", format: format: "%Y-%m-%dT%H" } } Output: "parsedDate": ISODate("2020-01-16T08:00:00Z") I cannot use format field in 3.6 since its not supported. How do I convert my date to format: "%Y-%m-%dT%H" in 3.6? 回答1: If I get