I think you have a problem with your _id field.
I tried to recreate the bug on my end:
Insert a ticket:
db.tickets.insert({title:"title",content:"content",counterEdit:0})
WriteResult({ "nInserted" : 1 })
db.tickets.find({title:"title"})
{ "_id" : ObjectId("5d3ae9f36a5393ea092cd876"), "title" : "title", "content" : "content", "counterEdit" : 0 }
Update:
I tried updating using the string i received from the find result:
db.tickets.update(
{ _id: "5d3ae9f36a5393ea092cd876" },
{
$set: { title: "new title", content: "new content" },
$inc: { counterEdit: 1 }
}
)
WriteResult({ "nMatched" : 0, "nUpserted" : 0, "nModified" : 0 })
No luck, Mongo couldn’t find it. But if I use ObjectId instead:
db.tickets.update(
{ _id: ObjectId("5d3ae9f36a5393ea092cd876") },
{
$set: { title: "new title", content: "new content" },
$inc: { counterEdit: 1 }
}
)
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Something was changed! And indeed:
db.tickets.find({})
{ "_id" : ObjectId("5d3ae9f36a5393ea092cd876"), "title" : "new title", "content" : "new content", "counterEdit" : 1 }
The counter and the fields were updated correctly…your query has no problems…
I am guessing you are sending the _id field in the ticketId parameter from the client to the server.
I can’t remember where, but I read that this is dangerous because the _id field is generated by the minimongo on the client, and MongoDB on the server, meaning that you end up with _id’s that do not match!
I would advise to create a specific new field in the ticket collection for the ID and generate it manually, and never use the _id field for operations that go through the client-server communications.
To see if I am right, please put console.log(ticketId) before the call of the method on the client and check if it’s the same id on the database.