Mongo DB中保存和插入有什么区别?

use*_*576 142 mongodb

Mongo DB中保存和插入有什么区别?两者看起来都一样

db.users.save({username:"google",password:"google123"})

db.users.insert({username:"google",password:"google123"})
Run Code Online (Sandbox Code Playgroud)

Rah*_*hul 139

保存与插入:

在您给出的示例中,行为基本相同.

save 如果使用"_id"参数传递,则行为会有所不同.

对于保存,如果文档包含_id,它将在_id字段上查询集合,如果不包含,则将插入.

如果文档不存在具有指定的_id值,则save()方法将使用文档中的指定字段执行插入.

如果存在具有指定_id值的文档,则save()方法将执行更新,将现有记录中的所有字段替换为文档中的字段.


保存与更新:

update修改与查询参数匹配的现有文档.如果没有这样的匹配文件,upsert就会出现这种情况.

  • upsert : false :没有这样的文件时没有任何事情发生
  • upsert : true :创建新文档,内容等于查询参数和更新参数

save:不允许任何查询参数.如果_id存在且存在匹配的doc _id,则替换它.如果没有_id指定/没有匹配的文档,它会将文档作为新文档插入.

  • 使用upsert保存vs更新怎么样?是的? (15认同)
  • 两者都有不同的语法.Update需要多个参数({condition},{update to doc},upsert,multi)而save只接受一个参数(_id是条件参数的参数).update可以接受任何条件但是save只有条件限制_id字段. (8认同)

squ*_*oid 70

让我们考虑这两个案例进行保存: -

1)在doc中有_id.

2)在doc中没有_id.

                        Save ()
                        /     \
                       /       \

                 Having _id     Not Having _id 

  ->In this case save will do    ->  It will do normal insertion 
    upsert to insert.Now             in this case as insert() do.
    what that means, it means 
    take the document and replace 
    the complete document having same
    _id.
Run Code Online (Sandbox Code Playgroud)

让我们在这里考虑插入的两个案例: -

1)在收集中有_id的doc.

2)没有收集doc的_id.

                        Insert()
                       /        \
                      /          \

   Doc Having _id in collection    Doc Not Having _id 
  ->  E11000 duplicate key     ->Insert a new doc inside the collection.
      error index:       
Run Code Online (Sandbox Code Playgroud)

  • 下一级图 (9认同)
  • 那里有gg图 (3认同)
  • 赞成整齐地绘制和呈现图表所花费的时间. (2认同)
  • [注意](https://docs.mongodb.com/v4.4/reference/method/db.collection.save/):从 MongoDB 4.2 开始,`db.collection.save()` 方法已被弃用。使用 [`db.collection.insertOne()`](https://docs.mongodb.com/v4.4/reference/method/db.collection.insertOne/#mongodb-method-db.collection.insertOne) 或 [相反,`db.collection.replaceOne()`](https://docs.mongodb.com/v4.4/reference/method/db.collection.replaceOne/#mongodb-method-db.collection.replaceOne)。 (2认同)

Alp*_*haB 36

save 插入或更新文档.

insert 只插入一次.

但在你的情况下,它会做同样的事情,因为save中提供的文件没有_id字段.


Abh*_*bhi 13

举个例子

拯救一个苹果

db.fruit.save({"name":"apple", "color":"red","shape":"round"})
WriteResult({ "nInserted" : 1 })

db.fruit.find();

{
    "_id" : ObjectId("53fa1809132c1f084b005cd0"),
    "color" : "red",
    "shape" : "round",
    "name" : "apple"
}
Run Code Online (Sandbox Code Playgroud)

使用以前保存的苹果的_id保存苹果

db.fruit.save(
{"_id" : ObjectId("53fa1809132c1f084b005cd0"),"name":"apple", 
"color":"real red","shape":"round"})

WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Run Code Online (Sandbox Code Playgroud)

现在我们保存的苹果颜色从红色变为红色

db.fruit.find();
{
    "_id" : ObjectId("53fa1809132c1f084b005cd0"),
    "color" : "real red",
    "shape" : "round",
    "name" : "apple"
}
Run Code Online (Sandbox Code Playgroud)

用_id保存一个苹果

db.fruit.save({"_id" : ObjectId("55551809132c1f084b005cd0"),
"name":"apple", "color":"real red","shape":"round"})

    WriteResult({ "nMatched" : 0, "nUpserted" : 1, 
"nModified" : 0, "_id": 55551809132c1f084b005cd0 })
Run Code Online (Sandbox Code Playgroud)

Apple已插入,因为没有具有相同Object ID的苹果进行更新

插入橙色

db.fruit.insert({"name":"orange", "color":"orange","shape":"round"})
WriteResult({ "nInserted" : 1 })
Run Code Online (Sandbox Code Playgroud)

插入橙色

db.fruit.find();
{
    "_id" : ObjectId("53fa1809132c1f084b005cd0"),
    "color" : "real red",
    "shape" : "round",
    "name" : "apple"
}
{
    "_id" : ObjectId("53fa196d132c1f084b005cd7"),
    "color" : "orange",
    "shape" : "round",
    "name" : "orange"
}
{
    "_id" : ObjectId("55551809132c1f084b005cd0"),
    "color" : "real red",
    "shape" : "round",
    "name" : "apple"
}
Run Code Online (Sandbox Code Playgroud)

因此,如果提供了对象id,则save将充当更新,前提是对象id已经存在,否则它会执行插入操作.


Rog*_*art 10

如果您尝试将"插入"与之前在同一集合中使用的ID一起使用,则会出现重复键错误.如果对已存在于同一集合中的ID使用"save",则会更新/覆盖它.

如果您希望进行真正的更新,我建议使用"更新".如果您使用集合中已有的相同ID进行保存,则Update将不会以Save方式覆盖.

例如,您有两个字段"x"和"y",并且您希望保留两个字段,但更改"x"的值.如果您选择"保存"命令并且未在前一个值中包含y或者在保存中没有y,那么y将不再具有相同的值或者在那里.但是,如果您选择使用$ set进行更新并且只在更新语句中包含x,则不会影响y.


Ada*_*ord 6

正如您在此处所看到的,save方法基本上会执行upsert(如果找到doc则更新,否则插入):

http://docs.mongodb.org/manual/reference/method/db.collection.save/#db.collection.save

插入只是一个直插入.