mongodb update 字符 操作
下面常用的update操作,用mongodb2.6.3版本测试的,官方发布的稳定版本2.4,建议用稳定版。 一,upsert表示如果有数据就不插入,没数据就插入 1,命令行下 db.peoples.update( //查找name等于tank的用户... { name: "tank" },... {... "_id":1,... name: "An
下面常用的update操作,用mongodb2.6.3版本测试的,官方发布的稳定版本2.4,建议用稳定版。
一,upsert表示如果有数据就不插入,没数据就插入
1,命令行下
> db.peoples.update( //查找name等于tank的用户 ... { name: "tank" }, ... { ... "_id":1, ... name: "Andy", ... rating: 10, ... score: 10 ... }, ... { upsert: true } //如果没有就插入 ... ); WriteResult({ "nMatched" : 0, "nUpserted" : 1, "_id" : 1 }) > db.peoples.find(); { "_id" : 1, "name" : "Andy", "rating" : 10, "score" : 10 } > db.peoples.update( ... { _id: 1 }, ... { ... "_id":1, ... name: "Andy", ... rating: 10, ... score: 10 ... }, ... { upsert: true } ... ); WriteResult({ "nMatched" : 1, "nUpserted" : 0 }) //有匹配数据就不做插入操作 > db.peoples.find(); { "_id" : 1, "name" : "Andy", "rating" : 10, "score" : 10 }
2,php upsert操作
$collection->update( array("name" => "zhang"), array("_id"=>2,"name"=>"tank","rating"=>10,"score"=>10), array("upsert" => true) ); print_r($collection->findOne());
二,$set 替换值
1,命令行下操作
> db.peoples.find(); { "_id" : 1, "name" : "Andy", "rating" : 10, "score" : 10 } > db.peoples.update( ... { _id: 1 }, ... { ... $set: { rating: 18 } ... } ... ); WriteResult({ "nMatched" : 1, "nUpserted" : 0 }) > db.peoples.find(); { "_id" : 1, "name" : "Andy", "rating" : 18, "score" : 10 }
2,php $set操作
$where = array("_id"=>1); $param = array('$set'=>array("score"=>"100")); //注意此处的set必须为 单引号 $collection->update($where,$param); print_r($collection->findOne());
三,$inc如果没有对应对段就直接赋值,如果有在原来的值上加上该值
1,命令行下操作
> db.peoples.find(); { "_id" : 1, "name" : "Andy", "rating" : 28, "score" : 10 } > db.peoples.update( ... { _id: 1 }, ... { ... $inc: { age: 30 } ... } ... ); WriteResult({ "nMatched" : 1, "nUpserted" : 0 }) > db.peoples.find(); { "_id" : 1, "age" : 30, "name" : "Andy", "rating" : 28, "score" : 10 } //第一次,加了一个字段 > db.peoples.update( ... { _id: 1 }, ... { ... $inc: { age: 30 } ... } ... ); WriteResult({ "nMatched" : 1, "nUpserted" : 0 }) > db.peoples.find(); { "_id" : 1, "age" : 60, "name" : "Andy", "rating" : 28, "score" : 10 } //第二次,发现有这个字段就把值加上去了
2,php $inc操作
$where = array("_id"=>1); $param = array('$inc'=>array("age"=>30)); $collection->update($where,$param); print_r($collection->findOne());
四,$unset删除字段
1,命令行下操作
> db.peoples.find(); { "_id" : 1, "age" : 120, "name" : "Andy", "rating" : 28, "score" : 10 } > db.peoples.update( ... { _id: 1 }, ... { ... $unset: { age: ""} //删除age字段 ... } ... ); WriteResult({ "nMatched" : 1, "nUpserted" : 0 }) > db.peoples.find(); { "_id" : 1, "name" : "Andy", "rating" : 28, "score" : 10 } >
2,php $unset操作
$where = array("_id"=>1); $param = array('$unset'=>array("score"=>"")); $collection->update($where,$param); print_r($collection->findOne());
五,$rename修改字段名称
1,命令行下操作
> db.peoples.find(); { "_id" : 1, "name" : "Andy", "rating" : 28 } > db.peoples.update( ... { _id: 1 }, ... { $rename: { "name": "firstname" } } //将name改成firstname ... ); WriteResult({ "nMatched" : 1, "nUpserted" : 0 }) > db.peoples.find(); { "_id" : 1, "firstname" : "Andy", "rating" : 28 }
2,php $rename 操作
$where = array("_id"=>1); $param = array('$rename'=>array("firstname"=>"name")); //将firstname改成name $collection->update($where,$param); print_r($collection->findOne());
六,multi更新多条数据
1,命令行下操作
> db.peoples.find(); { "_id" : 1, "name" : "Andy", "rating" : 28 } { "_id" : 2, "name" : "zhang", "rating" : 3, "score" : 5 } { "_id" : 3, "name" : "hao", "rating" : 30, "score" : 5 } > db.peoples.update( ... { rating: { $gt: 15 } }, ... { $inc: { score: 10 } }, ... { multi: true } ... ); WriteResult({ "nMatched" : 2, "nUpserted" : 0 }) > db.peoples.find(); { "_id" : 1, "name" : "Andy", "rating" : 28, "score" : 10 } //这条数据更新了 { "_id" : 2, "name" : "zhang", "rating" : 3, "score" : 5 } { "_id" : 3, "name" : "hao", "rating" : 30, "score" : 15 } //这条数据更新了
2,php multi操作
$where = array("rating"=>array('$gt'=>10)); $param = array('$set'=>array("name"=>"tank1","rating"=>50)); $ismore = array("multiple" => true); $collection->update($where,$param,$ismore);
版本不一样,功能多少会不一样,例如,我用的版本是2.6.3,$min和$max就用不了,会报"errmsg" : "Invalid modifier specified $min",希望mongodb出一个稳定高版本。
原文地址:mongodb update 字符 操作, 感谢原作者分享。

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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





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

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

It is impossible to view MongoDB password directly through Navicat because it is stored as hash values. How to retrieve lost passwords: 1. Reset passwords; 2. Check configuration files (may contain hash values); 3. Check codes (may hardcode passwords).

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

Detailed explanation of MongoDB efficient backup strategy under CentOS system This article will introduce in detail the various strategies for implementing MongoDB backup on CentOS system to ensure data security and business continuity. We will cover manual backups, timed backups, automated script backups, and backup methods in Docker container environments, and provide best practices for backup file management. Manual backup: Use the mongodump command to perform manual full backup, for example: mongodump-hlocalhost:27017-u username-p password-d database name-o/backup directory This command will export the data and metadata of the specified database to the specified backup directory.

Encrypting MongoDB database on a Debian system requires following the following steps: Step 1: Install MongoDB First, make sure your Debian system has MongoDB installed. If not, please refer to the official MongoDB document for installation: https://docs.mongodb.com/manual/tutorial/install-mongodb-on-debian/Step 2: Generate the encryption key file Create a file containing the encryption key and set the correct permissions: ddif=/dev/urandomof=/etc/mongodb-keyfilebs=512

Sorting index is a type of MongoDB index that allows sorting documents in a collection by specific fields. Creating a sort index allows you to quickly sort query results without additional sorting operations. Advantages include quick sorting, override queries, and on-demand sorting. The syntax is db.collection.createIndex({ field: <sort order> }), where <sort order> is 1 (ascending order) or -1 (descending order). You can also create multi-field sorting indexes that sort multiple fields.

MongoDB and relational database: In-depth comparison This article will explore in-depth the differences between NoSQL database MongoDB and traditional relational databases (such as MySQL and SQLServer). Relational databases use table structures of rows and columns to organize data, while MongoDB uses flexible document-oriented models to better suit the needs of modern applications. Mainly differentiates data structures: Relational databases use predefined schema tables to store data, and relationships between tables are established through primary keys and foreign keys; MongoDB uses JSON-like BSON documents to store them in a collection, and each document structure can be independently changed to achieve pattern-free design. Architectural design: Relational databases need to pre-defined fixed schema; MongoDB supports
