12
返回列表 发新帖
楼主: jieforest

PHP联合MongoDB开发实例

[复制链接]
论坛徽章:
277
马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14马上有车
日期:2014-02-18 16:41:112014年新春福章
日期:2014-02-18 16:41:11版主9段
日期:2012-11-25 02:21:03ITPUB年度最佳版主
日期:2014-02-19 10:05:27现任管理团队成员
日期:2011-05-07 01:45:08
11#
 楼主| 发表于 2014-6-22 12:47 | 只看该作者
Warning  Strings sent to the database need to be UTF-8 formatted to prevent an exception from occurring.

Once you’ve assigned your data properly to a variable—called $contact in this case—you can use the insert() function to insert it in the MongoCollection class:
  1. // Connect to the database
  2. $c = new MongoClient();
  3. // Select the collection 'people'
  4. $collection = $c->contacts->people;
  5. // Insert the document '$contact' into the people collection '$collection'
  6. $collection->insert($contact);
复制代码

使用道具 举报

回复
论坛徽章:
277
马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14马上有车
日期:2014-02-18 16:41:112014年新春福章
日期:2014-02-18 16:41:11版主9段
日期:2012-11-25 02:21:03ITPUB年度最佳版主
日期:2014-02-19 10:05:27现任管理团队成员
日期:2011-05-07 01:45:08
12#
 楼主| 发表于 2014-6-22 12:48 | 只看该作者
The insert() function takes five options, specified in an array: fsync, j, w, wtimeout and timeout. The fsync option can be set to TRUE or FALSE; FALSE is the default value for this option.

If set to TRUE, fsync forces the data to be written to the hard disk before it indicates the insertion was a success. This option will override any setting for the option w, setting it to 0. The j option can be set to TRUE or FALSE, where FALSE is the default. If set, the j option will force the data to be written to the journal before indicating the insertion was a success.

If you are unfamiliar with journaling, think of it as a log file that keeps track of the changes made to your data, before it is finally written to disk. This ensures that, were mongod to stop unexpectedly, it will be able to recover the changes written to the journal, thereby preventing your data from entering an inconsistent state.

The w option can be used to acknowledge or unacknowledge a write operation (making this option also applicable for remove() and update() operations). If w is set to 0, the write operation will not be acknowledged; set it to 1 and the write will be acknowledged by the (primary) server.

When working with replica sets, w can also be set to n, ensuring that the primary server acknowledges the write operation when successfully replicated to n nodes. w can also be set to 'majority'—a reserved string—ensuring that the majority of the replica set will acknowledge the write, or to a specific tag, ensuring that those tagged nodes will acknowledge the write.

For this option also, the default setting is 1. The wtimeout option can be used to specify how long the server is to wait for receiving acknowledgement (in milliseconds). By default, this option is set to 10000. Lastly, the timeout option allows you to specify how long (in milliseconds) the client needs to wait for a response from the database.

使用道具 举报

回复
论坛徽章:
277
马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14马上有车
日期:2014-02-18 16:41:112014年新春福章
日期:2014-02-18 16:41:11版主9段
日期:2012-11-25 02:21:03ITPUB年度最佳版主
日期:2014-02-19 10:05:27现任管理团队成员
日期:2011-05-07 01:45:08
13#
 楼主| 发表于 2014-6-22 12:48 | 只看该作者
The following example illustrates how to use the w and wtimeout options to insert data:
  1. // Define another contact
  2. $contact = array(
  3.         "Fir't Name" => "Victoria",
  4.         "Last Name" => "Wood",
  5.         "Address" => array(
  6.                         "Street" => "50 Ash lane",
  7.                         "Place" => "Ystradgynlais",
  8.                         "Postal Code" => "SA9 6XS",
  9.                         "Country" => "UK"
  10.         )
  11.         ,
  12.         "E-Mail" => array(
  13.                 "vw@example.com",
  14.                 "vw@office.com"
  15.         ),
  16.         "Phone" => "078-8727-8049",
  17.         "Age" => 28
  18. );
  19. // Connect to the database
  20. $c = new MongoClient();
  21. // Select the collection 'people'
  22. $collection = $c->contacts->people;
  23. // Specify the w and wtimeout options
  24. $options = array("w" => 1, "wtimeout" => 5000);
  25. // Insert the document '$contact' into the people collection '$collection'
  26. $collection->insert($contact,$options);
