2

I have a mongodb collection of following format :

{
        "_id" : ObjectId("5141916511e5b498fd2031c4"),
        "itemid" : 1,
        "recommendations" : [
                {
                        "itemid" : 216,
                        "rating" : 0.875297364790784
                },
                {
                        "itemid" : 246,
                        "rating" : 0.8793363655122852
                }
        ]
}
{
        "_id" : ObjectId("5141916511e5b498fd2031c5"),
        "itemid" : 2,
        "recommendations" : [
                {
                        "itemid" : 60,
                        "rating" : 0.9405825249353504
                },
                {
                        "itemid" : 76,
                        "rating" : 0.8822827294664317
                }
        ]
}

I want to retrieve recommendations for a given itemid and then iterate over it to print all the recommended itemids and ratings.I am using php for this.

When I try to iterate over returned cursor,it throws "Fatal error: Call to a member function hasNext() on a non-object" error. It seems the resultset returned by query is not of type cursor.

Below is the code I am using :

<?php
$mongodb = new Mongo("10.128.170.49:27017");
$database = $mongodb->ProductData;
$collection = $database->Recommendation1;

$cursor1 = $collection->findOne(array("itemid" => 1),array('recommendations'));

var_dump($cursor1);

echo "<hr/><p>iterating over a cursor</p>";
while ($cursor1->hasNext()): $document = $cursor1->getNext(); 
                        $itemid= $document['itemid'];
                        $probable_rating= $document['rating'];                      
                         echo ($itemid)."<br/>";
                         echo ($probable_rating)."<br/>";
                         echo "<hr/>";
endwhile;
?>

Please help me resolve this issue.

5
  • Using findOne() doesn't return a cursor. It returns a single object. You don't need to iterate for that. Commented Mar 21, 2013 at 9:28
  • As well as the answer below you can make your life, oh-so-easier by using a foreach instead of a while and then manually using getNext() Commented Mar 21, 2013 at 9:39
  • @Sammaye: foreach is not supported in V8, so newer versions of MongoDB no longer have that function. Commented Mar 21, 2013 at 11:44
  • @Nick They do in PHP, which is what he is coding in, in fact spidermonkey does not have a foreach either, instead it has a mongodb implemented forEach Commented Mar 21, 2013 at 11:52
  • @Nick Also V8 is only default since version 2.4 which was released yesterday Commented Mar 21, 2013 at 11:53

1 Answer 1

4

You have to change findOne() to find()

  • findOne() returns the first found result
  • find() returns a cursor
Sign up to request clarification or add additional context in comments.

2 Comments

I want to retrieve recommendations for a given itemid and it would always return a single result.Hence I used findOne. I am able to retrieve the recommendation using query that I used.I checked it using var_dump.But the query returns value for recommendation field which is an array.I have given the structure of the collenction in my above post.The recommanation array has multiple documents.I want to iterate over the returned array and print all recommended itemids for a searched productid/itemid. How should I retrieve value of each itemid and rating stored in recommendations array????
Well, if you are returned an array, then you should iterate over the array and don't use a cursor. Search for the correct php syntax to iterate over an array. I don't know php ...

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.