mongodb 修改器学习
1. 执行update相关操作都可以使用修改器. 2. 修改器是为了不整个大文档来替换修改, 而是修改局部 3. 详情: 3.1 $inc (自增) 为增加某个字段的数值 例子: 原文档如下: { "_id" : ObjectId("4b253b067525f35f94b60a31"), "url" : "www.example.com", "pageview
1. 执行update相关操作都可以使用修改器.
2. 修改器是为了不整个大文档来替换修改, 而是修改局部
3. 详情:
3.1 $inc (自增)
为增加某个字段的数值
例子:
原文档如下:
{ "_id" : ObjectId("4b253b067525f35f94b60a31"), "url" : "www.example.com", "pageviews" : 52 }
执行修改器 $inc
db.test.update({url:’www.example.com’},{$inc:{pageviews: 1}})
表示为pageviews这个 key 的值 加上1. 后面是对应要加上的值.
注意, $inc中, 字段对应的值只能是 数字. 但是可以是 正数也可以是负数. 如果是负数, 就是执行一个减法操作.
3.2 $set (设置)
修改某个字段的值为指定的值. 这里的修改是与字段类型无关的. 你可以把一个string修改成long或者一个对象都行.
例子:
原文档如下:
{ "_id" : ObjectId("4b253b067525f35f94b60a31"), "name" : "joe", "age" : 30, "sex" : "male", "location" : "Wisconsin" }
修改性别, 变成false (String -> Boolean)
执行修改器 $set
db.test.update({name:'joe'}, {$set:{sex:false}})
表示将 name为joe的这个对象的 sex修改为false
3.3 $unset (删除字段)
这个修改器是为了删除某个指定的字段及其值.
例子:
原文档如下:
{ "_id" : ObjectId("4f9105377522000000006be0"), "howlong" : 6, "lovePerson" : "zhanying", "person" : "jiacheo" }
把howlong删除
执行修改器 $unset
db.test.update({person:'jiacheo'},{$unset:{howlong:1}})
后面的1没具体意义, 相当于确认
3.4 $push (把一个数据放到数组里面)
例子:
原文档如下:
{ "_id" : ObjectId("4f9105377522000000006be0"), "lovePerson" : "zhanying", "person" : "jiacheo", "supporters" : [ "gongjin" ] }
执行修改器, 增加一个人到supporters里面
db.test.update({person:'jiacheo'},{$push:{supporters:'ziming'}})
这时候变成:
{ "_id" : ObjectId("4f9105377522000000006be0"), "lovePerson" : "zhanying", "person" : "jiacheo", "supporters" : [ "gongjin", "ziming" ] }
若果再执行一遍上面的代码, 那么数组还是会加上同样的名称, 如果你需要排同, 可以用后面这个修改器
3.5 $addToSet (将一个对象放到集合里面. 集合里面不会出现两个重复的一模一样的对象)
例子:
原文档如下:
{ "_id" : ObjectId("4f9105377522000000006be0"), "lovePerson" : "zhanying", "person" : "jiacheo", "supporters" : [ "gongjin", "ziming" ] }
执行修改器 $addToSet
db.test.update({person:'jiacheo'},{$addToSet:{supporters:'ziming'}})
文档内容没有改变..
以上两个修改器还可以配合 $each, 把一个数据里的数据append到另一个数组上去(后者会排重)
比如:
db.test.update({person:'jiacheo'}, {$addToSet:{supporters:{$each: ["gongjin","ziming","liuxun","feidu"] }}})
执行后, 结果如下:
{ "_id" : ObjectId("4f9105377522000000006be0"), "lovePerson" : "zhanying", "person" : "jiacheo", "supporters" : ["gongjin", "ziming", "feidu", "liuxun" ] }
3.6 $pop 队列出列 (按数组索引顺序出列(正序或者倒序))
例子:
原文档如下:
{ "_id" : ObjectId("4f9105377522000000006be0"), "lovePerson" : "zhanying", "person" : "jiacheo", "supporters" : ["gongjin", "ziming", "feidu", "liuxun" ] }
执行修改器 $pop
db.test.update({person:'jiacheo'},{$pop:{supporters:1}})
修改后变为
{ "_id" : ObjectId("4f9105377522000000006be0"), "lovePerson" : "zhanying", "person" : "jiacheo", "supporters" : [ "gongjin", "ziming", "feidu" ] }
value 值为1 表示数组索引靠后的先出列, 也就是后进先出, 相当于出栈的概念. value值为-1表示数组索引考前的先出列, 也就是先进先出, 相当于一个FIFO的队列
3.7 $pull (符合条件的元素出列)
与上一个相比, 这个修改器会把符合条件的元素从数组中删除, 而不是简单的按照索引值的大小来处理.
原文档如下:
{ "_id" : ObjectId("4f9105377522000000006be0"), "lovePerson" : "zhanying", "person" : "jiacheo", "supporters" : [ "gongjin", "ziming", "feidu" ] }
执行修改器 $pull
db.test.update({person:'jiacheo'},{$pull:{supporters: 'gongjin'}})
修改后:
{ "_id" : ObjectId("4f9105377522000000006be0"), "lovePerson" : "zhanying", "person" : "jiacheo", "supporters" : [ "ziming", "feidu" ] }
可以见, gongjin从supporters中被remove掉了.
这里所有被匹配的元素都会被出列
原文地址:mongodb 修改器学习, 感谢原作者分享。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



After several pre-releases, the KDE Plasma development team unveiled version 6.0 of its desktop environment for Linux and BSD systems on 28 February, using the Qt6 framework for the first time. KDE Plasma 6.1 now comes with a number of new features t

The Fitbit Ace LTE was officially launched in May, but is currently only available in the US. The smartwatch is aimed specifically at children, who can receive rewards for games through a more active lifestyle, while parents can always monitor their

Table of Contents Astar Dapp Staking Principle Staking Revenue Dismantling of Potential Airdrop Projects: AlgemNeurolancheHealthreeAstar Degens DAOVeryLongSwap Staking Strategy & Operation "AstarDapp Staking" has been upgraded to the V3 version at the beginning of this year, and many adjustments have been made to the staking revenue rules. At present, the first staking cycle has ended, and the "voting" sub-cycle of the second staking cycle has just begun. To obtain the "extra reward" benefits, you need to grasp this critical stage (expected to last until June 26, with less than 5 days remaining). I will break down the Astar staking income in detail,

Samsung is offering Galaxy Watch users a chance to experience new health and sleep tracking features through the One UI 6 Watch beta program. This was initially available for Galaxy Watch6 (which smashed our display brightness test) and Watch6 Classi

.NET 4.0 is used to create a variety of applications and it provides application developers with rich features including: object-oriented programming, flexibility, powerful architecture, cloud computing integration, performance optimization, extensive libraries, security, Scalability, data access, and mobile development support.

This article describes how to build a highly available MongoDB database on a Debian system. We will explore multiple ways to ensure data security and services continue to operate. Key strategy: ReplicaSet: ReplicaSet: Use replicasets to achieve data redundancy and automatic failover. When a master node fails, the replica set will automatically elect a new master node to ensure the continuous availability of the service. Data backup and recovery: Regularly use the mongodump command to backup the database and formulate effective recovery strategies to deal with the risk of data loss. Monitoring and Alarms: Deploy monitoring tools (such as Prometheus, Grafana) to monitor the running status of MongoDB in real time, and

This article introduces how to configure MongoDB on Debian system to achieve automatic expansion. The main steps include setting up the MongoDB replica set and disk space monitoring. 1. MongoDB installation First, make sure that MongoDB is installed on the Debian system. Install using the following command: sudoaptupdatesudoaptinstall-ymongodb-org 2. Configuring MongoDB replica set MongoDB replica set ensures high availability and data redundancy, which is the basis for achieving automatic capacity expansion. Start MongoDB service: sudosystemctlstartmongodsudosys

PiNetwork is about to launch PiBank, a revolutionary mobile banking platform! PiNetwork today released a major update on Elmahrosa (Face) PIMISRBank, referred to as PiBank, which perfectly integrates traditional banking services with PiNetwork cryptocurrency functions to realize the atomic exchange of fiat currencies and cryptocurrencies (supports the swap between fiat currencies such as the US dollar, euro, and Indonesian rupiah with cryptocurrencies such as PiCoin, USDT, and USDC). What is the charm of PiBank? Let's find out! PiBank's main functions: One-stop management of bank accounts and cryptocurrency assets. Support real-time transactions and adopt biospecies
