1
我喜欢将一个数组前置到一个数组。 为了追加,我这样写。使用Mongoose预先安排数组的正确方法?
Foo.update({ _id: id }, { $push: { bars: { $each: bars } } }, {upsert:true}, function(err){
});
是否有适当的方法来预先设定?
我喜欢将一个数组前置到一个数组。 为了追加,我这样写。使用Mongoose预先安排数组的正确方法?
Foo.update({ _id: id }, { $push: { bars: { $each: bars } } }, {upsert:true}, function(err){
});
是否有适当的方法来预先设定?
是的,你可以使用position: 0
db.Foo.update(
{ _id: id },
{
$push: {
bars: {
$each: bars,
$position: 0
}
}
}
)
看文档:https://docs.mongodb.org/manual/reference/operator/update/position/