请选择 进入手机版 | 继续访问电脑版
楼主: Nicolle
4396 27

Sams Teach Yourself NoSQL with MongoDB in 24 Hours, [推广有奖]

Nicolle 学生认证  发表于 2015-7-17 10:38:59 |显示全部楼层 |坛友微信交流群
提示: 作者被禁止或删除 内容自动屏蔽

使用道具

Nicolle 学生认证  发表于 2015-7-17 10:40:01 |显示全部楼层 |坛友微信交流群
提示: 作者被禁止或删除 内容自动屏蔽

使用道具

Nicolle 学生认证  发表于 2015-7-17 10:41:43 |显示全部楼层 |坛友微信交流群
提示: 作者被禁止或删除 内容自动屏蔽

使用道具

Nicolle 学生认证  发表于 2015-7-17 10:43:10 |显示全部楼层 |坛友微信交流群
提示: 作者被禁止或删除 内容自动屏蔽

使用道具

Nicolle 学生认证  发表于 2015-7-17 10:45:16 |显示全部楼层 |坛友微信交流群
提示: 作者被禁止或删除 内容自动屏蔽

使用道具

Nicolle 学生认证  发表于 2015-7-17 10:46:07 |显示全部楼层 |坛友微信交流群
提示: 作者被禁止或删除 内容自动屏蔽

使用道具

Lisrelchen 发表于 2015-7-17 10:50:21 |显示全部楼层 |坛友微信交流群
Updating Documents from Java

After objects have been inserted into a collection, you often need to update them from Java as data changes. The update() method on the DBCollection object enables you to update documents in a collection. The update method is versatile yet fairly easy to implement. The syntax for the update() method follows:

  1. update(query, update, [upsert], [multi])
复制代码

The query parameter is a BasicDBObject object that identifies which document(s) you want to change. The request matches the properties and values in the query with the fields and values of the object, and only those that match the query are updated. The update parameter is a BasicDBObject that specifies the changes to make to the documents that match the query. Hour 8, “Manipulating MongoDB Documents in a Collection,” describes the update operators used in this object.

The upsert parameter is a Boolean. When true, if no objects match the query, a new document is created. The multi parameter is also a Boolean; when true, all documents that match the query are updated. When multi is false, only the first document that matches the query is updated.

For example, the following changes the category field value to old for items in the collection, where category currently is new. With upsert set to false, new documents are not created even if no documents have a category of new; with multi set to true, all documents that match are updated:

  1. BasicDBObject query = new BasicDBObject("category", "New");
  2. BasicDBObject update = new BasicDBObject("$set",
  3.     new BasicDBObject("category", "Old"));
  4. update(query, update, false, true);
复制代码

使用道具

Lisrelchen 发表于 2015-7-17 10:54:00 |显示全部楼层 |坛友微信交流群
Upserting Documents from Java

Another way to use the update() method on the DBCollection object in Java is for upsert operations. An upsert operation first tries to update documents in the collection. If no documents match the update query, the $set operator is used to create a new document and add it to the collection. The following shows the syntax for the update() method:

  1. update(query, update, [upsert], [multi])
复制代码

The query parameter identifies which document(s) you want to change. The update parameter is a BasicDBObject that specifies the changes to make to the documents that match the query. For upsert operations, the upsert parameter should be set to true and the multi parameter should be set to false.

For example, the following performs an upsert on a document with name=myDoc. The $set operator defines the fields used to create or update the document. With upsert set to true, if the document is not found, it is created; otherwise, it is updated:

  1. BasicDBObject query = new BasicDBObject("name", "myDoc");
  2. BasicDBObject setOp = new BasicDBObject("name", "myDoc");
  3. setOp.append("number", 5);
  4. setOp.append("score", 10);
  5. BasicDBObject update = new BasicDBObject("$set", setOp);
  6. update(query, update, true, false);
复制代码

使用道具

Lisrelchen 发表于 2015-7-17 10:55:01 |显示全部楼层 |坛友微信交流群
13.Summary

In this hour, you looked at the objects the PHP MongoDB driver provides. These objects represent the connection, database, collection, cursor, and documents and provide functionality to access MongoDB from your PHP applications.

You also implemented the PHP MongoDB driver and created a basic PHP MongoDB application to connect to the database. Then you learned how to use the MongoCollection and MongoCursor objects to find and retrieve documents. Finally, you learned how to count and sort documents represented by the cursor before retrieving them.


使用道具

Lisrelchen 发表于 2015-7-17 10:55:42 |显示全部楼层 |坛友微信交流群
14.Summary

In this hour, you learned how to use additional methods on the MongoCollection and Cursor objects. You learned that the limit() method can reduce the documents the cursor retrieves and that usinglimit() and skip() enables you to page through a large dataset. Using a fields parameter on the find() method enables you to reduce the number of fields returned from the database.

This hour also covered applying the distinct(), group(), and aggregate() methods on the MongoCollection object to perform data gathering operations from a PHP application. These operations enable you to process data on the server before returning it to the PHP application, reducing the amount of data sent and the work required in the application.

使用道具

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

本版微信群
加好友,备注cda
拉您进交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-3-28 17:35