如何在 MongoDB 中插入数组值(php)? [英] How to insert array values(php) in MongoDB?

查看:88
本文介绍了如何在 MongoDB 中插入数组值(php)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 MongoDB 内容(当我使用 find() 时)是这样的:

My contents of MongoDB(when i used find()) is like this:

{
  "_id": ObjectId("50072b17b4a6de3b11000001"),
  "addresses": [
    {
      "_id": ObjectId("50072b17b4a6de3b11000004"),
      "address1": "770 27th Ave",
      "address2": null,
      "city": "San Mateo",
      "country": "United States",
      "country_code": "US",
      "name": "home",
      "primary": true,
      "state": "California",
      "zip": "94403"
    }
  ],
  "biography": null,
  "category_ids": [],
  "department": null,
  "emails": [
    {
      "_id": ObjectId("50072b17b4a6de3b11000003"),
      "_type": "Email",
      "name": "work",
      "email": "alan@altimetergroup.com"
    }
  ]
}

我需要做的是用额外的数据更新这个 MongoDB.为此,我需要将 php 中的值作为数组获取并将该数组插入到此集合中.如何在php中以数组的形式获取这些指定字段的值以及如何在php中插入这些值??

What I need to do is I need to update this MongoDB with additional datas's. For that I need to get the values in php as an array and insert that array in to this collection. How to get the values for these specified fields as an array in php and how to insert those in php??

推荐答案

对于插入:

$id = new MongoId();
$test->insert(array('t' => 'test', 'email' => array('_id' => $id, 'email' => 'ccc@ccc.com')));

结果:

{ "_id" : ObjectId("5088cba86527044a31000001"), "t" : "test", "email" : { "_id" : ObjectId("5088cba86527044a31000000"), "email" : "ccc@ccc.com" } }

它有效:)

这篇关于如何在 MongoDB 中插入数组值(php)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