2013-07-18 62 views
2

我想感谢所有帮助过我的人以及您的时间阅读本文。MySQL到Mongodb查询问题

我有一个将我的sql查询转换为mongodb查询的问题。

SQL查询:

SELECT m_time,m_latency FROM pkt_tbl WHERE (m_time-m_latency+" . LATENCY_DELTA . ")>=" . $rangeXFrom . " AND (m_time-m_latency+" . LATENCY_DELTA . ")<=" . $rangeXTo . " ORDER BY (m_time-m_latency+" . LATENCY_DELTA . ")" 

MongoDB的查询(我试试)

$latency_delta_split = array('m_time-m_latency'=>LATENCY_DELTA); 
$find_query = array($latency_delta_split=>array('$gte'=>$rangeXFrom),$latency_delta_split=>array('$lte'=>$rangeXTo)); 
$find_projectins = array('m_time'=>1, 'm_latency'=>1); $find_query = array(('m_time-m_latency'=>LATENCY_DELTA)=>array('$gte'=>$rangeXFrom),('m_time-m_latency'=>LATENCY_DELTA)=>array('$lte'=>$rangeXTo)); 

我仍然得到一个错误,无法真正找出解决方案。

我做了什么:

1] Read Mongodb Docs. 
2] Searched on StackOverflow for similar problems. (None addressed mine) 
3] Refered to PHP.NET official docs. 

但是,我无法弄清楚,帮助将不胜感激。

+0

你会得到什么错误? –

+0

查询不运行。我很确定查询中有错误。 (我没有做ORDER BY,但是我写的查询中有错误,这是肯定的。如果有人能帮我指出那会很棒。 –

+0

是的,但是你遇到了什么具体的错误。 Mondo和你没有得到任何结果,你是否看着你正在得到什么错误(即比“它不工作”更具体)? –

回答

7

在你的SQL中你有:m_time-m_latency,我认为这意味着从另一列中减去一列。

简短的回答:

这是不是你可以使用MongoDB的代表查询还因为你只能比较静态值的字段。

长的答案:

如果你想查找文档,其中m_time - m_latency + LATENCY_DELTA是在特定的范围内,那么你将不得不存储值预先计算的文档的另一场。如果你这样做,那么你就可以简单地运行与查询:

db.collection.find({ 'm_calculated_latency' : { '$gte' : FROM_RANGE, '$lte' : TO_RANGE } }); 

或者在PHP中:

$collection->find(array(
    'm_calculated_latency' => array(
     '$gte' => $from_range, 
     '$lte' => $to_range, 
    ) 
) 

解决方法:

随着MongoDB的聚合框架,你也许可以做到像你想要的查询,但它将不会是最快或最优雅的解决方案,也不会使用索引。所以重新设计您的模式并添加预先计算的字段。

与警告的方式进行,这里有云:

FROM=3 
TO=5 
DELTA=1 
db.so.aggregate([ 
    { $project: { 
     'time': { $add: [ 
      { $subtract: [ '$m_time', '$m_latency' ] }, 
      DELTA 
     ] }, 
     'm_time' : 1, 
     'm_latency' : 1 
    } }, 
    { $match: { 'time' : { $gte: FROM, $lte: TO } } }, 
    { $sort: { 'time' : 1 } } 
]); 

$项目一步,我们计算场时间m_time - m_latency + DELTA。我们还输出原始的m_timem_latency字段。然后在$匹配步骤中,我们将计算的timeFROMTO进行比较。最后我们按计算的time排序。 (因为你的原始SQL排序也没有意义,我假设你打算按时差排序)。

用我的输入数据:

> db.so.insert({ m_time: 5, m_latency: 3 }); 
> db.so.insert({ m_time: 5, m_latency: 1 }); 
> db.so.insert({ m_time: 8, m_latency: 1 }); 
> db.so.insert({ m_time: 8, m_latency: 3 }); 
> db.so.insert({ m_time: 7, m_latency: 2 }); 
> db.so.insert({ m_time: 7, m_latency: 4 }); 
> db.so.insert({ m_time: 7, m_latency: 6 }); 
> FROM=3 
> TO=5 
> DELTA=1 

这将产生:

{ 
    "result" : [ 
     { 
      "_id" : ObjectId("51e7988af4f32a33dac184e8"), 
      "m_time" : 5, 
      "m_latency" : 3, 
      "time" : 3 
     }, 
     { 
      "_id" : ObjectId("51e7989af4f32a33dac184ed"), 
      "m_time" : 7, 
      "m_latency" : 4, 
      "time" : 4 
     }, 
     { 
      "_id" : ObjectId("51e7988cf4f32a33dac184e9"), 
      "m_time" : 5, 
      "m_latency" : 1, 
      "time" : 5 
     } 
    ], 
    "ok" : 1 
} 

现在最后绝招是写从上面的PHP语法的汇总查询,正如你所看到的用处不大:

<?php 
$m = new MongoClient; 
$db = $m->test; 

$r = $db->so->aggregate([ 
    [ '$project' => [ 
     'time' => [ '$add' => [ 
      [ '$subtract' => [ '$m_time', '$m_latency' ] ], 
      $DELTA 
     ] ], 
     'm_time' => 1, 
     'm_latency' => 1 
    ] ], 
    [ '$match' => [ 'time' => [ '$gte' => $FROM, '$lte' => $TO ] ] ], 
    [ '$sort' => [ 'time' => 1 ] ] 
]); 
var_dump($r); 
?> 
+0

非常感谢! –

0

这没有任何意义。你不能链接在一起=>在类似案例,指定键值对时:

('m_time-m_latency'=>LATENCY_DELTA)=>array('$gte'=>$rangeXFrom) 

我真的是不知道你正在尝试做的有,以提供更好的建议。

+0

谢谢你的回应。我有一个SQL查询,我正在为此写一个mongodb查询。我写了几个简单的,但这看起来很复杂。不知道如何解决这个问题。 –