0

I am building a simple messaging system, and i have a collection in mongodb with documents like this:

{ "_id" : ObjectId("50ad003f9811e5bc5c000000"), "between" : [ "user1,user2,user3" ] }

I want to perform this query:

db.conversations.find({'between': ["user1,user2,user3"]});

to get this exact document back. This query works in mongo shell.

in php-mongo, i tried this:

$collection->find(array("between"=>array("user1", "user2", "user3")));

but it does not work.

What am i doing wrong ?

1
  • no error, it only returns an empty result Commented Nov 21, 2012 at 18:26

4 Answers 4

1

Wouldn't you want to do an In query here?

db.collection.find( { "between" : { $in : ["user1", "user2", "user3"] } } );

See In query here: Mongo Advanced $in query

making your PHP query look like:

$collection->find(array("between"=>array("$in"=>array("user1", "user2", "user3"))));
//untested, should be something similar to this.

or if you're trying to find it exactly wouldn't you just be able to do:

$collection->find(array("between"=>array("user1,user2,user3")));
Sign up to request clarification or add additional context in comments.

3 Comments

actually no, since the in operator matches a document that has any of the elements of the array. And i can't use the $all operator too, because i don't want to match a document that has all 3 users plus another one.
so you want to match only the documents that have exactly 1,2 and 3?
@QuArK: $collection->find(array("between"=>array("user1,user2,user3"))); doesn't work in your php? that would seem to simulate the exact query you have working in Mongo.. (notice the missing " separation between user's 1,2,3
1

First of all when you are saving your data you have to use array not a string

{ "between" : [ "user1,user2,user3" ] }

this stores in "between" an array of one element "user1,user2,user3"

Basically when you do your query in shell everything is ok, because you are asking for a array with one element. But in php you are asking for an array of three elements.

So, when you save your data, most probably that is what you need :

{ "between" : [ "user1","user2","user3" ] } 

and this will give you an array of three users.

Then read the documentation http://www.mongodb.org/display/DOCS/Advanced+Queries to and adjust your query depends on what you need: either the exact array or only some elements in the array

Comments

0

Have you tried:

$collection->find(array("between"=>"user1,user2,user3"));

or

$collection->find(array( "$elemMatch" => array( "between"=>"user1,user2,user3" ));

Comments

0

The $in operator is analogous to the SQL IN modifier, allowing you to specify an array of possible matches.

Consider the following example which uses the $or operator.

$collection->find([
    'select' => ['$in' => ['option 1', 'option 2', 'option 3']]
]);

References

Comments

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.