## 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
- 第一章 開發環境配置