mongo汇总如何选择“ $ graphLookup或$ lookup”

时间:2019-03-09 08:21:24

标签: mongodb mongoose

我不知道如何选择“ $ graphLookup或$ lookup”其他类似

我期待mongodb正式提供更完整的文档

示例:

{parentId: 0, cid: 1, other: 'a'},
{parentId: 0, cid: 2, other: 'b'},
{parentId: 0, cid: 3, other: 'c'},

{parentId: 1, cid: 11, other: 'aa'},
{parentId: 2, cid: 12, other: 'ab'},
{parentId: 3, cid: 13, other: 'ac'},

结果:

{
parentId: 0, cid: 1, other: 'a', 
  children: [
   {parentId: 1, cid: 11, other: 'aa'},
  ]
},{
parentId: 0, cid: 2, other: 'b',
  children: [
   {parentId: 2, cid: 12, other: 'ab'},
  ]
},{
parentId: 0, cid: 3, other: 'c',
  children: [
   {parentId: 3, cid: 13, other: 'ac'},
  ]
}
},

怎么办?

1 个答案:

答案 0 :(得分:0)

您需要使用$graphLookup

library(plotly)
library(shiny)
library(htmlwidgets)
library(colourpicker)
library(shinyjs)

## javascript from previous question's answer:
jsCode <- "shinyjs.changelegend = function(){
var paths = d3.select('#plot1').
select('.legend').
select('.scrollbox').
selectAll('.traces').
select('.scatterpts')
.attr('d','M8,0A8,8 0 1,1 0,-8A8,8 0 0,1 8,0Z');}"

ui <- fluidPage(
  tags$script(src = "https://d3js.org/d3.v4.min.js"),
  useShinyjs(),
  extendShinyjs(text = jsCode),
  fluidRow(
    column(2,numericInput(inputId = 'markersize_plot1', label = 'marker', min = 1, max = 40, value = 20)),
    column(2,numericInput(inputId = 'legendsize_plot1', label = 'legend', min = 1, max = 40, value = 10)),
    column(2,numericInput(inputId = 'markersize_plot2', label = 'marker', min = 1, max = 40, value = 4)),
    column(2,numericInput(inputId = 'legendsize_plot2', label = 'legend', min = 1, max = 40, value = 20))
  ),
  fluidRow(
    column(4,plotlyOutput("plot1")),
    column(4,plotlyOutput("plot2")),
    column(2,uiOutput('buttons_color_1'))
  ),
fluidRow(
  column(2,numericInput(inputId = 'markersize_plot3', label = 'marker', min = 1, max = 40, value = 10)),
  column(2,numericInput(inputId = 'legendsize_plot3', label = 'legend', min = 1, max = 40, value = 30)),
  column(2,numericInput(inputId = 'markersize_plot4', label = 'marker', min = 1, max = 40, value = 7)),
  column(2,numericInput(inputId = 'legendsize_plot4', label = 'legend', min = 1, max = 40, value = 40))
),
  fluidRow(
    column(4,plotlyOutput("plot3")),
    column(4,plotlyOutput("plot4")),
    column(2,uiOutput('buttons_color_2'))
    )
)


server <- function(input, output, session) {
  values <- reactiveValues(colors1 = c('red', 'blue', 'black'), colors2 = c('yellow', 'blue', 'green')  )


  lapply(c(1:2), function(i) {
  output[[paste('buttons_color_', i,sep = '')]] <- renderUI({
    isolate({ lapply(1:3, function(x) {  ## 3 in my app changes based on clustering output of my model
      Idname <- if(i == 1) { COLElement_1(x) } else {COLElement_2(x) }
      div(colourpicker::colourInput(inputId = Idname, label = NULL,
                                    palette = "limited", allowedCols = TheColors,
                                    value = values[[paste('colors', i, sep = '')]][x],
                                    showColour = "background", returnName = TRUE),
          style = " height: 30px; width: 30px; border-radius: 6px;  border-width: 2px; text-align:center; padding: 0px; display:block; margin: 10px")
    })
    })})

  outputOptions(output, paste('buttons_color_', i,sep = ''), suspendWhenHidden=FALSE)
  })


  COLElement_1 <-    function(idx){sprintf("COL_button_1-%d",idx)}
  lapply(1:3, function(ob) { 
  COLElement_1 <- COLElement_1(ob)
  observeEvent(input[[COLElement_1]], {
    values[[paste('colors', 1, sep = '')]][ob] <- input[[COLElement_1]]
    plotlyProxy("plot1", session) %>%
      plotlyProxyInvoke("restyle", list(marker = list(color = input[[COLElement_1]])), list(as.numeric(ob)-1))
    plotlyProxy("plot2", session) %>%
      plotlyProxyInvoke("restyle", list(marker = list(color = input[[COLElement_1]])), list(as.numeric(ob)-1))
  })  
  })

  COLElement_2 <-    function(idx){sprintf("COL_button_2-%d",idx)}
  lapply(1:3, function(ob) { 

  COLElement_2 <- COLElement_2(ob)
  observeEvent(input[[COLElement_2]], {
    values[[paste('colors', 2, sep = '')]][ob] <- input[[COLElement_2]]
    plotlyProxy("plot3", session) %>%
      plotlyProxyInvoke("restyle", list(marker = list(color = input[[COLElement_2]])), list(as.numeric(ob)-1))
    plotlyProxy("plot4", session) %>%
      plotlyProxyInvoke("restyle", list(marker = list(color = input[[COLElement_2]])), list(as.numeric(ob)-1))
  })
  })

  myplotly <- function(THEPLOT, xvar, setnr) {
    markersize <- input[[paste('markersize', THEPLOT, sep = '_')]] 
    markerlegendsize <- input[[paste('legendsize', THEPLOT, sep = '_')]]
    colors <- isolate ({values[[paste('colors', setnr, sep = '')]]  })
    p <- plot_ly(source = paste('plotlyplot', THEPLOT, sep = '.'))
    p <-  add_trace(p, data = mtcars, x = mtcars[[xvar]], y = ~mpg, type = 'scatter', mode = 'markers', color = ~as.factor(cyl), colors = colors)
    p <- layout(p, title = 'mtcars group by cyl with switching colors')
    p <- plotly_build(p) 


    # this is a bit of a hack to change the size of the legend markers to not be equal to the plot marker size.
    # it makes a list of 1 size value for each marker in de trace in the plot, and another half of with sizes that are a lot bigger.
    # the legend marker size is effectively the average size of all markers of a trace
    for(i in seq(1, length(sort(unique(mtcars$cyl) )))) {
      length.group <- nrow(mtcars[which(mtcars$cyl  == sort(unique(mtcars$cyl))[i]), ])
      p$x$data[[i]]$marker$size <- c(rep(markersize,length.group), rep(c(-markersize+2*markerlegendsize), length.group))
    }
    p
  } 



output$plot1 <- renderPlotly({ myplotly('plot1', 'hp', 1) })
output$plot2 <- renderPlotly({ myplotly('plot2', 'disp', 1)})
output$plot3 <- renderPlotly({ myplotly('plot3','hp', 2)})
output$plot4 <- renderPlotly({ myplotly('plot4', 'disp', 2)})
}

shinyApp(ui, server)

$lookup用于“加入”集合进行处理。