从数组中删除元素?

时间:2018-12-03 14:58:58

标签: c# mongodb

我有一个名为player_inventory的mongodb集合,看起来像这样:

{
    "_id": ObjectId("5c04ac140adfb226ef597c9b"),
    "playerid": "H4F5F6E7FFE9D04N6I7NIFNKEN",
    "common": 500,
    "uncommon": 500,
    "rarity": 500,
    "mythical": 500,
    "coins": 1000000,
    "gems": 1000000,
    "tokensv1": 0,
    "tokensv2": 0,
    "trackerpos": 22,
    "inventoryprogress": 100,
    "packs": [],
    "vanity": null,
    "vanityselectors": [
        "name": "selector1"
     ],
    "decklist": [
        {
            "id": "aafcf24c-9d86-4808-b264-c97254635157",
            "name": "Super Deck",
            "description": "An amazing deck!",
            "format": "",
            "resourceId": "00000000-0000-0000-0000-000000000001",
            "deckTileId": 25068
        }
    ]
}

我一直在寻找一种通过decklist来摆脱id牌组的方法,但是我遇到了麻烦。

我目前正尝试通过以下方式将其删除,但无法正常工作:

var dbClient = new MongoClient();
var db = dbClient.GetDatabase("mtga");
var collection = db.GetCollection<PlayerInventory.Payload>("player_inventory");

var filter = Builders<PlayerInventory.Payload>.Filter.Eq("id", "H4F5F6E7FFE9D04N6I7NIFNKEN");
var update = Builders<PlayerInventory.Payload>.Update.Pull("decklist.$[].id", "aafcf24c-9d86-4808-b264-c97254635157");
var result = collection.UpdateOne(filter, update);

我已经尝试过更新文档,但是没有任何效果,或者出现错误,甚至连正式文档也无法帮助我。

0 个答案:

没有答案