PHP 中的 MongoDB - 如何将项目插入集合中的数组中?

发布于 2024-10-25 02:01:41 字数 641 浏览 3 评论 0原文

这必须很简单,但我似乎无法弄清楚...假设我有一个集合users,这是集合中的第一项:

{ 
    "_id" : ObjectId("4d8653c027d02a6437bc89ca"), 
    "name" : "Oscar Godson", 
    "email" : "[email protected]", 
    "password" : "xxxxxx", 
    "tasks" : [
    {
        "task" : "pick up stuff",
        "comment" : "the one stuff over there"
    },
    {
        "task" : "do more stuff",
        "comment" : "lots and lots of stuff"
    }
] }

How with the PHP driver for MongoDB然后我会去将另一个“任务”添加到集合中此项的“任务”数组中吗

This has to be easy, but i can't seem to figure it out... Let say i had a collection users and this is the first item in the collection:

{ 
    "_id" : ObjectId("4d8653c027d02a6437bc89ca"), 
    "name" : "Oscar Godson", 
    "email" : "[email protected]", 
    "password" : "xxxxxx", 
    "tasks" : [
    {
        "task" : "pick up stuff",
        "comment" : "the one stuff over there"
    },
    {
        "task" : "do more stuff",
        "comment" : "lots and lots of stuff"
    }
] }

How with the PHP driver for MongoDB would I then go and add another "task" to the "tasks" array in this item in a collection

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

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

发布评论

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

评论(1

东风软 2024-11-01 02:01:41

使用Mongo的$push操作:

$new_task = array(
  "task" => "do even more stuff",
  "comment" => "this is the new task added"
);
$collection->update(
  array("_id" => ObjectId("4d8653c027d02a6437bc89ca")), 
  array('$push' => array("tasks" => $new_task))
);

Use Mongo's $push operation:

$new_task = array(
  "task" => "do even more stuff",
  "comment" => "this is the new task added"
);
$collection->update(
  array("_id" => ObjectId("4d8653c027d02a6437bc89ca")), 
  array('$push' => array("tasks" => $new_task))
);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文