dqc18251 2014-12-11 09:42
浏览 78
已采纳

在MongoDB中为对象添加值

I have json object in MongoDB. I want to add a value to this object, for example in locations -> machines -> sensors. I know the _id for the machine, for the location and for the sensor. What is the most efficient way to do this?.

    array(
    "_id" => '547f2e4cb54b3a96203c9869',
    "name" => Array (
        "nl" => "klant1"
    ),
    "locations" => Array (
        Array (
            "_id" => "1",
            "name" => Array (
                "nl" => "location1",
            ),
            "machines" => Array (
                Array (
                    "_id" => "1",
                    "name" => Array (
                        "nl" => "boot1"
                    ),
                    "sensors" => Array (
                        Array (
                            "_id" => "1",
                            "log" => Array (
                            )
                        ),
                        Array (
                            "_id" => "2",
                            "log" => Array (
                            )
                        )
                    )
                ),
                Array (
                    "_id" => "2",
                    "name" => Array (
                        "nl" => "boot2"
                    ),
                    "sensors" => Array (
                        Array (
                            "_id" => "1",
                            "log" => Array (
                            )
                        ),
                        Array (
                            "_id" => "2",
                            "log" => Array (
                            )
                        )
                    )
                ), 
                Array (
                    "_id" => "3",
                    "name" => Array (
                        "nl" => "boot3"
                    ),
                    "sensors" => Array (
                        Array (
                            "_id" => "1",
                            "log" => Array (
                            )
                        ),
                        Array (
                            "_id" => "2",
                            "log" => Array (
                            )
                        )
                    )
                )
            )
        )
    )
  • 写回答

1条回答 默认 最新

  • doukekui0914 2014-12-16 00:27
    关注

    Super easy. You can either do a findAndModify call. Or if you want to do it a little more manually, you can do:

    $collection = $mongo->your_collection;
    $obj = $collection->findOne($query);
    $obj = $obj['locations']['machines']['sensors'][] = $new_sensor;
    $collection->save($obj);
    

    Mongo is smart enough to identify the object using _id key which causes it to treat these saves as updates to existing objects. Without it you'd be creating new objects with this.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 宇视监控服务器无法登录
  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥15 DruidDataSource一直closing
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据