0

キーを持つ内部配列を引っ張る方法address1

"_id": 2,
"info": {
    "address1": {
        "city": {
            "0": "Indore"
        },
        "state": {
            "0": "MP"
        }
    },
    "address2": {
        "city": {
            "0": "Mhow"
        },
        "state": {
            "0": "MP"
        }
    }
}

データを削除した後は、次のようになります。

"_id": 2,
"info": {
    "address1": {
        "city": {
            "0": "Indore"
        },
        "state": {
            "0": "MP"
        }
    }
}

私はこれを使用しましたdb.info.update({"_id":2},{'$pull':{"info":{"address1":{'$exists':true}}}}) が、エラーが発生しますCannot apply $pull/$pullAll modifier to non-array

4

2 に答える 2

1

あなたが本当に欲しいのはアドレスの配列のように私には見えますよね?それで、ユーザーは無制限のアドレスを持つことができますか?その場合は、次のような配列が必要です。

"_id": 2,
"info": {
    "address": [{
        "city": {
            "0": "Indore"
        },
        "state": {
            "0": "MP"
        }
    },
     {
        "city": {
            "0": "Mhow"
        },
        "state": {
            "0": "MP"
        }
    }]
}

アドレスを配列として使用すると、$pushと$pullを使用できます

于 2012-04-10T06:36:47.013 に答える
0

モンゴコンソールから:

> db.tester.find()
{ "_id" : 2, "info" : { "address1" : { "city" : { "0" : "Indore" }, "state" : { "0" : "MP" } }, "address2" : { "city" : { "0" : "Mhow" }, "state" : { "0" : "MP" } } } }
> db.tester.findAndModify({query: {"_id":2}, update : {$unset: {"info.address2":1}}, new:true})
{
    "_id" : 2,
    "info" : {
        "address1" : {
            "city" : {
                "0" : "Indore"
            },
            "state" : {
                "0" : "MP"
            }
        }
    }
}
> db.tester.find()
{ "_id" : 2, "info" : { "address1" : { "city" : { "0" : "Indore" }, "state" : { "0" : "MP" } } } }

これはあなたが必要とすることを達成しますか?

于 2012-04-10T06:07:17.793 に答える