## updateOne
~~~
updateOne(oldValue,newValue)
~~~
1.update one document
~~~
const MongoClient = require('mongodb').MongoClient;
var url = 'mongodb://localhost:27017/';
MongoClient.connect(url, {
useNewUrlParser: true
}, (err, client) => {
if (err) throw err;
var test = client.db('test');
var myquery = {name:"chengchao"};
var newValues = {$set:{name:"jake",address:"江山如画"}}
test.collection('douban').updateOne(myquery,newValues,(err,res)=>{
if(err) throw err;
console.log('one document updated');
client.close()
})
})
~~~
## 2.Update Only Specific Fields
~~~
const MongoClient = require('mongodb').MongoClient;
var url = 'mongodb://localhost:27017/';
MongoClient.connect(url, {
useNewUrlParser: true
}, (err, client) => {
if (err) throw err;
var test = client.db('test');
var myquery = {name:"jake"};
var newValues = {$set:{name:"jake-love"}}
test.collection('douban').updateOne(myquery,newValues,(err,res)=>{
if(err) throw err;
console.log('one document updated');
client.close()
})
})
~~~
## 3.update Many Documents
~~~
const MongoClient = require('mongodb').MongoClient;
var url = 'mongodb://localhost:27017/';
MongoClient.connect(url, {
useNewUrlParser: true
}, (err, client) => {
if (err) throw err;
var test = client.db('test');
var myquery = {name:/^j/};
var newValues = {$set:{address:"极客营"}};
test.collection('douban').updateMany(myquery,newValues,(err,res)=>{
if(err) throw err;
console.log('one document updated');
client.close()
})
})
~~~
- MongoDB
- 第一章开发环境配置
- 第二章 基础操作
- 2-1 create-collection
- 2-2 collection-insert
- 2-3 find
- 2-4 query
- 2-5 sort排序
- 2-6 delete
- 2-7 drop-collection
- 2-8 update
- 2-9 limit
- 2-10 join
- 2-10-1 返回json给前台
- 2-11 ObjectId
- 第三章 数据库封装
- 3-0 数据库封装思路
- 3-1 单例
- 3-2 增加数据的执行时间
- 3-1-1 查询耗时
- 3-1-2 数据连接示例
- 3-3 简单封装
- 3-4 二次封装
- 3-5 结合art-template使用
- 3-6 数据库封装终极
- Redis
- 第一章 开发环境配置