0

Structure :

Collection : merchant > id, name, country

I want to select all name field from this collection, but all the documentation I read says I must specify the name like :

  $name = array('name' => 'test');

  $cursor = $collection->find($name);

PS : Something like

select name from merchant

in sql is enough

2 Answers 2

1

Try the following:

db.merchant.find({}, {"name": 1});

Which would be translated to the following PHP code:

$name = array('name' => 1);

Hope this would solve your issue.

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

Comments

0

I think you are confusing the first optional parameter query with the second optional parameter fields ...

http://php.net/manual/en/mongocollection.find.php

public MongoCursor MongoCollection::find ([ array $query = array() [, array $fields = array() ]] )

If you pass basically an empty array for your query (ala *) and name for the fields ... that should work.

$db->collection->find(array(), array("name" => 1));

This mapping chart might be helpful for you: http://php.net/manual/en/mongo.sqltomongo.php

1 Comment

This will return _id field, not name :(

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.