dongzhiyong8577 2012-04-17 08:40
浏览 111
已采纳

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:

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

  • 写回答

2条回答 默认 最新

  • duanfen2349 2012-04-17 10:35
    关注

    It looks very much as though you want to do

    $collection->insert($post);
    

    instead of:

    foreach ($postas $id => $item) {
       $collection->insert($item);
    }
    

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?