2
[{
  "_id": {
    "$oid": "5ee227d69dba6729ca8938fc"
  },
  "uuid": "4e9be217-a2c7-490f-86b7-2d46a69980a3",
  "locks": {
    "furnace_1591879638": {
      "type": "FURNACE",
      "location": {
        "world": "world",
        "x": -33,
        "y": 73,
        "z": -227
      },
      "created": "Thu Jun 11 14:47:18 CEST 2020",
      "peopleWithAccess": []
    },
    "chest_1591903237541": {
      "type": "CHEST",
      "location": {
        "world": "world",
        "x": -36,
        "y": 73,
        "z": -224
      },
      "created": "Thu Jun 11 21:20:37 CEST 2020",
      "peopleWithAccess": []
    }
  }
},{
  "_id": {
    "$oid": "5ee2864622c67536a249fb0a"
  },
  "uuid": "6fc93f76-b03b-4af3-a679-ac53cafdb288",
  "locks": {}
}]

Hi, I just stared with MongoDB and I was used to working with MySQL so this is really confusing for me. I've been trying to delete a object from an array but unsuccessfully. I tried this:

getMongoManager().getDatabase().getCollection("players").updateOne(new Document("uuid", player.getUniqueId().toString()),
                new Document("$unset", "locks." + id))

That gave me an error Modifiers operate on fields but we found type string instead. How would I delete for exmaple object furnace_1591879638 from player with uuid 4e9be217-a2c7-490f-86b7-2d46a69980a3 in Java?

1
  • There are some examples here. Commented Jun 12, 2020 at 2:10

1 Answer 1

2

Unfortunately, we cannot use the $unset operator to remove objects from an array, since the operator can only remove whole fields. For removing and inserting in an array, we are using $pull and $push.

I would recommend you to look up, both Bson Updates and Filters class because they are making it way easier, instead of using the operators (At least for the mongodb driver version 3.8 or higher; I do not know if older versions support Bson). With the help of these classes, you could extend your

MongoManager

class and try something like this:

public void pullByFilter(String queryField, Object queryValue, String arrayName, Object value) {
    MongoCollection<Document> collection = getCollection("players");
    Bson update = Updates.pullByFilter(Filters.eq(arrayName, value));
    collection.updateOne(Filters.eq(queryField, queryValue), update);
}

This method should remove the specified value from the array. Just for clearance: the queryField and queryValue parameters are used to identify the document (for you, the queryField should be "uuid" and the queryValue should be the players UUID as string).

Lastly, I think the method you tried with the $unset operator is giving you an error because you need to specify a new Document after the operator. This:

getMongoManager().getDatabase().getCollection("players").updateOne(new Document("uuid", player.getUniqueId().toString()),
                new Document("$unset", "locks." + id))

should rather be:

getMongoManager().getDatabase().getCollection("players").updateOne(new Document("uuid", player.getUniqueId().toString()),
                new Document("$unset", new Document("locks", id)))

Also, a good tutorial for updating documents can be found here.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.