0

I have a mongodb collection called player_inventory that looks like this:

{
    "_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
        }
    ]
}

I've been looking around for a method to get rid of a deck from decklist by it's id, but I'm having trouble with it.

I'm currently trying to remove it doing it the following way, but it won't work:

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);

I have tried updating the document, but nothing works or I get errors, not even the official docs can help me out with.

ULI-R0
  • 171
  • 8
  • 2
    Arrays generally do not support add/remove operations. Lists and other advanced collections do. Of course this sounds more like you want to remove it from the Database, at wich point it becomes a delete and database design question, not a C# one. – Christopher Dec 03 '18 at 15:05
  • Thanks for referencing that one out, but how I would approach that solution to mines? he has a `"username": "bodrum"` element above his target element, and I have a list, I need to fix it now, `vanityselectors` is a list, how I would adapt my query? – ULI-R0 Dec 03 '18 at 15:06
  • 1
    @Christopher in Mongo, the only "collection" data structure is an array, which does support dynamically adding and removing items. It's C# that has specialized types (Array, List, IEnumerable, ...). – gunr2171 Dec 03 '18 at 15:08
  • actually the solution was here: https://stackoverflow.com/questions/32995762/nested-array-pull-query-using-c-sharp-mongodb-driver – ULI-R0 Dec 03 '18 at 15:33
  • thanks everyone! :D – ULI-R0 Dec 03 '18 at 15:33

0 Answers0