Json data insert with PHP-MongoDB

661 views
Skip to first unread message

sanjay mangalaram

unread,
Mar 12, 2015, 9:29:14 AM3/12/15
to mongo...@googlegroups.com
Hello Team,

I have checked mongodb commands which insert the data in json format. that's works when we try this using terminal 
db.adddata.insert(
   {
     item: "BE10",
     details: { model: "14Q2", manufacturer: "XYZ Company"}, 
     stock: [ { size: "L", qty: 5}],  
     category: "clothing"
});

While I am doing the same using PHP code to insert json format data it gives error like 

Warning:  MongoCollection::insert(): expects parameter 1 to be an array or object, string given in

Anyone have a suggestion for this. Help would be appreciable.

Thank you

Derick Rethans

unread,
Mar 12, 2015, 10:12:59 AM3/12/15
to mongo...@googlegroups.com
On Thu, 12 Mar 2015, sanjay mangalaram wrote:

> I have checked mongodb commands which insert the data in json format.
> that's works when we try this using terminal:

> db.adddata.insert(
>
> {
> item: "BE10",
> details: { model: "14Q2", manufacturer: "XYZ Company"},
> stock: [ { size: "L", qty: 5}],
> category: "clothing"
>
> });
>
> While I am doing the same using PHP code to insert json format data it gives error like
> *Warning*: MongoCollection::insert(): expects parameter 1 to be an array or object, string given in

This list is for the development *of* MongoDB, not for people using
MongoDB. Please use the mongod...@googlegroups.com group for future
questions.

In any case, you need to use the language's native data formats
for storing data into MongoDB.

In PHP, that would be like:

$db->colname->insert( [
'item' => 'BE10',
'details' => [ 'model' => '14q2', 'manufacturer' => 'XYZ Company' ],
'stock' => [ [ 'size' => 'L', 'qty' => 5 ] ],
'category' => 'clothing'
] );

Which probably means, you might need to convert your JSON string to PHP
data types, with the json_decode() function.

cheers,
Derick
Reply all
Reply to author
Forward
0 new messages