MySQL到Mongodb的查询问题


MySQL To Mongodb query issue

我要感谢过去帮助过我的每一个人,感谢你抽出时间阅读这篇文章。

我在将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.

但是,我想不通,如果能帮忙,我们将不胜感激。

在您的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 } }
] );

$project步骤中,我们将字段时间计算为m_time - m_latency + DELTA。我们还输出原始的m_timem_latency字段。然后在$match步骤中,我们将计算出的timeFROMTO进行比较。最后根据计算得到的CCD_ 9进行排序。(由于您最初的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 );
?>

这毫无意义。在以下情况下指定键值对时,不能将=>链接在一起:

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

老实说,我不确定你在那里想做什么,以便提供更好的建议。