根据两个mongo字段的组来查找总计

时间:2017-11-12 12:32:21

标签: mongodb mongoose mongodb-query

我有这样的收集数据 -

library(shiny)

# test data
write.csv(reshape2::tips, "tips.csv", row.names = FALSE)
write.csv(mtcars, "mtcars.csv")

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      fileInput('datafile', 'Choose CSV file',
                accept = c('text/csv', 'text/comma-separated-values,text/plain', '.csv')),
      conditionalPanel(
        # use a server side condition
        condition = "output.fileUploaded",
        # placeholders will be replaced from the server
        selectInput("pic", "Primay C", "placeholder 1"),
        selectInput("level", "select level", "placeholder 2")
      )
    ),
    mainPanel(
      h3("filtered data"),
      tableOutput("table")
    )
  )
)

server <- function(input, output, session){
  # create reactive version of the dataset (a data.frame object)
  filedata <- reactive({
    infile <- input$datafile
    if (is.null(infile))
      # User has not uploaded a file yet. Use NULL to prevent observeEvent from triggering
      return(NULL)
    temp <- read.csv(infile$datapath)
    temp[order(temp[, 1]),]
  })

  # inform conditionalPanel wheter dropdowns sohould be hidden
  output$fileUploaded <- reactive({
    return(!is.null(filedata()))
  })
  outputOptions(output, 'fileUploaded', suspendWhenHidden=FALSE)

  # update the selectInput elements according to the dataset

  ## update 'column' selector
  observeEvent(filedata(), {
    updateSelectInput(session, "pic", choices = names(filedata()))
  })

  ## update 'level' selector
  observeEvent(input$pic, {
    column_levels <- unique(filedata()[[input$pic]])
    updateSelectInput(session, "level", choices = column_levels,
                      label = paste("Choose level in", input$pic))
  }, ignoreInit = TRUE)

  # show table
  output$table <- renderTable({
    subset(filedata(), filedata()[[input$pic]] == input$level)
  }, bordered = TRUE)
}

shinyApp(ui, server)

我希望以下面的格式获得输出 -

{
    "user_id" : "1",
    "branch_id" : "1",
    "total" : 100,
},
{
    "user_id" : "1",
    "branch_id" : "1",
    "total" : 200
},
{
    "user_id" : "1",
    "branch_id" : "3",
    "total" : 1400
},
{
    "user_id" : "2",
    "branch_id" : "1",
    "total" : 100
},
{
    "user_id" : "2",
    "branch_id" : "1",
    "total" : 100
},

我尝试了一个mongo聚合查询,但是查询将输出视为未定义。

基本上我需要每个分支机构明智地获得他所获得的总分。

以下是我尝试过的但没有工作的内容 -

[
{
    "user_id":"1",
    "branch_id":"1",
    "grand_total":"300"
},
{
    "user_id":"1",
    "branch_id":"3",
    "grand_total":"1400"
},
{
    "user_id":"2",
    "branch_id":"1",
    "grand_total":"200"
}
]

请帮忙。

1 个答案:

答案 0 :(得分:0)

您的汇总管道必须如下所示:

{
    "$group": {
        "_id": {
            user_id: "$user_id",
            branch_id: "$branch_id"
        },
        "grand_total": {
            "$sum": "$total"
        },

    }
}, {
    "$project": {
        "_id": 0,
        "user_id": "$_id.user_id",
        "branch_id": "$_id.branch_id",
        "total": "$grand_total"
    }
}

_id管道中的"$group"字段中,添加要将文档分组的字段。如果您只想按一个字段分组,可以按如下方式编写:

{"$group": {
        "_id": "$user_id"
        }
}

如果您想要分组多个字段(就像您的情况一样),那么您可以按如下方式编写它:

{"$group": {
        "_id": {
           user_id: "$user_id",
           branch_id: "$branch_id"
           }
        }
}

每个聚合管道都会更改您的文档。因此,如果您将所有总计的总和称为$group

,则在"grand_total"
"grand_total": {
            "$sum": "$total"
        }

然后在您的$project渠道中,total字段不再存在。但相反,我们创建了一个新的字段(grand_total),即总和。