mongo db

[MongoDB] update 쿼리 만들기(Aggregation)

nan2 2023. 1. 2. 15:34
반응형
String update_aggregation_query = "[" +
                    "    {" +
                    "        \"$match\": {" +
                    "            \"{{Field name}}\": \"" + {{Value}} + "\"" +
                    "        }" +
                    "    }, {" +
                    "        \"$addFields\": " + {{update할 field와 value}} +
                    "    }, {" +
                    "        \"$merge\": {" +
                    "            \"into\": \"{{Collection name}}\", " +
                    "            \"on\": \"{{Primary Key}}\", " +
                    "            \"whenMatched\": \"merge\", " +
                    "            \"whenNotMatched\": \"fail\"" +
                    "        }" +
                    "    }" +
                    "]";

 

- addFields : 새로운 field 추가할 때 사용하며, 기존 field와 이름이 같은 경우 기존 값만 변경됨

- merge : 파이프라인의 마지막 단계

-> whenMatched: <replace|keepExisting|merge|fail|pipeline>

-> whenNotMatched: <insert|discard|fail>

반응형