MongoDB Learning Record---The Find return value for PHP extensions

Source: Internet
Author: User

MongoDB was used in a recent project, mostly using MongoDB's PHP extension. The method used in MongoDB extensions for a query is find (). Here's a simple note for the experiment in understanding the MongoDB extension's find () method, and hopefully to help others.

In a mongodb php extension, you can query using Mongocolletion's Find ($query, $fileds), where find () matches the query based on $query criteria, $query the fields that need to be returned. When you make find (), it is important to note that all variables to be queried must be enclosed in single quotes, so that the value of the variable is not replaced by the command. Another thing to note is that $fields represents the items to be returned, all the fields to be displayed in $field correspond to 1, and the items that do not display correspond to 0, and 0 are used only for the ' _id ' field, which results in errors for other fields. Both $query and $ are passed to find () in the form of an array, and the parameters of the MongoDB PHP extension are passed in the form of an array.

  in the standard practice, quotation marks are not used in the $fields array (here and below if no special description is given, brackets including curly braces and parentheses) will cause 0, which will cause 0 to not work, but will not affect the 1, which advocates the use of standard practice. to illustrate the difference in quoting $fields fields, here are a few experiments to illustrate their effects.

  I. Standard practice

The value of the ' _id ' field in the $field array is 0 without quotation marks, and all the remaining fields to be displayed are 1.

  

(a)

(b)

Figure 1 Standard practice

When you use the Find () lookup after connecting to MongoDB to select a database and a collection, the returned result is actually cursors, and if you want to display the returned results, you need to use Iterator_to_array () to convert the returned results to the form of an array. As the result of Figure 1 (b) shows, the returned result does not have the ' _id ' field, and the returned result in the array's key is [0]. Here,MongoDB adds a ' _id ' field for each inserted document, and if you do not specify a ' _id ' field of 0 during the lookup, it will appear by default in the returned result. as shown in 2 (a) (b), when the ' _id ' field in the $query array is not specified, the default is the output ' _id ' field, and in the result of this output, key is no longer a numeric subscript starting with [0], but the value of the ' _id ' field in the original article.

(a)

  

(b)

Figure 2 The case where the ' _id ' field is not specified as 0

  Two, enclose 0 in quotation marks

If you enclose 0 in quotation marks, the $fields field is added ' _id ' = ' 0 ' or ' _id ' and ' 0 ', the ' _id ' field will still be displayed, and the corresponding array in the returned result array key is not starting from [0], but in the original document ' _ The value of the ID ' field corresponds to the ' key ' value of the array. As shown in.

(a)

(b)

(c)

Figure 3 Quotation marks for the ' _id ' field in the $fields array

  Third, use 0 in the $fileds array except for the ' _id ' field

As already mentioned, 0 can only be used in the ' _id ' field to control whether the value of the ' _id ' field is displayed, but it must never be used for other fields, which causes the program to error if used in other fields. As shown, set the ' tags ' field to 0, causing the program to error.

(a)

(b)

Figure 4 Error when using 0 in the other fields in the $field array

  This is also an indication of the $fields array used to store the return value, set the value of the field you want to display to 1, the field not set to display, but never allow 0 for the rest of the fields except the ' _id ' field.

  Summary:

  Beginner MONGO, later encountered problems in and everyone to study together. Sorry the figure is not very clear, when the picture was cut down and saved in Word, and then use the time is not very clear.

MongoDB Learning Record---The Find return value for PHP extensions

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.