-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdelete.js
44 lines (39 loc) · 1.04 KB
/
delete.js
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
console.log("hello delete");
const MongoDB = require("mongodb");
const MongoClient = MongoDB.MongoClient;
const url = "mongodb://localhost:27017";
const dbName = "xuesheng";
const client = new MongoClient(url, {
useNewUrlParser: true,
useUnifiedTopology: true
});
const deleteDocument = (db, cb) => {
const collection = db.collection("studentsInfo");
// collection.deleteMany(
collection.deleteOne(
{
age: 27,
sex: "male"
},
(err, result) => {
if (err) {
console.log("err", err);
}
console.log("result :", result);
console.log("result.deletedCount: ", result.deletedCount);
console.log("result.result.n: ", result.result.n);
cb(result);
}
);
};
client.connect(err => {
if (err) {
console.log("err", err);
return;
}
console.log("成功连接MongoDB数据库");
const db = client.db(dbName);
deleteDocument(db, () => {
client.close();
});
});