简体   繁体   中英

How to store object in Mongo DB as value?

Now, when I add values to Mongodb using PHP it stores as array:

{ "_id" : ObjectId("591e9b60470e6c500f3c9869"),"value" : [ "mama", "papa" ] }

How to store data for value as object: {"mama" : 1, "papa" : 2} ?

I tried:

$data = array("value" => array("mama" => 1, "papa" => 2))

$this->collection->insert($data);

It is inserted as array!

Try using these lines -

$data = (object) ["value" => (object) ["mama" => 1, "papa" => 2]]

$this->collection->insert($data);

It'll create object with mentioned properties.

 db.products.insert(
    [
      { _id: 11, item: "pencil", qty: 50, type: "no.2" },
      { item: "pen", qty: 20 },
      { item: "eraser", qty: 25 }
    ]
 )

 or
 {
    "_id" : ObjectId("591c382a82956dc8a500002e"),
     "name" : "alpesh",
     "email" : "admin@gmail.com",
     "contact_number" : "0123456789",
     "position" : "devloper",
     "updated_at" : ISODate("2017-05-17T11:46:50.000Z"),
     "created_at" : ISODate("2017-05-17T11:46:50.000Z")
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM