I have an array looks like ['N300W150727', '123test123', '123test1234']
I want push it into array mongoDB
I used $push
it adds array inside array
async updateSn(updateSn: UpdateSN) {
const { id, bindedSn } = updateSn;
return await this.userModel.updateOne(
{ id: id },
{
$push: {
bindedSn: bindedSn,
},
},
);
}
Result
bindedSn
:
Array
0
:
"123test123"
1
:
"123test1234"
2
:
Array
my questions are :
1 - How to spread an array inside in mongoDB I used the spread operator nothing happen
async updateSn(updateSn: UpdateSN) {
const { id, bindedSn } = updateSn;
return await this.userModel.updateOne(
{ id: id },
{
$push: {
bindedSn: [...bindedSn],
},
},
);
}
2 - How can I send item of the array item by item to the service
CodePudding user response:
I guess what you want to do is to combine $push and $each
userModel.updateOne(
{ id: id },
{ $push: { bindedSn: { $each: bindedSn } } }
)
More from docs here