0

I am trying to convert the following example from mysql to Mongodb.

However, i am stuck at a point. Any inputs would be highly appreciated.

SQL Query:

$cmd = "SELECT COUNT(*) FROM (SELECT * FROM pkt_tbl WHERE m_out_port= " . $out_port . " AND m_time<=" . $selPointX . " AND m_time>=" . $startTime . ") AS pkt_tbl WHERE m_in_port=" . $i;

Mongodb Query (WHAT I TRIED):

    $find_query = array('m_out_port'=>$out_port, 'm_in_port'=>$i,'m_time'=>array('lte' => $selPointX, 'gte'=>$startTime));
$find_projections = count();
$result = $table->find($find_query, $find_projections);

What I tried (Aside from Code)

I read the mongodb docs and other PHP.NET (Mongodocs) but still couldn't find relevant solution.

I am not sure if it's right/I am going in right path.

Please elucidate. - R

iron man
  • 41
  • 8
  • Please check. OOps. I changed it now. Does it look alright or do I need to do anything else? – iron man Jul 16 '13 at 02:05
  • Please don't change your question so much after it was answered. Rather use comments in case it doesn't work or you have further issues. Your error now is that you miss '$' in '$gte' and '$lte'. – sashkello Jul 16 '13 at 02:09
  • Because now the answer is answering different question, you see... – sashkello Jul 16 '13 at 02:10
  • Thank you for the inputs.I am quite new here but you are right! :) – iron man Jul 16 '13 at 02:44
  • Looks like upon execution, I'm facing an error at `$find_projections = count();` Is it appropriate to use Select count(*) in this way? @sashkello – iron man Jul 16 '13 at 23:14

1 Answers1

0

You need to use $gte and $lte keywords for date range (greater or equals to and less than or equals to). Now you select only those records where date is equal to startTime and selPointX (so if they are different, you get no values returned).

Change your time conditions to:

'm_time'=>array('$lte' => $selPointX, '$gte'=>$startTime)

If you need those times not inclusive, then use $gt and/or $lt.

sashkello
  • 17,306
  • 24
  • 81
  • 109
  • Looks like upon execution, I'm facing an error at `$find_projections = count();` Is it appropriate to use Select count(*) in this way? – iron man Jul 16 '13 at 23:11
  • I think you just need $result = $table->count($find_query) – sashkello Jul 16 '13 at 23:52
  • This is my final query, and I am still getting an error. If i comment the `$result` variable then the page works fine. (I am testing the query in a seperate php file) `$find_query = array('m_out_port'=>$out_port,'m_time'=>$selPointX, 'm_time'=>array('$gte'=>$startTime)); echo $find_query; $result = $table -> $find($find_query); echo $result;` Any inputs? And thanks @sashkello for your help – iron man Jul 17 '13 at 00:06
  • 1. 'm_time' issue is not completely right (selects time = selPointX and > startTime). 2. find($find_query) instead of $find($find_query) – sashkello Jul 17 '13 at 00:13
  • Alright. thank you. I'll try to see if it works again. I'll return back with the result. – iron man Jul 17 '13 at 00:53