我正在尝试在MongoDB 3.2中使用聚合管道实现文本搜索这就是我的mongo查询的样子:
db.revws.aggregate(
[
{ $match: { $text: { $search: "terrible" } } },
{ $sort: { score: { $meta: "textScore" } } },
{ $project: {score: { $meta: "textScore" },'ProductInfo.ProductID' :1 , 'Reviews.Title': 1, 'Reviews.Content': 1, 'Reviews.Overall': 1,'Reviews.Author': 1, _id: 0 } }
]
)
这在我的收藏中运行良好。生成的文档结构如下所示:
{
"Reviews" : {
"Title" : "Terrible terrible terrible",
"Author" : "C. Leinart",
"Overall" : "1.0",
"Content" : "I love cameras - i have multiple cameras for multiple uses and this is the absolute worst ever. The quality of the prints are terrible. The paper turns sort of a bluish color. The battery life is terrible and to top it all off, i lost my charger and there is no such thing as a replacement charger in Polaroid-land. Really...you CANNOT buy a replacement charger. So if you have one of these, hang on to the charger and be prepared to only use for fun party pics - it's novel but not worth the $$."
},
"ProductInfo" : {
"ProductID" : "B005O08KH6"
},
"score" : 2.53571428571429
}
当我尝试根据用户在PHP中的输入执行类似的文本搜索查询时。我收到一个错误"$project规范必须是一个对象"。
这是我的PHP代码:
<html>
<body>
<h3>MongoDB Test - Aggregation pipeline</h3>
<h2>Text search - Amazon reviews</h2>
<br>
<form method="post">
search: <input type="text" name="term"><br><br>
<input type="submit" value="Display results">
</form>
</body>
</html>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$term = $_REQUEST['term'];
// connect to mongodb : default is localhost:27017
$m = new MongoClient();
echo "Connection to database successfully"."</br>";
$db = $m->selectDB('test');
$collection = new MongoCollection($db, 'revws');
/*
db.revws.aggregate(
[
{ $match: { $text: { $search: "terrible" } } },
{ $sort: { score: { $meta: "textScore" } } },
{ $project: {score: { $meta: "textScore" },'ProductInfo.ProductID' :1 , 'Reviews.Title': 1, 'Reviews.Content': 1, 'Reviews.Overall': 1,'Reviews.Author': 1, _id: 0 } }
]
)
*
*/
$pipeline = array (
array(
'$match' => array('$text' => array('$search'=> $term))
),
array('$sort' => array("score" => array('$meta' => "textScore"))
),
array('$project' => array( array("score" => array('$meta' => "textScore"), 'ProductInfo.ProductID' => 1,
'Reviews.Title' => 1, 'Reviews.Content' => 1 , 'Reviews.Overall' => 1, 'Reviews.Author' => 1, '_id' => 0
)
)
)
);
var_dump($pipeline);
$cursor = $collection->aggregate($pipeline);
echo "<br><br>";
echo '<table border="1"><tr><td>text score</td><td>ProductID</td><td>Review Title</td><td>Content</td><td>Author</td><td>Rating</td></tr>';
foreach ($cursor as $doc) {
echo '<tr>';
echo
'<td>'.$doc['score'].'</td>'.
'<td>'.$doc['ProductInfo']['ProductID'].'</td>'.
'<td>'.$doc['Reviews']['Title'].'</td>'.
'<td>'.$doc['Reviews']['Content'].'</td>'.
'<td>'.$doc['Reviews']['Author'].'</td>'.
'<td>'.$doc['Reviews']['Overall'].'</td>';
echo '</tr>';
}
echo '</table>';
}
?>
这个php的输出是:
连接到数据库成功数组(3){[0]=
我故意var_dump显示基于mongo查询的php。有人能在这里指出错误吗?谢谢
你试过吗
$pipeline = array (
array(
'$match' => array('$text' => array('$search'=> $term))
),
array('$sort' => array("score" => array('$meta' => "textScore"))
),
array('$project' => array("score" => array('$meta' => "textScore"), 'ProductInfo.ProductID' => 1,
'Reviews.Title' => 1, 'Reviews.Content' => 1 , 'Reviews.Overall' => 1, 'Reviews.Author' => 1, '_id' => 0
)
)
);
我刚刚从$project中删除了一个额外的数组