I have a User Schema that looks like this:
reputation: [
{
voter_id: {
type: String,
required: true,
},
},
{
score: {
type: Number,
required: true,
},
},
{
comment: {
type: String,
maxlength: 100,
},
},
{
voted_at: {
type: Date,
default: Date.now,
},
},
],
Whenever I try to push an object into this array like this
User.updateOne(
{ id: user.id },
{
$push: {
reputation: {
voter_id: interaction.user.id,
score: rating,
comment,
},
},
},
);
Only the first property of the object to push, here voter_id is set in the database, even though the other values are not undefined.
The object stored in the database looks like this:
{ "_id" : ObjectId("61478043630ef626f50c9c2b"), "reputation" : [ { "voter_id" : "214772155114717184", "_id" : ObjectId("61478060630ef626f50c9c7c") } ], "__v" : 0 }
The properties are missing. For this example score was 1 and comment "test". Whats the problem here?
The array notation is what tricks into making the items within separate, should be a single object:
reputation: [
{
voter_id: {
type: String,
required: true,
},
score: {
type: Number,
required: true,
},
comment: {
type: String,
maxlength: 100,
},
voted_at: {
type: Date,
default: Date.now,
},
}
]