How can i use if condition with MongoDB find query

<?php
   $realtime = date("2016-09-22 12:55:24");
   $mongotime = New DateTime($realtime);
   $mt = $mongotime->getTimeStamp(); 
   var_dump($mt); 
   //database Connection
   $server= mongodb://localhost:27017"; 
   $c = new Mongo($server); 
   $db = $c->dbname; 
   echo "Database selected"; 

   //collection selection
   $collection = $db->collection; 
   echo "Collection selected"; 
   $cursor = $collection->find(); 

   //If Condition here i'm getting diifficulty
   if (($realtime-timestamp)<=5) { 
     if (channel<=11) {
       if (channel>=36) { 
         echo “dual band”; 
       }
     } 
     if (channel>=36) { 
       if (channel<=11) {
         echo “dual band”; 
       }
     } else { 
       echo “Single band”;
     }
     foreach ($cursor as $doc) { 
       var_dump($doc); 
     }
   }
 ?>

Hi @chopraarpita92 and welcome to SitePoint Forums.

Please enclose your scripts starting and ending with three backticks on their own lines:

<?php
$realtime 	= date("2016-09-22 12:55:24");
$mongotime 	= New DateTime($realtime);
$mt 		= $mongotime->getTimeStamp(); 
var_dump($mt); 

# DATABASE CONNECTION
  $server= mongodb://localhost:27017"; 
  $c = new Mongo($server); 
  $db = $c->dbname; 
  echo "Database selected";

# COLLECTION SELECTION
  echo "Collection selected"; 
  $collection = $db->collection; 
  $cursor     = $collection->find(); 
  # If Condition
    if (($realtime-timestamp) <= 5) { 
			
      if (channel <= 11) {
	if (channel>=36) { 
	    echo “dual band”; 
	}
       }
		 
     if (channel >= 36) { 
	if (channel<=11) {
	   echo “dual band”; 
	}
			
	}else{ 
	   echo “Single band”;
	}
			
	foreach ($cursor as $doc) { 
	   var_dump($doc); 
	}
     }

I find it best to indent code to see where the condition starts and ends.

Can you spot the problem where certain conditions are never met?

1 Like

Thanks John,

I’m using first time mongo with php. So i’m not familiar that how to use if condition with find query. i’m able to fetch the data if i’m not using if condition but what i need is putting time value & checking the value with database time value if they hv difference btw less than or equal to 5 then i’m checking of the channel value.

Here is the dataformat -
{ “_id” : ObjectId(“57e36429e4b0b720a9196d8e”), “timestamp” : NumberLong(1474520101), “age” : NumberLong(2713), “data_rate” : NumberLong(4), “type” : NumberLong(2), “channel” : NumberLong(6), “ap” : “36.222.198.197.171.30”, “associated” : NumberLong(1), “rssi” : NumberLong(-67) }

Can you see that if the channel <= 11 it will never ever meet the following condition?

Same applies for the next if(channel >= 36)

I have a bunch of data & want to check that in the whole database do i have both type of channel data. i mean some channels are less than 11 & some are greater than 36 so want to check wheather i have both type of data. when i’m checking the time. pre define time with database time also it is not comparing.

Putting aside the conditional logic errors, have you tried running the “query” with hard-coded values to see if at least that works?

Off-Topic
if ((age < 21) AND (age > 65)) {
echo “ageless”;
}

yes i have checked with hard core value.

Please read and try to understand Post #4.

The PHP logic is incorrect and some conditions will never be met.

Try adding these lines to the top of your script:

<?php 
   error_reporting(-1);
   ini_set( 'display_errors', 'true');

$realtime = date("2016-09-22 12:55:24");

Unless channel is a predefined constant an error will be shown.

Also to ensure the if condition is correct I would add the following:

echo '<br>' .__LINE__ .'  | ' .$realtime-timestamp;
if (($realtime-timestamp)<=5) { 
   echo '<br>' .__LINE__ .'  | ' .$realtime-timestamp;
   die;

Thanks. As long as that code is working it leaves the conditional as the problem area.

I’m guessing that the database is returning a “full set of results” and you are using PHP to work with those results instead of using PHP to put together queries that would select only what you’re interested in.

The latter would likely be more efficient, but as long as the database is small the former is probably good enough and any difference between the two would be negligible.

I see “$realtime-timestamp)<=5” so I’m assuming if it’s > 5 you do nothing with the results.

Then you have “channel<=11” and “channel>=36”

I don’t know which is dual band and which is single band, but I think if you don’t nest the contradicting condition (and make sure they’re variable$) you just may see what you’re hoping to see.

EDIT
I just noticed your code example has “curly quotes”. These will cause problems. If you are using Word as a code editor please don’t

Try this

if (($realtime-timestamp) <= 5) { 
  if ($channel <= 11) {
    echo "channel less than 11"; 
  }
  if ($channel >= 36) { 
    echo "channel greater than 36";
  }
}else{ 
  echo "timestamp greater than 5";
}

is “$realtime-timestamp” a defined variable?

1 Like

Thanks John! I have tried this one & it is not working. i can’t put $channel if i’m putting $ then it is giving error. ‘channel’ is working but the thing is i have channel 40 which is greater than 36 so it should like channel greater than 36 but it is showing channel less than 11.

Errors are preventing your script from working.

What are the errors displayed?

Try using my debug function:

<?php 
   error_reporting(-1);
   ini_set( 'display_errors', 'true');

//===========================
  usage fred( $var );
//===========================
function fred($val)
{
  echo '<pre>'; 
    print_r( $val ); 
    // var_dump( $val );
  echo '</pre>';
}//

// try this:
   fred(cursor);

I’m afraid that because I’m unfamiliar with mongoDB you likely know more than I do, and that I can only guess.

So “channel” is a mongo global or a field name and not a PHP variable?

Could you eliminate all the PHP if conditional code by using a different “find” line? eg.

$cursor = $collection->find( { channel { $lt: 11, $gt: 36 } } );

Off-topic
I thought Ruby’s ActiveRecord syntax was different, but seeing operators that look like variables !

yes its not a variable, Its a field name. Find query what you have suggested i can’t use that because it will give the data only which has channel btw 11 to 36. I have used the same query with another piece of code.

Try using the following breakpoint and ensure you have no errors then gradually move the breakpoint until you discover an error.


echo '<br>' .__LINE__; kill;

// when an error is encountered use the following:
fred( $variable-giving-the-problem ); 
echo '<br>' .__LINE__; kill;

// or 

var_dump( $variable-giving-the-problem );
echo '<br>' .__LINE__; kill;

If you cannot solve the problem then copy and paste the errors here.

Edit:
Added missing text.

Hmmm. confusing indeed.

I must say I would find it aggravating to have my
“less than 11 or greater than 36”
interpreted as
“greater than 11 and less than 36”

There have been times I’ve thought about trying mongo, but not anymore.

1 Like

What is a complete list of all the possible ‘channel’ values?

for less than 11 it can be 3,6,11 & greater than equal to 36 can be 36,40,44 something like that

yes bit confusing. Can ewsily find the values btw 11 to 36 but diificult to find less than 11 & greater than 36.

Very interesting discussion glad that I came across such informative post.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.