MongoDB:使用 save() 更新集合中的现有文档

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4574056/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 11:54:12  来源:igfitidea点击:

MongoDB: Use save() to update an existing document in a collection

mongodbmongodb-php

提问by Roger

everybody. I love manga. And now I am learning to love mongo too :-)

大家。我爱漫画。现在我也在学习爱上 mongo :-)

There is an explanation of how to use save() to update an existing document in a collection in MongoDB PHP? but I couldn't apply it to the PHP's "reality" :-)

有关于如何使用 save() 更新 MongoDB PHP 集合中的现有文档的说明?但我无法将其应用于 PHP 的“现实”:-)

> var mongo = db.things.findOne({name:"mongo"});
> print(tojson(mongo));
{"_id" : "497dab624ee47b3a675d2d9c" , "name" : "mongo"}
> mongo.type = "database";
database
> db.things.save(mongo);
> db.things.findOne({name:"mongo"});
{"_id" : "497dab624ee47b3a675d2d9c" , "name" : "mongo" , "type" : "database"}

Here's my test code:

这是我的测试代码:

<?php
$a=array('_id'=>'test_a','field1'=>'anything');
$b=array('_id'=>'test_a','field2'=>'anything else');

$m=new Mongo();
$c=$m->db->test;
$c->save($a);
$c->save($b);//overwrites the previous record

/*
//With update() it works fine
$filter=array('_id'=>'test_a');
$update=array('$set'=>array('field2'=>'anything else'));
$c->update($filter,$update);
//$c->save($filter,$update);//Also tried...
*/

$f=$c->find();
echo $f->count()." found \n";
$i=iterator_to_array($f);//mongo cursos iterator
$m->close();//disconnect mongo

print_r($i);
?>

The fact is that in the PHP example save() overwrites the object while in the JS example it updates it. I wish I can reproduce in PHP the same as in JS.

事实是,在 PHP 示例中 save() 覆盖了对象,而在 JS 示例中它会更新它。我希望我可以像在 JS 中一样在 PHP 中重现。

Thanks in advance.

提前致谢。

回答by Justin Jenkins

These examples aren't doing the same thing ...

这些例子并没有做同样的事情......

In the JS examples you declared the object mongo

在 JS 示例中,您声明了对象mongo

> var mongo = db.things.findOne({name:"mongo"});

Then, you modified the sameobject to add type...

然后,您修改了同一个对象以添加类型...

> mongo.type = "database";

In the PHP example you declared TWO objects ...

在 PHP 示例中,您声明了两个对象......

$a=array('_id'=>'test_a','field1'=>'anything');
$b=array('_id'=>'test_a','field2'=>'anything else');

This would be like doing the following in the JS shell ...

这就像在 JS shell 中执行以下操作...

> var apples = db.things.findOne({name:"mongo"});
> var orange = db.things.findOne({type:"database"});

So applesis a different object/document from oranges... hence when you run save()and pass the NEW object it totally overwrites the old object.

所以apples是一个与橙子不同的对象/文档......因此,当您运行save()并传递 NEW 对象时,它会完全覆盖旧对象。

Your update()worked fine because you searched for the object ...

您的update()工作正常,因为您搜索了对象...

$filter=array('_id'=>'test_a');

Then ran the update on that object; passing the new field instead of passing a new objectto replace it ...

然后在该对象上运行更新;传递新字段而不是传递新对象来替换它......

$update=array('$set'=>array('field2'=>'anything else'));
$c->update($filter,$update);

In the JS examples you only had one objectso it basically appended it on the update instead of replacing it.

在 JS 示例中,您只有一个对象,因此它基本上将它附加到更新中而不是替换它。

Correct PHP Code

正确的 PHP 代码

The following PHP would replicate what you were doing in the JS shell ...

以下 PHP 将复制您在 JS shell 中所做的事情......

<?php

$connection = new Mongo();
$db = $connection->foo;
$collection = $db->testing;

// create new object & save
$a = array('color'=>'red');
$collection->save($a);

// add type = fruit to existing object & save
$a['type'] = 'fruit';
$collection->save($a);

// print out results.
echo "<pre>";
var_dump($collection->findOne(array("color" => "red")));
echo "</pre>";

?>

Note: I'd stronglyrecommend you don't set your own object _id's and instead let the driver do it for you as I did in the example above.

注意:我强烈建议您不要设置自己的对象_id,而是让驱动程序为您完成,就像我在上面的示例中所做的那样。