复制代码

使用道具 举报

回复
论坛徽章:
277
马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14马上有车
日期:2014-02-18 16:41:112014年新春福章
日期:2014-02-18 16:41:11版主9段
日期:2012-11-25 02:21:03ITPUB年度最佳版主
日期:2014-02-19 10:05:27现任管理团队成员
日期:2011-05-07 01:45:08
14#
 楼主| 发表于 2014-6-22 12:49 | 只看该作者
And that’s all there is to inserting data into your database with the PHP driver. For the most part, you will probably be working on defining the array that contains the data, rather than injecting the data into the array.

Listing Your Data

Typically, you will use the find() function to query for data. It takes a parameter that you use to specify your search criteria; once you specify your criteria, you execute find() to get the results. By default, the find() function simply returns all documents in the collection. This is similar to the shell examples discussed in Chapter 4. Most of the time, however, you will not want to do this. Instead, you will want to define specific information to return results for. The next sections will cover commonly used options and parameters that you can used with the find() function to filter your results.

使用道具 举报

回复
论坛徽章:
277
马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14马上有车
日期:2014-02-18 16:41:112014年新春福章
日期:2014-02-18 16:41:11版主9段
日期:2012-11-25 02:21:03ITPUB年度最佳版主
日期:2014-02-19 10:05:27现任管理团队成员
日期:2011-05-07 01:45:08
15#
 楼主| 发表于 2014-6-22 12:49 | 只看该作者
Returning a Single Document

Listing a single document is easy: simply executing the findOne() function without any parameters specified will grab the first document it finds in the collection. The findOne function stores the returned information in an array and leaves it for you to print it out again, as in this example:

  1. // Connect to the database
  2. $c = new MongoClient();
  3. // Select the collection 'people' from the database 'contacts'
  4. $collection = $c->contacts->people;
  5. // Find the very first document within the collection, and print it out
  6. // using print_r
  7. print_r($collection->findOne());
复制代码

As noted previously, it’s easy to list a single document in a collection: all you will need to do is define the findOne() function itself. Naturally, you can use the findOne() function with additional filters. For instance, if you know the last name of a person you’re looking for, you can specify it as an option in the findOne() function:

  1. // Connect to the database
  2. $c = new MongoClient();
  3. // Select the collection 'people' from the database 'contacts'
  4. $collection = $c->contacts->people;
  5. // Define the last name of the person in the $lastname variable
  6. $lastname = array("Last Name" => "Moran");
  7. // Find the very first person in the collection with the last name "Moran"
  8. print_r($collection->findOne($lastname));
复制代码

使用道具 举报

回复
论坛徽章:
0
16#
发表于 2014-11-29 06:26
thanks


您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

TOP技术积分榜 社区积分榜 徽章 团队 统计 知识索引树 积分竞拍 文本模式 帮助
  ITPUB首页 | ITPUB论坛 | 数据库技术 | 企业信息化 | 开发技术 | 微软技术 | 软件工程与项目管理 | IBM技术园地 | 行业纵向讨论 | IT招聘 | IT文档
  ChinaUnix | ChinaUnix博客 | ChinaUnix论坛
CopyRight 1999-2011 itpub.net All Right Reserved. 北京盛拓优讯信息技术有限公司版权所有 联系我们 未成年人举报专区 
京ICP备16024965号-8  北京市公安局海淀分局网监中心备案编号:11010802021510 广播电视节目制作经营许可证:编号(京)字第1149号
  
快速回复 返回顶部 返回列表