programing

여러 $초로 쿼리를 그룹화하는 방법은?

telecom 2023. 10. 2. 10:57
반응형

여러 $초로 쿼리를 그룹화하는 방법은?

아래와 같이 문의하고 싶은데, 이것은 하나만 포함하고 있습니다.$cond.

2개로 질의하는 방법$cond?

collection.aggregate(
    { 
        $match : { 
            '_id' : {$in:ids}
        } 
    },
    {
        $group: {
            _id: '$someField',
            ...
            count: {$sum: { $cond: [ { $eq: [ "$otherField", false] } , 1, 0 ] }}
        }
    },
    function(err, result){
        ...
    }
);

안에 복합 표현을 사용하려는 경우{$cond:[]}- 다음과 같은 것.

collection.aggregate(
    { 
        $match : { 
            '_id' : {$in:ids}
        } 
    },
    {
        $group: {
            _id: '$someField',
            ...
            count: {$sum: { $cond: [ {$and : [ { $eq: [ "$otherField", false] },
                                               { $eq: [ "$anotherField","value"] }
                                     ] },
                                     1,
                                     0 ] }}
        }
    },
    function(err, result){
        ...
    }
);

연산자는 여기에 문서화되어 있습니다. http://docs.mongodb.org/manual/reference/operator/aggregation/ # boolean-

이와 같이 $초 안에 여러 개의 $초와 여러 개의 기준을 추가할 수 있습니다.'

collection.aggregate(
[
  {
    "$match": {
      //matching criteria
    }
  },
  {
    "$project": {
      "service": {
        "$cond": {
          "if": {
            "$eq": [
              "$foo",
              "bar"
            ]
          },
          "then": "return string1",
          "else": {
            "$cond": {
              "if": {
                "$eq": [
                  "$foo",
                  "bar"
                ]
              },
              "then": "return string2",
              "else": {
                "$cond": {
                  "if": {
                    "$or": [
                      {
                        "$eq": [
                          "$foo",
                          "bar1"
                        ]
                      },
                      {
                        "$eq": [
                          "$foo",
                          "bar2"
                        ]
                      }
                    ]
                  },
                  "then": "return string3",
                  "else": "$foo"
                }
              }
            }
          }
        }
      },
      "_id": 0
    }
  }
]
)

`

언급URL : https://stackoverflow.com/questions/20836978/how-to-group-query-with-multiple-cond

반응형