1

我想将 MongoDBfind()方法生成的所有结果输出到divvia jQuery 的getJSON()方法中。

文档

{
   "_id": ObjectId("34576347347"),
   "content": "here is some content",
   "field2": {
     "subfield1": {
       "0": "sf1v0",
       "1": "sf1v1",
       "2": "sf1v2" 
    },
     "subfield2": "value 2" 
  },
   "field2": "value 3" 
}

PHP

<?php   

$dbhost = 'username:password@127.x.xx.x:27017/';  
$dbname = 'my_database';  
$m = new MongoClient("mongodb://$dbhost");  
$db = $m->$dbname;  

// specify the collection  
$collection = $db->myCollection;  

// define the query  
$query = $collection->find(array("field2.subfield2" => "value 2"));  

foreach($query as $result) {  
echo $result['field2'] . " - " . $result['content'] . "<br>";  
}  

?>  

直接访问 PHP 文件时,它会显示:

value 3 - here is some content // values from the first document
value 3 - here is some content // values from the second document

下面是我尝试div通过 jQuery 的getJSON()方法获得相同的输出。

jQuery

$.getJSON("http://path/to/file.php", {cid: href, format: 'json'}, function(results){  
$("#my_div").html(results);
});

我尝试过的其他事情

使用iterator_to_array()功能:

PHP

// define the query  
$query = $collection->find(array("field2.subfield2" => "value 2"));  
echo json_encode(iterator_to_array($query, false));

结果

div 的内容变为空 div 即:

<div id="my_div"></div>  

以下得到结果:

PHP

// define the query  
$query = $collection->find(array("field2.subfield2" => "value 2"));  
echo json_encode(iterator_to_array($query, false));

jQuery

$("#my_div").html(results[0].field2 + " - " + results[0].content + "<br>" + results[1].field2 + " - " + results[1].content);

但它不是一个可扩展/灵活的解决方案。

我在想我需要使用 jQuery.each().append()方法,以便:

  • 对于数组中的每个 Document
  • 将其内容附加到 div

但我不知道该怎么做,这就是我尝试过的:

$.getJSON("http://path/to/file.php", {cid: href, format: 'json'}, function(results){ 
$("#my_div").html("");
$.each(results, function(){ 
$("#my_div").append(results.field2 + " - " + results.content + "<br>");
});
});

它正在输出:

undefined - undefined
undefined - undefined
4

2 回答 2

3

解决方案

PHP

<?php   

$dbhost = 'username:password@127.x.xx.x:27017/';  
$dbname = 'my_database';  
$m = new MongoClient("mongodb://$dbhost");  
$db = $m->$dbname;  

// specify the collection  
$collection = $db->myCollection;  

// define the query  
$query = $collection->find(array("field2.subfield2" => "value 2"));  

//  add content type (otherwise firebug doesn't show a JSON tab and type is text/html)
header("Content-type: application/json");

// convert the cursor to an array
echo json_encode(iterator_to_array($query, false));

?> 

jQuery

$.getJSON("http://path/to/file.php", {cid: href, format: 'json'}, function(results){ 
$("#my_div").html("");
$.each(results, function(key,value){ 
$("#my_div").append(results.field2 + " - " + results.content + "<br>");
});
});

将显示内容my_div为:

value 3 - here is some content
value 3 - here is some content
于 2013-09-07T14:05:11.627 回答
1

首先,您必须从执行查询后创建的游标中提取所有结果文档。我在下面的示例中所做的是准备了一个数组来保存光标指向的所有文档。

拥有这一系列文档后,您真正需要做的就是将结果编码为 JSON 格式。为此,您可以使用该json_encode()功能

json_encode()- 返回值的 JSON 表示

这是一个示例用法:

$cursor = $collection->find( YOUR_QUERY );  
$response = [];
foreach ($cursor as $document) {
  $response[] = $document;
}

echo json_encode( $response );
于 2013-09-07T12:16:24.807 回答