monggo 嵌套數組操作

{
    "_id" : "201",
    "shopServiceCategorys" : [
        {
            "shopServiceCategoryId" : NumberLong(4593138),
            "shopid" : NumberLong(201),
            "serviceCategoryId" : NumberLong(5),
            "status" : 3.0
        },
        {
            "shopServiceCategoryId" : NumberLong(4593139),
            "shopid" : NumberLong(201),
            "serviceCategoryId" : NumberLong(12),
            "status" : 4.0
        }
    ],
   
}

更新 id=201,shopServiceCategoryId.serviceCategoryId=12 的status值爲0

執行($定位符,可以找到匹配的數組位置,但只更新第一個,怎麼更新所有匹配的):

db.getCollection('shop').update(
   {"_id":"201","shopServiceCategorys.serviceCategoryId":12},
   {"$set":{"shopServiceCategorys.$.status":0}
       }
)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章