sql >> Database teknologi >  >> NoSQL >> MongoDB

Gruppe tæller 2 element objekt array mongodb

Du har kun 2 "USER2", så kan du ikke $sum 4 $last på det _id.try at bruge compose _id, hvis du har brug for efter indeks i bruger.

db.teste.aggregate([
  {$group:{
    _id: {user:"$user",last:"$last"},
    total: {$sum:1},
    last: {
      $sum: {
        $cond:[ {$eq: ["$last", user]}, 1, 0]
      }
    }
  }}
])

Hvis du skal tilføje streng eller andre forekomster i forskellige felter. Forespørgslen er:

db.teste.aggregate([
  {$group:{
    _id: "$user"
  }},
  {$lookup:{
    from: "teste",
    let: { indice: "$_id" },
    pipeline: [
      {$group:{
        _id: null,
        user:{$sum:{$cond:[
          {$eq:["$user", "$$indice"]}, 1, 0
        ]}},
        last:{$sum:{$cond:[
          {$eq:["$last", "$$indice"]}, 1, 0
        ]}}
      }},
      {$project:{
        _id: 0
      }}
    ],
    as: "res"
  }}
])

Fra nul til helt:

//First we'll extract only what we need find, on this case distinct users
db.teste.aggregate([
  {$group:{
    _id: "$user"
  }}
])

//Here we will get the indexes of the search and reinsert in our filter using $let (nodejs var)
db.teste.aggregate([
  {$group:{
    _id: "$user"
  }},
  {$lookup:{
    from: "teste",
    let: { indice: "$_id" },
    pipeline: [],
    as: "res"
  }}
])

//Let's counter the total of reinserted elements
db.teste.aggregate([
  {$group:{
    _id: "$user"
  }},
  {$lookup:{
    from: "teste",
    let: { indice: "$_id" },
    pipeline: [
      {$group:{
        _id: null,
        total:{$sum:1}
      }}
    ],
    as: "res"
  }}
])

//Now let's test a true condition
db.teste.aggregate([
  {$group:{
    _id: "$user"
  }},
  {$lookup:{
    from: "teste",
    let: { indice: "$_id" },
    pipeline: [
      {$group:{
        _id: null,
        user:{$sum:{$cond:[
          {$eq:[true, true]}, 1, 0
        ]}}
      }}
    ],
    as: "res"
  }}
])

//cond tested let's extract what we want
db.teste.aggregate([
  {$group:{
    _id: "$user"
  }},
  {$lookup:{
    from: "teste",
    let: { indice: "$_id" },
    pipeline: [
      {$group:{
        _id: null,
        user:{$sum:{$cond:[
          {$eq:["$user", "$$indice"]}, 1, 0
        ]}},
        last:{$sum:{$cond:[
          {$eq:["$last", "$$indice"]}, 1, 0
        ]}}
      }}
    ],
    as: "res"
  }}
])

//Let's take the _id of the sub-colection because we do not need it
db.teste.aggregate([
  {$group:{
    _id: "$user"
  }},
  {$lookup:{
    from: "teste",
    let: { indice: "$_id" },
    pipeline: [
      {$group:{
        _id: null,
        user:{$sum:{$cond:[
          {$eq:["$user", "$$indice"]}, 1, 0
        ]}},
        last:{$sum:{$cond:[
          {$eq:["$last", "$$indice"]}, 1, 0
        ]}}
      }},
      {$project:{
        _id: 0
      }}
    ],
    as: "res"
  }}
])



  1. Deaktiver stop ordfiltrering i en MongoDB-tekstsøgning

  2. Spil! 2 Framework - Tilføj Java Mongo-driver

  3. MongoDB Java Driver oprettelse af database og samling

  4. Redis koncept:I hukommelsen eller DB?