-
Notifications
You must be signed in to change notification settings - Fork 0
/
find.js
84 lines (75 loc) · 2.1 KB
/
find.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
console.log("find function!");
const MongoDB = require("mongodb");
const MongoClient = MongoDB.MongoClient;
const url = "mongodb://localhost:27017";
// const dbName = "xuesheng";
const dbName = "shici";
const client = new MongoClient(url, {
useNewUrlParser: true,
useUnifiedTopology: true
});
const findDocuments = (db, cb) => {
const collection = db.collection("jrsc");
collection.find({ type: "INSERT_JR_SHICI" }).toArray((err, result) => {
if (err) {
console.log("err", err);
}
console.log("result:", result);
console.log(
`show all documents from collection about ${result.length} items!`
);
cb(result);
});
};
const updateDocument = (db, cb) => {
const collection = db.collection("jrsc");
collection.updateMany(
{ type: "INSERT_SHICI" },
{ $set: { type: "INSERT_JR_SHICI" } },
(err, result) => {
if (err) {
console.log("err", err);
}
console.log("result:", result);
cb(result);
}
);
};
client.connect(err => {
if (err) {
console.log("err", err);
return;
}
console.log("Connected successfully to server!");
const db = client.db(dbName);
findDocuments(db, () => {
client.close();
});
// updateDocument(db, () => {
// client.close();
// });
});
const findDocumentsWithFilter = (db, filter, cb) => {
const collection = db.collection("studentsInfo");
collection.find(filter).toArray((err, result) => {
if (err) {
console.log("err", err);
}
console.log("result:", result);
console.log(
`show all documents from collection about ${result.length} items!`
);
cb(result);
});
};
// client.connect(err => {
// if (err) {
// console.log("err", err);
// return;
// }
// console.log("Connected successfully to server!");
// const db = client.db(dbName);
// findDocumentsWithFilter(db, { age: 27, sex: "male" }, () => {
// client.close();
// });
// });