1

How can I make mongodb return results in a simple array?

Ex:

My first query:

$user_ids = $dm->createQueryBuilder('AcmeBundle:Users')
->hydrate(false)
->select('_id')
->getQuery()
->execute();

My second query:

$no_credit = $dm->getRepository('AcmeBundle:Places')
->createQueryBuilder('places')
->distinct('_id')
->field('visited.users')
->in($user_ids)
->getQuery()
->count();

How can I achieve this when the first query won't return an array of MongoID objects?

7
  • by simple array, you mean numeric array? Commented Jul 30, 2012 at 16:57
  • 1
    I must admit I do not symfony however I believe it returns an implmentation of MongoCursor as such getting the cursor object and doing iteratortoarray or something similar will solve the problem in a hackish way. Commented Jul 30, 2012 at 17:02
  • by "simple array" I mean an array(). Since that's how in() works: field('wtvr')->in(array(,,,)) . Commented Jul 30, 2012 at 17:09
  • Ok I see multiple problems here now. You will need to filter out your first query by iterating through it and forming the new array in which to do the in(). It is the only way due to how Mongo returns the results. Commented Jul 30, 2012 at 17:10
  • @Sammaye Please write your comment as an answer so I can accept it. Commented Jul 30, 2012 at 17:16

1 Answer 1

1

I must admit I do not symfony however I believe it returns an implmentation of MongoCursor as such getting the cursor object and doing iteratortoarray or something similar will solve the problem in a hackish way.

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

1 Comment

While this didn't exactly solve my problem, it does solve similar issues. By doing array_keys() I can get the "raw data" that is stored on the results. But as your second comment suggests, "You will need to filter out your first query by iterating through it and forming the new array in which to do the in()".

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.