MongoDB -- 更新

时间:2023-03-09 18:09:40
MongoDB -- 更新

$pull:

db.collection.update(
<query>,
{
$pull: {
<arrayField>: <query2>
}
}
)

MongoDB -- 更新

$pullAll:

db.collection.update(
<query>,
{
$pullAll: {
<arrayField>: [ <value1>, <value2> ... ]
}
}
)

MongoDB -- 更新

$push:

db.collection.update(
<query>,
{
$push: {
<field>: <value>
}
}
)

将 value 压入 field 中。若是 value
本身也是数组,那么是将整个数组当做一个元素压入。不去重

MongoDB -- 更新







$pushAll:

db.collection.update(
{
field: value
},
{
$pushAll:
{
field1: [ value1, value2, value3 ]
}
}
);

若压入的 value 是单个值将会报错,必须是一个 array,不去重

MongoDB -- 更新

当 value 为一个 array 的时候,将 value
里面的值分别增加到 field 中。而不是当做一个总体

MongoDB -- 更新



$each 配合 $addToSet :

仅仅将不存于数组 field 中的多值增加到field中。去重

db.COLLECTION.update(
<query>,
{
$addToSet: {
<field>: {
$each: [ <value1>, <value2> ... ]
}
}
}
)

MongoDB -- 更新

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcGFuZG9yYV9tYWRhcmE=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">



$each 配合 $push:

将多值压入数组 field 中,不去重

db.COLLECTION.update(
<query>,
{
$push: {
<field>: {
$each: [ <value1>, <value2> ... ]
}
}
}
)

MongoDB -- 更新