mongodb 子文档批量更新不完全的问题
巴扎黑
巴扎黑 2017-04-28 09:03:37
0
1
594

有如下数据结构

{
    "_id" : ObjectId("55dc1454f754a3342000002c"),
    "title" : "出轨如旋风 防火防盗防小三",
    "games" : [ 
        {
            "id" : 1615,
            "name_zh" : "穿越时空遇见你",
            "typename" : "休闲益智"
        },
        {
            "id" : 6356,
            "name_zh" : "恋爱物语3:梦想之翼",
            "typename" : "角色扮演"
        }, 
        {
            "id" : 6505,
            "name_zh" : "恋爱物语:湛蓝的回忆",
            "typename" : "角色扮演"
        }
    ]
}

/* 2 */
{
    "_id" : ObjectId("55dc145ef754a3342000002d"),
    "title" : "炎炎夏日,跟我一起High翻天!",
    "games" : [ 
        {
            "id" : 1128,
            "name_zh" : "追击野兽",
            "typename" : "角色扮演"
        }, 
        {
            "id" : 3276,
            "name_zh" : "世界僵尸大联盟",
            "typename" : "策略游戏"
        },
        {
            "id" : 10360,
            "name_zh" : "绿色忍者蛙年",
            "typename" : "角色扮演"
        }
    ]
}

/* 3 */
{
    "_id" : ObjectId("55dc1467f754a3342000002e"),
    "title" : "简约而不简单 像素游戏合辑",
    "games" : [ 
        {
            "id" : 1557,
            "name_zh" : "我的世界",
            "typename" : "休闲益智"
        },
        {
            "id" : 6228,
            "name_zh" : "像素地下城 汉化",
            "apk_size" : "5.39M",
            "typename" : "角色扮演"
        }, 
        {
            "id" : 6230,
            "name_zh" : "进化之地 汉化版",
            "typename" : "角色扮演"
        }
    ]
}

运行批量更新语句:

db.getCollection('xxxxx').update({'games.typename':'角色扮演'},{'$set':{'games.$.typename':'角色'}},0,1)

结果发现批量更新不完整,结果如下:

{
    "_id" : ObjectId("55dc1454f754a3342000002c"),
    "title" : "出轨如旋风 防火防盗防小三",
    "games" : [ 
        {
            "id" : 1615,
            "name_zh" : "穿越时空遇见你",
            "typename" : "休闲益智"
        },
        {
            "id" : 6356,
            "name_zh" : "恋爱物语3:梦想之翼",
            "typename" : "角色"
        }, 
        {
            "id" : 6505,
            "name_zh" : "恋爱物语:湛蓝的回忆",
            "typename" : "角色扮演"
        }
    ]
}

/* 2 */
{
    "_id" : ObjectId("55dc145ef754a3342000002d"),
    "title" : "炎炎夏日,跟我一起High翻天!",
    "games" : [ 
        {
            "id" : 1128,
            "name_zh" : "追击野兽",
            "typename" : "角色"
        }, 
        {
            "id" : 3276,
            "name_zh" : "世界僵尸大联盟",
            "typename" : "策略游戏"
        },
        {
            "id" : 10360,
            "name_zh" : "绿色忍者蛙年",
            "typename" : "角色扮演"
        }
    ]
}

/* 3 */
{
    "_id" : ObjectId("55dc1467f754a3342000002e"),
    "title" : "简约而不简单 像素游戏合辑",
    "games" : [ 
        {
            "id" : 1557,
            "name_zh" : "我的世界",
            "typename" : "休闲益智"
        },
        {
            "id" : 6228,
            "name_zh" : "像素地下城 汉化",
            "apk_size" : "5.39M",
            "typename" : "角色"
        }, 
        {
            "id" : 6230,
            "name_zh" : "进化之地 汉化版",
            "typename" : "角色扮演"
        }
    ]
}

请问更新语句要怎么写才能完整的更新所有符合条件的数据

巴扎黑
巴扎黑

reply all(1)
仅有的幸福

Mongo’s $ operator refers to the first matched array element, so it can only replace one array object. If you want to modify all matching objects of the embedded array, you can currently query and modify them before writing them. There is currently no operator that can directly implement this function.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template