0
votes

I can't get the document fields updated.

In my React JS project I am using this updateOne method

update = {
     // description: this.state.name,
     // barcode: this.state.price,
     // brand: this.state.brand,
     // category: obj,
     // subcategory: this.state.subCategory,
     // brand: this.state.brand,
     size: this.state.size,
     // fitmybike: this.state.fitmybike,
     // images: this.state.images,
     // itemdescription: this.state.itemDescription,
     // size: this.state.size,
}

this.state.client.auth.loginWithCredential(new AnonymousCredential()).then((user) => {
     var _id = this.state._id
     this.state.db.collection('products').updateOne({"_id": _id}, {$set: update}).then((result)=>{
          console.log('Updated: ')
          console.log(result)
     })
     .catch((e)=>console.log(e))
})

My _id is the hex string of BSON.ObjectID and I commented out all fields except the size field that is the string. I made sure size field in my schema is the string and that I can update it as a client.

My log throws:

Updated: 
editModal.js:150 {matchedCount: 0, modifiedCount: 0, upsertedId: undefined}

What I understand is that it the _id didn't match the one in database, but when I check my database and compare both _id's they are exactly the same. Also after this method I see that in the document that was suppose to be udpated, one field appeared: _id__stitch_transaction:ObjectId(5ebbe7a994f9ce9c20fe5c6d)

According to MongoDB Stitch documentation:

While running update operations, Stitch temporarily adds a reserved field, _id_stitch_transaction, to documents. Once a document is successfully updated, Stitch removes this field.

So I'm not sure if I understand this correctly, but does it mean the documents waits to be updated? I'm a bit confused with this.

1

1 Answers

0
votes

Alright. I found the problem. I didn't know that mongo finds documents based on BSON.ObjectID and not hexString.

this.state.client.auth.loginWithCredential(new AnonymousCredential()).then((user) => {
            var _id = this.state._id
                this.state.db.collection('products').updateOne({_id: new BSON.ObjectID(_id)}, {$set: update}).then((result)=>{
                    console.log('Updated: ')
                    console.log(result)
                })
                .catch((e)=>console.log(e))
        })

This works now