I’m working on a test-application for my master thesis where I’m trying to create a very simple blog to show some of the advantages, disadvantages, features, and so on about using MongoDB.
I’ve created a small random post generator, which should produce an array with a post and everything it should contain for a blog post document inside MongoDB.
My array looks like this:
array
'author' =>
array
'name' => string 'Shaine Coffey' (length=13)
'email' => string 'Lars_Cherry@.com' (length=16)
'post' => string 'Vivamus non lorem vitae odio sagittis semper. Nam tempor diam natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Aenean eget magna. Suspendisse tristique neque venenatis lacus. Etiam bibendum fermentum metus. Aenean sed pede nec ante blandit viverra. Donec tempus, lorem fringilla ornare placerat, orci lacus vestibulum lorem, sit amet ultricies sem magna nec quam. Curabitur vel lectus. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec dignissi'... (length=570)
'date' => string 'Wed Mar 28 02:52:23 +0000 2007' (length=30)
'rating' => int 0
'comments' =>
array
0 =>
array
'name' => string 'Finn Hale' (length=9)
'email' => string 'Magee_Huffman@.net' (length=18)
'upVotes' => int 52
'downVotes' => int 88
'comment' => string 'augue malesuada malesuada. Integer id magna et ipsum cursus vestibulum. ligula. Aenean gravida nunc sed pede. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Proin vel arcu eu odio tristique pharetra. Quisque ac libero nec ligula consectetuer rhoncus. Nullam velit dui, semper et, lacinia vitae, sodales at, velit. Pellentesque ultricies dignissim lacus. Aliquam rutrum lorem ac risus. Morbi metus. Vivamus euismod urna. Nullam lobortis quam' (length=479)
1 =>
array
'name' => string 'Axel Hall' (length=9)
'email' => string 'Phillip_Simmons@.org' (length=20)
'upVotes' => int 99
'downVotes' => int 31
'comment' => string 'mauris, aliquam eu, accumsan sed, facilisis vitae, orci. Phasellus dapibus ut, pellentesque eget, dictum placerat, augue. Sed molestie. Sed id risus quis diam luctus lobortis. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos hymenaeos. Mauris ut quam vel sapien imperdiet ornare. In faucibus. Morbi vehicula. Pellentesque tincidunt tempus risus. Donec egestas. Duis ac arcu. Nunc mauris. Morbi non sapien molestie orci tincidunt adipiscing. Mauris molestie pharetra nibh. Aliquam '... (length=554)
2 =>
array
'name' => string 'Laith Rowland' (length=13)
'email' => string 'Graham_Nicholson@.net' (length=21)
'upVotes' => int 15
'downVotes' => int 73
'comment' => string 'iaculis enim, sit amet ornare lectus justo eu arcu. Morbi Suspendisse eleifend. Cras sed leo. Cras vehicula aliquet libero. Integer in magna. Phasellus dolor elit, pellentesque a, facilisis non, bibendum sed, est. Nunc laoreet lectus quis massa. Mauris vestibulum, neque sed dictum eleifend, nunc risus varius orci, in consequat enim diam vel arcu. Curabitur ut odio vel est tempor bibendum. Donec felis orci,' (length=409)
'tags' =>
array
0 => string 'War' (length=3)
1 => string 'Freedom' (length=7)
Everything is random generated and for example the number of comments can vary and the same with the number of tags.
When I try to run the insert code into MongoDB I get the error Warning: MongoCollection::insert() expects parameter 1 to be an array or object
My Insert code looks like this:
for($i=0; $i<$n; $i++) {
$post = BlogPosts();
foreach ($postas $id => $item) {
$collection->insert($item);
}
}
I’ve of cause entered the MongoDB database and collection, like so (at the top of my PHP file):
$mongo = new Mongo();
$db = $mongo->insert_test;
$collection = $db->testing;
I’m suspecting that my error lies in how I create my array, but I can’t quite seem to find where the error is. When I’m encoding it into JSON, it’s validated correctly.
Another thing is, that when I look inside my MongoDB database “insert_test” inside the collection “testing” I get a lot of entries (documents), but they are “split” up, so for example the “tags” are in one document, the “comments” are in another, and so on.
Any help or suggestions to what my error is would be very appreciated.
Thanks
Sincere
- Mestika