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 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥200 关于#c++#的问题,请各位专家解答!网站的邀请码
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号