0

So I have a php script on a web server listening for mongoDB queries via a JSON object POST with another page building and sending queries to the php service. All my standard queries such as : {"field1":"2342342","field2":"234234"} are sent and return the correct resutls.

However, when I try to send a query that includes a range of values it returns nothing.

{"field2":"1234","date" : {$gte : "2013-02-11"},"date" : {$lte : "2013-02-11"}}

I can go into the command line and call:

db.collection.find({"field2":"1234","date" : {$gte : "2013-02-11"},"date" : {$lte : "2013-02-11"}} ) 

which returns the results as expected. Obviously there is something going on that I do not fully understand.

PHP command doing the search:

$c_collection->find(json_decode($request));

Any help would be much appreciated.

2
  • How is $request built? Commented Feb 28, 2013 at 22:00
  • I have a number of fields on a form that, when search is hit, builds the JSON string and sends it(ajax POST) to the php script. Commented Feb 28, 2013 at 22:29

1 Answer 1

1

You are attempting to search the database using a duplicate element name. MongoDb would issue an error such as this while performing this search:

Duplicate element name 'date'.

You need to place your range within the same block. For example:

{
    'date': {
        $gte:"datehere",
        $lte:"datehere"
    }
}

In PHP:

$criteria = array('date' => array('$gte' => 'datehere', '$lte' => 'datehere'));

Then use that json as your criteria.

Also, why are you decoding the criteria before passing it to the database? The method can accept an array just fine.

Sign up to request clarification or add additional context in comments.

2 Comments

Nevermind in regards to decoding the criteria. I had misunderstood and now I see that you are building a json string instead of an array before sending it.
You are correct. There was an additional issue of the dates being strings and the comparison of them. There was not anything else going on. Now getting results from: {"runDateTime" : {"$gte" : "2013-02-04" , "$lte" : "2013-02-04Z"}}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.