I have a Schema as follows (simplied for this)
const userSchema = new Schema({
_id: { type: String, required: true }
data: [{
_id: { type: String, required: true }
score: { type: Number, default: 0 }
}]
});
I am struggling to know how to implement a query that checks if
- A document with
_id == id
exists - A subdocument in the
data
array with_id == dataId
And if these conditions are true, increment the score by 1
If the document isn’t present, create a new document with data.score: 1
, but if the subdocument isn’t present, I want to be able to add a new subdocument into the array and set the score to 1 so data.score: 1
also.
Thank you for any help.