node.js - mongoDB Aggregate By Date and another field -
I provide a date field and AD id. I have the following code that produces the amount of all the offers viewed for each month.
db.useroffers.aggregate ({$ project: {date: {month: {$ month: "$ viewdate"}, Year: {$ Year: "$ viewdate"},}} }, {$ range: {offer_id: {$ in: [101,102,103,104,105,106]}}}, {$ group: {_id: {offer_id: "$ offer_id However, I need $ Mah- $ year group and the sum of all "$ Date =" $ date "offer for limited offer ID
< P> The question will be better with a sample document, though it seems that you have mixed in some conditions around it. First Pipe Lie In the document is to rebuild when you structure change for the date you want, then you have left other fields which you want to use later in other pipeline steps. There is another area that is included interactively _id
, so you need to explicitly declare those fields that you want.
< P> Second, only one numeric Argument, shows the number of documents to keep in the archive. It usually only makes sense to use it if you are testing a very large source and your entire pipeline is complete, or after you sort the end result, you want to return some entries. . Like P> Selection you are trying to do here, you need an operator, the way you are trying match > Offer_id
value. db.useroffers.aggregate (// match {$ match: {offer_id: {$ in: [101,102,103,104,105,106]}}}, // Change document group also {$ Project Can: {date: {month: {$ month: "$ view"}, year: {$ year: "$ viewdate"},}, offer_id: 1, view: 1}}, // group "date" and "Ofr_aidi" {$ group: {_id: {date: "$ date", offer_id: "$ offer_id"}, view: {$ sum: 1}}}, // output findings {$ project: {_id: 0 , Date: {$ concat: [{$ Substr: ["$ _id.date.month", 0, 2]}, "-", {$ substr: ["$ _id.date.year", 0, 4] }]}, Offer_id: "$ _id.offer_id, view: 1}})
Comments
Post a Comment