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

发布于 2024-10-10 02:30:21 字数 1274 浏览 8 评论 0原文

大家。我喜欢漫画。现在我也学着喜欢 mongo :-)

有一个解释 如何使用 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"}

这是我的测试代码:

<?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);
?>

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

提前致谢。

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

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" :-)

> 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.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

傲影 2024-10-17 02:30:21

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

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

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

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

> mongo.type = "database";

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

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

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

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

因此 apples 是与 oranges 不同的对象/文档 ...因此,当您运行 save() 并传递新对象时,它会完全覆盖旧对象。

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

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

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

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

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

正确的 PHP 代码

以下 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>";

?>

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

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

In the JS examples you declared the object mongo

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

Then, you modified the same object to add type ...

> mongo.type = "database";

In the PHP example you declared TWO objects ...

$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 ...

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

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

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

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

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

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

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

Correct PHP Code

The following PHP would replicate what you were doing in the 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 strongly recommend 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文