2

I have following in the database, and I like to find this item with a the search term "open"

It should almost be something like this, I guess???

 $query = array(array('keywords' => array('$in'=>'open')));
 $cursor = $collection->find($query);
 return iterator_to_array($cursor);

In Database:

[3b33162ad4ed5ffdeb88a1b2085535b1] => Array
    (
        [_id] => 3b33162ad4ed5ffdeb88a1b2085535b1
        [title] => Something
        [keywords] => Array
            (
                [2] => open
                [7] => source
            )

        [added] => MongoDate Object
            (
                [sec] => 1305884144
                [usec] => 658000
            )

    )
1
  • And the question is? What is the problem? And why a nested array(...)? Commented May 20, 2011 at 12:05

2 Answers 2

2

The problem you're having is the structure of your data.

MongoDB natively works with arrays when querying. The following will work.

$document = array( 'keywords' => array('open', 'source') );
$collection->insert($document);
$collection->find( array('keywords' => 'open') );

The problem you have is that keywords is not an array in your document. In your document keywords is another document.

'keywords' => array( '2' => 'open',
                     '7' => 'source' )

In JSON this would look like this:

{ 'keywords' : { '2': 'open', '7': 'source' } };

Based on what you're trying to do you need to save keywords as an array and not as a hashtable.

In PHP it's not always obvious which you're dealing with at code time. However, if you print_r an array, it will look like the following. Notice the array is zero-based and is not missing any keys.

    [keywords] => Array
        (
            [0] => open
            [1] => source
            [2] => rules
        )
Sign up to request clarification or add additional context in comments.

Comments

1

You can query Arrays in MongoDB like normal values. Here an example in the JavaScript Shell.

> db.php.insert({title: "something", keywords: ["foo","bar"]})
> db.php.insert({title: "something", keywords: ["open","bar"]})
> db.php.insert({title: "something", keywords: ["baz","faz"]})
> db.php.find({ keywords: "open" });
{ "_id" : ObjectId("4dd677229a547d85bb02fce5"), "title" : "something", "keywords" : [ "open", "bar" ] }

For Faster Queries you should create an index on the array.

db.php.ensureIndex({keywords: 1})

3 Comments

What has this to do with the PHP specific question?
He didn't knew how to query the keywords fields. I showed him in the JavaScript Shell how he can do it. He just needs to translate the Query to PHP. Probably it is: $query = array("keywords" => "open").
I don't get it! I still don't know how to query something if the key is unknown.

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.