'Unresponsive' document, refuses to update

First time using this, sorry if incorrect tags.

I’ve just ran into an issue where i was able to retrieve a document, but i could not add or edit properties, no matter what i tried. All data was read successfully, and after deleting and recreating the document, it started to work again without any code changes. The document had only 10 entries with little data in them so size wasnt an issue. I also was still able to add and delete documents in the collection, which only had that single file.

Anyone else ran into this issue? I have 0 clue what could have caused it

Update: I had an object that stored discord snowflakes mapped to a string. Adding 123456 or 12 appears to cause the document to not respond, removing it fixes it

If you could share what you are actually doing, some code, we could help identify what you are doing wrong. From your text there is no way for us to help.

Hi,
Please share your MongoDB version used and the command you have executed and clear error.

Regards

This is core code, i tried to minimize as much as possible whilst still having the bug. Using typescript with node v20.7.0 and mongodb 6.1.0.

export const testfn = async ()  =>{
    const coll = db.collection("blacklist2")
    const doc = await coll.findOne({blacklist: "blacklist"})
    if(!doc) return;

    const newdoc = {
        $set: {
            randomprop: "foobarfoo"
        }
    }
    await coll.updateOne(doc, newdoc)
}

The current db entry looks like

{
  "_id": {
    "$oid": "634c11e7c23e564ae4c4cd2a"
  },
  "blacklist": "blacklist",
  "anonymous": {
    "arandomkey": "test"
  },
  "randomprop": "foobar"
}

When manually adding the following field, "12": "a", using MongoDB Compass, to the anonymous object, the document no longer will update until this key is removed. No errors occurs, and the code after the updateOne still executes.

Why to you findOne() before attempting the changes?

If no document matches the query no update will occur. You are duplicating what the server is doing but much slower.

Get rid of the findOne and the !doc return and simply updateOne( { blacklist : “blacklist” } , newdoc ) ;

you can use upsert when updating a document…if the document doesn’t exist an entry will be inserted: https://www.mongodb.com/docs/manual/reference/method/db.collection.update/

Yes but doing an upsert in this case would not lead to the same functionality of testfn().

It is clear from the code that the intent is that randomprop must be set to foobarfoo if and only if there is at lease one document with

{ blacklist : "blacklist" }

With an upsert, a new document is created if none existed.

More on the above, which I missed at first.

I really really really do not know how you are able to manually add a field in the extremely short period of time between the findOne() and the updateOne(). I suspect that the bug you have is not reproducible with the redacted code that you shared. I suspect that in your real code you have a lot of other things going up between the time you findOne() and the time you updateOne().

What happen is that the query part of updateOne() specifies doc, but the doc you fetched does not match the document in the database anymore because of the added the field 12. If you inspect the result of updateOne() you will see that matchedCount is 0. What you have is a concurrent write issue that you must determine how to resolve.

I am adding the field manually, and then i’m running the code, so the field is present before it reaches the findOne call.
When pasting the document, i forgot to refresh Mongodb compass, causing it to show the previous version, the object is foobarfoo and not foobar

By this

Do I understand correctly that randomprop already had the value foobarfoo when findOne() and updateOne() were called?

If this is the case then what you observe is normal behavior. A document will not be modified when it has the same value the you $set. By checking the return value of updateOne() you will see that matchedCount is 1 but updateCount is 0. Totally normal.

Dear @luwuna, I have spent time working on your issue. I would appreciate a followup or closure by marking one of the post as the solution.

Ad Thanks vance

1 Like

Sorry for the late response, i’ve been busy with uni stuff.
I currently dont appear to experience this issue anymore, i dont know whether this was because the keys were the same (thus the document indeed wont update, although i also did try adding a new field, which wouldnt update.)
As this is for a small project, and i’ve been using mongodb for a few years, i probably might have messed up something somewhere that prevents it from updating.
If the issue does reoccur, i’ll let you know.
Thanks for the help!

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.