MongoDB未添加记录


MongoDB not adding records

我已经花了很长时间试图解决这个问题,但我似乎找不到问题所在。我正在尝试将php中的一条记录添加到我的mongo DB中(它已经设置好了,我已经测试了样本代码以确保它能工作)

添加记录的代码如下:

// add a record
$obj = array( "title" => "Calvin and Hobbes", "author" => "Bill Watterson" );
$collection->insert($obj);

我添加记录的代码:

$obj = array($currentarray[0][$currentCol] => $currentarray[$currentRow][$currentCol]);
$collection->insert($obj);

如果我打印对象:

Array ( [President ] => George Washington ) Array ( [Wikipedia Entry] => http://en.wikipedia.org/wiki/George_Washington ) Array ( [Took office ] => 30/04/1789 ) Array ( [Left office ] => 4/03/1797 ) Array ( [Party ] => Independent ) Array ( [Portrait] => GeorgeWashington.jpg ) Array ( [Thumbnail] => thmb_GeorgeWashington.jpg ) Array ( [Home State] => Virginia )

但是,如果我搜索"肖像",我会得到一个错误:

DB Notice: Undefined index: Portrait in /home/me/public_html/yes.php on line 74

额外代码:

// connect
$m = new Mongo();
// select a database
$db = $m->data;
// select a collection (analogous to a relational database's table)
$collection = $db->test;
do{
while(comparison statement){
$obj = array($currentarray[0][$currentCol] => $currentarray[$currentRow][$currentCol]);
$collection->insert($obj);
}
} while (comparison statement);
// find everything in the collection
$cursor = $collection->find();
// iterate through the results
foreach ($cursor as $obj) {
    echo $obj["Party "] . "'n";
}

所以,我已经解决了我的问题(至少目前)。我的记录正在添加到我的数据库中(我看到了上一个问题的快速print_r($obj))。所以现在,我唯一的问题是正确地检索它们,因为这就是我的错误的来源。

更新:当我试图检索它们时,我遇到了错误,因为我试图在我的所有阵列中"肖像"。这就是为什么从技术上讲,我从检索语句中得到了大约7个错误和1个实际答案。