I'm currently building a REST-Api with node.js express and can't figure out how to update / add elements to the scores Array.
Here is one document from my MongoDB collection (how it should look):
My mongoose model:
const challengesSchema = new mongoose.Schema({
createdBy:{
type:String,
required: true
},
mapType:{
type:String,
required: true
},
time:{
type:Number,
required: true
},
numberOfMaps:{
type:String,
required: true
},
maps:{
type:Array,
required: true
},
pin:{
type: String,
required: true
},
takenBy:{
type: Array,
required: false
}
})
Basically I receive an id, which I can use to do Challenges.findById({challenge._id}) .
I figured out how to add an object to the takenBy Array, like so:
Challenges.findOneAndUpdate(
{ _id: challenge._id },
{
$push: {
takenBy: user
}
}
);
How can I add an element (score, like "20") to the scores array in the array 'takenBy' ?
