MongoDB原子更新修改器详解:$inc/$set/$unset/$push/$pop/upsert

2 下载量 29 浏览量 更新于2024-09-01 收藏 62KB PDF 举报
MongoDB 是一种流行的分布式文档数据库,特别适合存储半结构化数据。在处理文档更新时,除了完全替换整个文档之外,有时需要对文档的特定字段进行原子性操作,这就需要用到 MongoDB 的一系列更新修改器,包括 $inc、$set、$unset、$push、$pop 和 upsert。 1. **$inc (增量操作)**: - $inc 是MongoDB中最基本的修改器之一,用于对数值类型的键进行加法或减法操作。在给出的示例中,`db.b.update` 方法利用 `$inc` 修改器对 `size` 字段进行增量操作。例如,`$inc: {"size":1}` 表示将 `size` 的值增加 1,而 `$inc: {"size":-1}` 则表示减少 1。这个操作确保了每次更新都是原子性的,不会影响其他文档,除非更新条件匹配。 - 请注意,$inc 只适用于数值类型的数据,并且只支持整数和浮点数。如果尝试对非数值键或字符串键使用 $inc,将会导致错误。 2. **$set (设置/覆盖操作)**: - $set 改变文档中指定键的值,如果键不存在则创建。在提供的代码片段中,通过 `findOne` 方法查询文档后,可以使用 `$set` 来添加或更新键值对。例如,`$set: {"ty": "new_value"}` 会直接覆盖 `ty` 字段的值,如果该字段还未存在,则会被创建。 - 如果你希望保留原有文档结构,仅更新指定键,而不是覆盖整个文档,$set 是一个非常有用的工具。 3. **$unset (移除操作)**: - $unset 可以删除文档中指定的键及其对应的值。例如,`$unset: {"size": 1}` 会移除 `size` 字段,即使其值为 0 也会被删除。 4. **$push (数组扩展操作)**: - 对于数组类型的键,$push 可以在数组末尾添加新元素。如 `db.collection.update({_id: _id}, {$push: {items: "new_item"}})`,会在 `items` 数组中追加 "new_item"。 5. **$pop (数组弹出操作)**: - $pop 可以从数组中弹出一个元素,可以选择从开头(`$pop: {field: 1}`)或结尾(`$pop: {field: -1}`)移除元素。 6. **$upsert (插入或更新操作)** - 如果文档不存在,$upsert 会先插入新的文档;如果文档存在,则执行正常的更新操作。这对于避免因找不到文档而导致的更新失败很有帮助。 在 MongoDB 中,每个修改器都有其特定用途,根据实际需求选择合适的修改器能提高数据库操作的效率和数据一致性。然而,需要注意的是,虽然这些修改器提供了强大的灵活性,但它们都遵循文档更新的事务原则,即要么全部成功,要么全部不执行,这保证了数据的一致性。在使用时,务必确保更新条件的精确性,以防止意外地更新到不期望的文档。

为什么会这样[user_mongo@nosql01 replicaset]$ cd /opt [user_mongo@nosql01 opt]$ ll total 0 drwxr-xr-x. 3 root root 25 Mar 16 17:08 servers drwxr-xr-x. 2 root root 51 Mar 16 17:10 software [user_mongo@nosql01 opt]$ tar -zxvf /opt/software/mongodb-linux-x86_64-rhel70-4.4.12.tgz -C /opt/servers/mongodb_demo/replicaset/ mongodb-linux-x86_64-rhel70-4.4.12/LICENSE-Community.txt tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/LICENSE-Community.txt: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/MPL-2 tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/MPL-2: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/README tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/README: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/THIRD-PARTY-NOTICES tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/THIRD-PARTY-NOTICES: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/bin/install_compass tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/bin/install_compass: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/bin/mongo tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/bin/mongo: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/bin/mongod tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/bin/mongod: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/bin/mongos tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/bin/mongos: Cannot open: No such file or directory tar: Exiting with failure status due to previous errors [user_mongo@nosql01 opt]$ tar -zcvf /opt/software/mongodb-linux-x86_64-rhel70-4.4.12.tgz -C /opt/servers/mongodb_demo/replicaset/ tar: Cowardly refusing to create an empty archive Try `tar --help' or `tar --usage' for more information.

2023-06-01 上传