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?

5
  • AFAIK those are the only possible solutions. Commented Feb 28, 2016 at 7: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? Commented Feb 28, 2016 at 7: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. Commented Feb 28, 2016 at 9: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. Commented Feb 28, 2016 at 10:48
  • @BlakesSeven, great, it works. Please, arrange your comment as the answer. Commented Feb 29, 2016 at 19:56

1 Answer 1

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