Questions about the PHP query MongoDB limit return field

Source: Internet
Author: User
Tags mongodb find mongodb limit

Recently wanted to do a front-end control Interface field return of a basic method, through the MongoDB find ($query, $field) query to specify the fields of the query, but encountered such a problem:

There are two encapsulation methods in the work code:

/**
* Query a record
* @param String collection name
* @param array Query criteria
* @param The fields to be returned by array
* Results of @return array query
*/
Public Function Find_one ($coll _name, $condition = Array (), $ret _fields = Array (' _id ' = 0)} {
return $this->docsdb->find_one ($coll _name, $condition, $ret _fields);
}

/**
* Enquiry
* @param String collection name
* @param array query conditions array<br/> such as <i>array (col_a=>111) </i>
* @param Array Collection filter array<br/> full look like this: <i>array (Sort=>array (col_a=>1,col_b=>-1), skip=>100 , limit=>10, timeout=>5000, Immortal=>true) </i>, which means:
* <ol>
* <li>wrapped to wrapped as an array of keys, by default in order of arrays </li>
* <li>_id, the default does not return data with the Mongoid field, if you specify the return column is the same effect </li>
* <li>sort col_a to Asc,col_b for DESC, can be multi-column combination </li>
* <li>skip means to start fetching data from 101 records, skipping the first 100 </li>
* <li>limit the number of selected articles </li>
* <li>timeout indicates the time to wait for a response (temporarily not used) </li>
* <li>immortal indicates whether to maintain the link is permanent, default true, at this time timeout expires (temporarily not used) </li>
* </ol>
* @param array needs to return fields (usually returning only the necessary fields to speed up the response)
* Results of @return Mixed query
*/
Public function Find ($coll _name, $condition = Array (), $result _filter = Array (' wrapped ' = = ', ' with_objectid ' = = 0, ' Timeout ' = ' immortal ' = ' + ', $ret _fields = Array ()) {
return $this->docsdb->find ($coll _name, $condition, $result _filter, $ret _fields);
}

**
' With_objectid ' + 0 was set to 1 when the parameter was passed in
**
According to the above description, $ret _fields is the field in MongoDB find ($query, $field), but by testing, I have found such a problem if I want to go to display ID, passing in the parameter array
$ret _fields = Array (
' _id ' = 1,
' Account ' = 1,
' Card ' = 1
),
and call the Find method, according to the theory, this time ' _id ' This field is also displayed, and I through this condition directly query MongoDB, is also displayed, but the printed data is not with the ID, the data is as follows:

{
"Account": "Qwe",
"Card": ""
}

Add a test result, if the Find method does not pass in the $ret_fields variable past, that is, does not limit the return field, is the existence of ' _id '

By modifying the conditions, found that the ' _id ' and 1 This condition does not write is useless, so I use the same
$ret _fields to call the Find_one method, this time unexpectedly returns the ' _id ' field, the data is as follows

{
"_id": {
"$id": "58f472672430e11817000152"
},
"Account": "Qwe",
"Card": ""
}

This problem first recorded in this, after the settlement of a record of the solution, if there is a great God can explain this reason, please advise, thank you!

Questions about the PHP query MongoDB limit return field

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.