MongoDB 指定应用上下文数据模型
2015-10-12 10:47
309 查看
Model Data for Atomic Operations
Pattern
In mongoDB, write operations, eg. db.collection.update(), db.collection.findAndModify(), db.collection.remove(), are atomic on the level of a single document. For fields the must be updated together, embedding the fields within the same document ensures that the fields can be update atomically.For example, consider the situation where you need to maintain the book information, including the number of copies available for checkout as well as the checkout information.
The available copies of the book and the checkout information should be in sync. As such, embedding the available field and checkout field within the same document ensures that the two field atomically.
![](http://images2015.cnblogs.com/blog/817474/201510/817474-20151012110126179-1758738642.png)
Then to update with new checkout information, you can use the db.collection.update() method to atomically update both the available field and the checkout field.
![](http://images2015.cnblogs.com/blog/817474/201510/817474-20151012110127007-14660351.png)
Model Data to Support Keyword Search
This pattern describes one method for supporting keyword search using MongoDB to support application search functionality, that use keywords stored in an array in the same document as the text field. Combined with a multi-key index, this pattern can support application’s keyword search operations.Given a collection of library volumes that you want to provide topic-based search. For each volume, you add the array topics, and you add as many keywords as needed for a given volume.
For the Moby-Dick volume:
![](http://images2015.cnblogs.com/blog/817474/201510/817474-20151012113014851-610667399.png)
You then can create a multi-key index on the topics array:
![](http://images2015.cnblogs.com/blog/817474/201510/817474-20151012113016288-370448467.png)
The multi-key index create separate index entry for each keyword in the topic array. For example, the index contain one entry for whaling and another for allegory:
![](http://images2015.cnblogs.com/blog/817474/201510/817474-20151012113016757-811016279.png)
Model Monetary Data
……Model Time Data
Overview
MongoDB stores time in UTC by default, and will convert any local time representations into this form. Application that must operate or report on some unmodified local time value may store the time zone alongside the UTC timestamp, and compute the original local time in their application logic.Example
Storing the current date and the current offset from UTC:![](http://images2015.cnblogs.com/blog/817474/201510/817474-20151012113017319-1612438881.png)
Reconstruct the original local time by applying the saved offset:
![](http://images2015.cnblogs.com/blog/817474/201510/817474-20151012113017851-83036103.png)
相关文章推荐
- MongoDB mapReduce操作
- mongodb logRotate 定期…
- Mongodb shard 索引
- linux mongodb driver&n…
- js 操作mongodb
- mongodb java 多线程执行
- mongodb shard 分片
- mongodb 权限管理
- mongodb mapReduce实例
- mongodb replset 成员类型
- mongodb timestamp 处理
- mongodb 模糊查询
- 编译MongoDB 及驱动
- mongodb js使用
- Mongodb shard && shardkey
- MongoDB 复制集配置
- MongoDB每64位版本下载
- 双十一来了,别让你的mongodb宕机了
- MongoDB安装指南(windows环境下)
- Ubuntu 升级 MongoDB 2.x 到 3.x