0

I seem to be having some trouble using the cursor as I try to pull data out of a MongoDB. Here is a screenshot of my simple data structure:

data structure screenshot http://droplr.com/IpcH+

I'm trying to use the following:

$collection_name = "users";
$collection = $this->mongo->db->$collection_name;
$which_document = array("email" => $email_address);
$which_fields = array("words");
$cursor = $collection->find($which_document, $which_fields);

But my $cursor does not seem to be going any further into the document than the actual document. In the end, I'm looking to sort() and limit() on the created_at field within the "words" field, but I seem to be at an impasse here. And either MongoDB's error passing isn't very helpful, or I've no idea how to use it properly. (I'd assume the latter.)

One idea: I'm not sure the "words" table being generated inside of square brackets is correct, but here is the code I'm using to insert data here:

function create_word($word, $email_address){
    $collection = 'users';

// make sure word isn't null
if($word !== NULL){

// add new data to the 'words' set
    $new_data = array('$addToSet' => array('words' => array(
                                                            'word' => $word,
                                                            'status' => 'open',
                                                            'created_at' => time()
                                                            )));
    $this->mongo->db->$collection->update(array('email' => $email_address), $new_data);         
}

Thanks so much for any help you can give!

2 Answers 2

2

You can't sort subdocuments. You need to do it on the client side. In your case retrieve the words array:

$array = iterator_to_array($cursor); 
$words = $array[0]["words"];

and then use php to sort the values.

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

3 Comments

Ah, dot notation! Is there a hard-and-fast rule on when one should use dot notation in the php driver? Unfortunately, no luck. Here is a screenshot of a var_dump of the cursor value using this code. Did I miss something from your updates? I'm actually going to be sorting on created_at, but I tried to alternate between "1" and "-1", with no differences.
I was curious and tried it myself now. Sorting subdocuments doesn't work. The example in the documentation covers a different kind of sorting where you have nested values inside a document and sort all the documents after them and not an nested array.
Interesting. This is my first project with MongoDB, from your vantage point, would you suggest some sort of data restructuring? This seems like a fairly important issue, as I'll be essentially paginating this data, it would be very nice to use .limit(), .skip(), and .sort().
1

Try

$collection->find(array($which_document, $which_fields))->sort(array("words.created_at" => -1));

5 Comments

Unfortunately I have no luck here. Whether I use 1 or -1, my var_dump remains the same.
Let's clarify shall we? You're trying to sort the "words" array by the "created_at?" I'm not sure if you can do that. I raised this issue w/ the developers. See the following discussion I raised on mongodb's forum groups.google.com/group/mongodb-user/browse_thread/thread/…
Sounds like you and halfdan are both pointing to the same issue. Reading through the thread now, but I'll pose the same question here as I have with him. Would you suggest a data restructuring since I'll be working so intensively with this data?
If the "words" array will get large, I would separate it into its own collection.
Thanks so much, the intangibles of best data structure practices and minor things that won't work are difficult to grasp at first. Looks like I need to break myself of the idea that as much as possible should be stored in a single collection.

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.