nodejs渐入佳境[18]-mongodb-node增删查改

1
> npm install --save mongodb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
//mongodb 3以上写法
const MongoClient = require('mongodb').MongoClient;

//TodoApp是一个数据库名字,mongo中不需要创建数据库
MongoClient.connect('mongodb://localhost:27017/TodoApp',(err,client)=>{
if(err){
return console.log('Unable to connect to MongoDB server');
}
console.log('Connect to MongoDB server');
const db = client.db('TodoApp');

//插入数据
db.collection('Todos').insertOne({
text:'Something to do',
completed:false
},(err,result)=>{
if(err){
return console.log('unable to insert todo',err);
}
console.log(JSON.stringify(result.ops,undefined,2));
});
//关闭客户端
client.close();

});

id

插入一个对象的时候,会自动的产生一个随机的id。id包含了时间戳和机器识别码。
我们可以借助与mongo的方法,为我们产生一个随机数。

1
2
3
4
5
const {MongoClient,ObjectID} = require('mongodb');
var obj = new ObjectID();
console.log(obj);
//返回时间
console.log(obj.getTimestamp());

参考资料:
MongoDB Node.js Driver Documentation
node-mongodb-native

查询

原始数据:

1
2
3
4
5
6
7
8
9
10
11
12
[
{
"_id": "5bee9e3794c5f22eb989f6eb",
"text": "Something to do",
"completed": false
},
{
"_id": "5beeaf0a2d48eb2b7c90758f",
"text": "Something to do",
"completed": true
}
]

js代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
const {MongoClient,ObjectID} = require('mongodb');

MongoClient.connect('mongodb://localhost:27017/TodoApp',(err,client)=>{
if(err){
return console.log('Unable to connect to MongoDB server');
}
console.log('Connect to MongoDB server');
const db = client.db('TodoApp');

//返回所有document
db.collection('Todos').find().toArray().then((docs)=>{
console.log('Todos');
console.log(JSON.stringify(docs,undefined,2));
},(err)=>{
console.log('unable to fetch todos', err);
})

client.close();
});

执行代码返回:

1
2
3
4
5
6
7
8
9
10
11
12
[
{
"_id": "5bee9e3794c5f22eb989f6eb",
"text": "Something to do",
"completed": false
},
{
"_id": "5beeaf0a2d48eb2b7c90758f",
"text": "Something to do",
"completed": true
}
]

筛选

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
const {MongoClient,ObjectID} = require('mongodb');

MongoClient.connect('mongodb://localhost:27017/TodoApp',(err,client)=>{
if(err){
return console.log('Unable to connect to MongoDB server');
}
console.log('Connect to MongoDB server');
const db = client.db('TodoApp');
db.collection('Todos').find({completed:true}).toArray().then((docs)=>{
console.log('Todos');
console.log(JSON.stringify(docs,undefined,2));
},(err)=>{
console.log('unable to fetch todos', err);
})

client.close();

});

执行代码返回:

1
2
3
4
5
6
7
[
{
"_id": "5beeaf0a2d48eb2b7c90758f",
"text": "Something to do",
"completed": true
}
]

筛选id

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
const {MongoClient,ObjectID} = require('mongodb');

MongoClient.connect('mongodb://localhost:27017/TodoApp',(err,client)=>{
if(err){
return console.log('Unable to connect to MongoDB server');
}
console.log('Connect to MongoDB server');
const db = client.db('TodoApp');
db.collection('Todos').find({
new ObjectID('5beeaf0a2d48eb2b7c90758f')
}).toArray().then((docs)=>{
console.log('Todos');
console.log(JSON.stringify(docs,undefined,2));
},(err)=>{
console.log('unable to fetch todos', err);
})

client.close();

});

执行代码返回:

1
2
3
4
5
6
7
[
{
"_id": "5beeaf0a2d48eb2b7c90758f",
"text": "Something to do",
"completed": true
}
]

删除document

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
const {MongoClient,ObjectID} = require('mongodb');

MongoClient.connect('mongodb://localhost:27017/TodoApp',(err,client)=>{
if(err){
return console.log('Unable to connect to MongoDB server');
}
console.log('Connect to MongoDB server');
const db = client.db('TodoApp');
// db.collection('Todos').deleteMany({text:'mike'}); 删除全部

//删除一个
// db.collection('Todos').deleteOne({text:'mike'}).then((result)=>{
// console.log(result);
// });

//删除并返回对象
db.collection('Todos').findOneAndDelete({text:'mike'}).then((result)=>{
console.log(result);
});

client.close();

});

更新

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
const {MongoClient,ObjectID} = require('mongodb');

MongoClient.connect('mongodb://localhost:27017/TodoApp',(err,client)=>{
if(err){
return console.log('Unable to connect to MongoDB server');
}
console.log('Connect to MongoDB server');
const db = client.db('TodoApp');

//查找并更新
db.collection('Todos').findOneAndUpdate({text:'mike'},{
$set:{
completed:false //设置更改
}
},{
returnOriginal:false //返回的结果为更新后的值。
}).then((result)=>{
console.log(result); //打印跟新结果
});

client.close();
});