-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate.js
46 lines (41 loc) · 1.21 KB
/
update.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
45
46
console.log("hello update");
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 updateDocuments = (db, cb) => {
const collection = db.collection("studentsInfo");
// collection.updateMany(
collection.updateOne(
{ name: "youling" },
{ $set: { age: 15 } },
(err, result) => {
if (err) {
console.log("err", err);
return;
}
console.log("result:", result);
console.log("result.updatedCount: ", result.updatedCount);
console.log("result.result.n : ", result.result.n);
console.log(
`Updated the document with the field a equal to ${result.result.n}`
);
cb(result);
}
);
};
client.connect(err => {
if (err) {
console.log("connect to server failed!");
return;
}
console.log("Connected successfully to mongoDB server!");
const db = client.db(dbName);
updateDocuments(db, () => {
client.close();
});
});