4

I'm using MongoDB 3.2 and MongoDB Java Driver 3.2. I want to update the value the document having its ID. In order to do that I tried to use the following two approaches (found in Stackoverflow and MongoDB Blog):

Approach #1:

for(String docID : expiredDocsIDs) {

    Bson filter = Filters.eq("_id", docID);

    Bson updates = Updates.set("isExpired", true);

    dbCollection.findOneAndUpdate(filter, updates);
}

Approach #2:

expiredDocsIDs.stream()
    .forEach(docID -> {

        BasicDBObject updateFields = new BasicDBObject();
        updateFields.append("isExpired", true);
        updateFields.append("fetchStatus", "FETCHED");

        BasicDBObject setQuery = new BasicDBObject();
        setQuery.append("$set", updateFields);

        BasicDBObject searchQuery = new BasicDBObject("_id", docID);

        dbCollection.updateOne(searchQuery, setQuery);
});

None of these approaches does not work. It iterates over the list of documents IDs, executes the code but at the end of the code, when I check the documents in DB there is no any change in the documents' field I tried to update.

How can I update the specific document in MongoDB?

Mike
  • 14,010
  • 29
  • 101
  • 161
  • AFAIK those are the only possible solutions. – Pragnani Feb 28 '16 at 07:43
  • @PragnaniKinnera, it does not work for me. I checked, the `_id` is correct but there is no any change after code execution. Do I miss something? – Mike Feb 28 '16 at 07:45
  • I think you are missing something for sure. What is not working? How about an explanation of what you think "not working" means. So what is happening? What do you expect to happen? All of which is missing from this question. Understand to the rest of us, that's how we do it. So we need to understand why it's not good enough for you. – Blakes Seven Feb 28 '16 at 09:28
  • 1
    So clear and basic problem is `docID` is of type `String`. But the likely data actually in `_id` I'm betting is an `ObjectId()` which simply has not been cast. So if you cast your strings to `ObjectId()` values that they actually are, then something matches. This is one reason why you should always check the response objects from `.update()` calls. It would be telling you that nothing was matched. – Blakes Seven Feb 28 '16 at 10:48
  • @BlakesSeven, great, it works. Please, arrange your comment as the answer. – Mike Feb 29 '16 at 19:56

1 Answers1

5

As BlakesSeven correctly noted, the problem was with a casting of _id field. The original code sent this parameter as String while the correct way is to send a parameter of ObjectId type.

The correct and worked code form MongoDB 3.2:

this.trackedEpisodesReg.entrySet().stream()
    .filter(ep -> ep.getValue().isExpired())
    .forEach(ep -> {

        BasicDBObject updateFields = new BasicDBObject();
        updateFields.append("isExpired", true);

        BasicDBObject setQuery = new BasicDBObject();
        setQuery.append("$set", updateFields);

        BasicDBObject searchQuery = new BasicDBObject("_id", new ObjectId(ep.getValue().getEpisodeID()));

        dbCollection.updateOne(searchQuery, setQuery);
    });
Community
  • 1
  • 1
Mike
  • 14,010
  • 29
  • 101
  • 161