我有这样的mongodb文件......
{
"semantics" : [
{
"text": "abc gave payment to xyz",
"action": "gave"
},
{
"text": "abc wanted quicker solution",
"action": "want"
}
],
"keyword" : [
{
"word":"payment",
"imp":0.91
},
{
"word":"solution",
"imp":0.7
}
]
}
要求是为那些重要性大于0.9的单词找到action
值。
在上述情况下,payment
的重要性超过0.9,因此应予以考虑。 payment
存在于其中一个数组中,action
中的gave
值为if(isset($_REQUEST['search'])){
$pro_price = $_REQUEST['pro_price'];
foreach ($_REQUEST['pro_price'] as $pro_price) {
$statearray[] = mysql_real_escape_string($pro_price);
}
$states = implode ("','", $statearray);
$sql = "SELECT * FROM addproduct WHERE pro_price IN ('$states'))";
$result = mysql_query($sql) or die(mysql_error());
if (mysql_num_rows($result) == 0)
{
echo "Sorry, but we can not find an entry to match your query...<br><br>";
}
else
{
echo "<table border='1' width='900' class='srchrslt'>
<tr class='head'>
<td>pro_name</td>
<td>pro_brand</td>
<td>hsn_code</td>
<td>pro_tax2</td>
<td>pro_tax3</td>
</tr>";
while($row = mysql_fetch_assoc( $result ))
{
echo "<tr>";
echo "<td>" . $row['pro_name'] . " </td>";
echo "<td>" . $row['pro_brand'] . " </td>";
echo "<td>" . $row['hsn_code'] . " </td>";
echo "<td>" . $row['pro_tax2'] . " </td>";
echo "<td>" . $row['pro_tax3'] . " </td>";
echo "</tr>";
}
echo "</table>";
}
。
我正在请求帮助构建同样的mongodb查询。
答案 0 :(得分:2)
您可以先使用mapReduce:
db.collection.mapReduce(
function() {
for (var i = 0; i < this.keyword.length; i++) {
if (this.keyword[i].imp >= 0.9) {
emit(this.keyword[i].word, this.semantics)
}
}
},
function(key, values) { },
{
out: {merge: 'result'},
finalize: function(key, semantics) {
var result;
for (var i = 0; i < semantics.length; i++) {
if (semantics[i].text.indexOf(key) != -1) {
result = {key: semantics[i].action};
}
}
return result;
}
)
我们不关心此处的reduce函数,因为map将返回{word with imp >= 0.9: the whole semantics}
。
稍后,在将结果存储到result
集合之前,调用finialize函数,它会遍历与单词关联的所有语义,并获取与包含关键字的文本相关的所有操作。
在此之后,您可以db.result.find()
查看结果,您会看到一些空结果,因为并非所有键都有匹配的文本和操作,您需要清理一下。