IT/MongoDB

몽고디비 - findOneAndUpdate

KeepGooing 2020. 6. 13. 14:58
반응형

오늘 사용할 것은 arrayFilters이며

이는 몽고디비 버전 3.6이상부터 가능하오니

가급적이면 3.6이상 혹은 4.0이상으로 버전업으로 하시고 사용하시길 바랍니다.

 

 

 

이번에는 john의 returnData를 파인애플에서 포도로 바꾸는 쿼리이다.

$set 과 arrayFilters 를 쓴다.

(참고로 String형으로 특정 필드의 값을 변경하고 싶으면 $set을 Array형으로 특정 필드를 변경하고 싶으면 $push를 쓴다.)

 

 

$set 과 arrayFilters

db.getCollection('intent_test').findOneAndUpdate(
{intent : "MBTI_TYPE"},
{$set : {"intentArray.$[t].executeInfo.$[c].type2Data.$[d].returnData" : "포도"}},
{arrayFilters : [{"t.type1" : "ISTJ"}, {"c.type2" : "male"}, {"d.searchData.name" : "john"}]},
{upsert:true}
);

 

결과

 

/* 1 */
{
    "_id" : ObjectId("5edf2e8ec4130e33cda725b3"),
    "intent" : "MBTI_TYPE",
    "intentArray" : [ 
        {
            "type1" : "ISTJ",
            "executeInfo" : [ 
                {
                    "check" : "foodTendency",
                    "type2" : "female",
                    "type2Data" : [ 
                        {
                            "searchData" : {
                                "name" : "Jessica",
                                "height" : "165",
                                "weight" : "42"
                            },
                            "returnData" : "파인애플"
                        }, 
                        {
                            "searchData" : {
                                "name" : "Amy",
                                "height" : "165",
                                "weight" : "42"
                            },
                            "returnData" : "사과"
                        }, 
                        {
                            "searchData" : {
                                "name" : "Alice",
                                "height" : "165",
                                "weight" : "42"
                            },
                            "returnData" : "샌드위치"
                        }
                    ]
                }, 
                {
                    "check" : "foodTendency",
                    "type2" : "male",
                    "type2Data" : [ 
                        {
                            "searchData" : {
                                "name" : "john",
                                "height" : "175",
                                "weight" : "72"
                            },
                            "returnData" : "포도"
                        }, 
                        {
                            "searchData" : {
                                "name" : "josh",
                                "height" : "190",
                                "weight" : "92"
                            },
                            "returnData" : "사과"
                        }, 
                        {
                            "searchData" : {
                                "name" : "kan",
                                "height" : "185",
                                "weight" : "82"
                            },
                            "returnData" : "샌드위치"
                        }
                    ]
                }
            ]
        }
    ]
}

 

이를 프로그래밍적으로 유용하게 적용하기 위해서는

 

특정 필드의 key 값을 뽑거나 혹은 변수로 받아 

arrayFilters의 각 $연산자의 array이의 필드의 값으로 이용하여

원하는 데이터의 값만 변경하면 된다.

 

 

 

이번엔 array형태의 값을 넣기 위한 $push와 arrayFilters 연산자를 이용한다.

이름은 Tom, 키 188 , 몸무게  86 그리고 returnData를 망고를 type2가 male인 type2Data Array 안에 넣는 쿼리이다.

db.getCollection('intent_test').findOneAndUpdate(
{intent : "MBTI_TYPE"},
{$push : {"intentArray.$[t].executeInfo.$[c].type2Data" : {"searchData" : {"name" : "Tom" , "height" : "188" , "weight" : "86" } , "returnData" : "망고"}}},
{arrayFilters : [{"t.type1" : "ISTJ"}, {"c.type2" : "male"}, ]},
{upsert:true}
);

 

결과

 

/* 1 */
{
    "_id" : ObjectId("5edf2e8ec4130e33cda725b3"),
    "intent" : "MBTI_TYPE",
    "intentArray" : [ 
        {
            "type1" : "ISTJ",
            "executeInfo" : [ 
                {
                    "check" : "foodTendency",
                    "type2" : "female",
                    "type2Data" : [ 
                        {
                            "searchData" : {
                                "name" : "Jessica",
                                "height" : "165",
                                "weight" : "42"
                            },
                            "returnData" : "파인애플"
                        }, 
                        {
                            "searchData" : {
                                "name" : "Amy",
                                "height" : "165",
                                "weight" : "42"
                            },
                            "returnData" : "사과"
                        }, 
                        {
                            "searchData" : {
                                "name" : "Alice",
                                "height" : "165",
                                "weight" : "42"
                            },
                            "returnData" : "샌드위치"
                        }
                    ]
                }, 
                {
                    "check" : "foodTendency",
                    "type2" : "male",
                    "type2Data" : [ 
                        {
                            "searchData" : {
                                "name" : "john",
                                "height" : "175",
                                "weight" : "72"
                            },
                            "returnData" : "포도"
                        }, 
                        {
                            "searchData" : {
                                "name" : "josh",
                                "height" : "190",
                                "weight" : "92"
                            },
                            "returnData" : "사과"
                        }, 
                        {
                            "searchData" : {
                                "name" : "kan",
                                "height" : "185",
                                "weight" : "82"
                            },
                            "returnData" : "샌드위치"
                        }, 
                        {
                            "searchData" : {
                                "name" : "Tom",
                                "height" : "188",
                                "weight" : "86"
                            },
                            "returnData" : "망고"
                        }
                    ]
                }
            ]
        }
    ]
}

 

반응형

'IT > MongoDB' 카테고리의 다른 글

$unset (Field Update Operators)  (0) 2021.04.12
JSON.parse() 활용하여 몽고디비 필드 동적 생성  (0) 2021.04.11
MONGODB - aggregate  (0) 2020.06.13
MONGODB - find  (0) 2020.06.13
MONGODB - 기본구조  (0) 2020.06.09