揮発性のメモ2

http://d.hatena.ne.jp/iww/

MongoDBで、配列の要素数を減らす

> db.controltable.findOne({ id: 123 })
{
        "_id" : ObjectId("xxxxxxxxxxxxxxxxxxxxxxxx"),
        "id" : 123,
        "name" : "ほげ",
        "control" : {
                "within" : [
                        "Alpha",
                        "Bravo",
                        "Charlie",
                        "Delta"
                ]
        }
}

.control.within[] の配列の長さを短くしたい
$pop をつかい、配列からひとつ取得することで末尾を削除する
1段深いところにあるので、つなげて "control.within" と書く

> db.controltable.update({ id: 123 }, { "$pop": { "control.within": 1 }})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.controltable.findOne({ id: 123 })
{
        "_id" : ObjectId("xxxxxxxxxxxxxxxxxxxxxxxx"),
        "id" : 123,
        "name" : "ほげ",
        "control" : {
                "within" : [
                        "Alpha",
                        "Bravo",
                        "Charlie"
                ]
        }
}