2012-04-17 08:40
浏览 106

PHP + MongoDB:尝试将多维数组插入MongoDB时出错

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:

  'author' => 
      'name' => string 'Shaine Coffey' (length=13)
      'email' => string '' (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' => 
      0 => 
          'name' => string 'Finn Hale' (length=9)
          'email' => string '' (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 => 
          'name' => string 'Axel Hall' (length=9)
          'email' => string '' (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 => 
          'name' => string 'Laith Rowland' (length=13)
          'email' => string '' (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' => 
      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) {

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.

- Mestika

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • duanfen2349 2012-04-17 10:35

    It looks very much as though you want to do


    instead of:

    foreach ($postas $id => $item) {

    You don't say what variable your var_dump shows. Is it of $post or is it of $item?

    解决 无用
    打赏 举报
  • dqppv86022 2012-04-17 09:01

    Before you call $collection->insert($item);, do a var_dump( $item ). That should tell you exactly what you are trying to insert into the collection. You'll most likely find that it's not an array or object.

    解决 无用
    打赏 举报

相关推荐 更多相似问题