5d6d网 发表于 2017-12-16 06:02:37

[nodejs,mongodb,angularjs2]我的便利贴

1 // insert method  
2 var insertDocuments = function(db,data,col,callback) {
  
3   // Get the documents collection
  
4   var collection = db.collection(col);
  
5   // Insert some documents
  
6   collection.insertMany(, function(err, result) {
  
7         assert.equal(err, null);
  
8         callback(result);
  
9   });
  
10 }
  
11 // find documents
  
12 var findDocuments = function(db,col,callback,filterJson={}) {
  
13   // Get the documents collection
  
14   var collection = db.collection(col);
  
15   // Find some documents
  
16   collection.find(filterJson).toArray(function(err, docs) {
  
17         assert.equal(err, null);
  
18         docs.sort(function(a,b){return b.time.replace(/\-/g,'') - a.time.replace(/\-/g,'');});
  
19         callback(docs);
  
20   });
  
21 }
  
22 // remove docs
  
23 var removeDocument = function(db,removeJson,col,callback) {
  
24   // Get the documents collection
  
25   var collection = db.collection(col);
  
26   // or use methode: remove
  
27   collection.deleteOne(removeJson, function(err, result) {
  
28         assert.equal(err, null);
  
29         callback(result);
  
30   });
  
31 }
  
32 // update docs
  
33 var updateDocument = function(db,updateJson,setJson,col,callback) {
  
34   // Get the documents collection
  
35   var collection = db.collection(col);
  
36   // Update document
  
37   collection.updateMany(updateJson
  
38   , { $set: setJson }, function(err, result) {
  
39         assert.equal(err, null);
  
40         callback(result);
  
41   });
  
42 }
  
43 /*
  
44 note opp
  
45*/
  
46 router.get('/getAllNote',function(req, res, next) {
  
47   // Use connect method to connect to the server
  
48   MongoClient.connect(url,function(err, db) {
  
49         assert.equal(null, err);
  
50         findDocuments(db,'note',function(allDocs) {
  
51             db.close();
  
52             res.json(allDocs);
  
53         });
  
54   });
  
55 });
  
56
  
57 router.post('/getNote',function(req, res, next) {
  
58   // Use connect method to connect to the server
  
59   MongoClient.connect(url,function(err, db) {
  
60         assert.equal(null, err);
  
61         let filterJson = {'_id':new ObjectID(req.body._id)};
  
62         findDocuments(db,'note',function(allDocs) {
  
63             db.close();
  
64             res.json(allDocs);
  
65         },filterJson);
  
66   });
  
67 });
  
68
  
69 router.post('/addNote',function(req, res, next) {
  
70   // Use connect method to connect to the server
  
71   MongoClient.connect(url, function(err, db) {
  
72         insertDocuments(db,req.body,'note',function(result) {
  
73             db.close();
  
74             res.json(result);
  
75         });
  
76   });
  
77 });
  
78
  
79 router.post('/delNote',function(req, res, next) {
  
80   // Use connect method to connect to the server
  
81   MongoClient.connect(url, function(err, db) {
  
82         let removeJson = {'_id': new ObjectID(req.body._id)};
  
83         removeDocument(db,removeJson,'note',function(result) {
  
84             db.close();
  
85             res.json(result);
  
86         });
  
87   });
  
88 });
  
89
  
90 router.post('/editNote',function(req, res, next) {
  
91   // Use connect method to connect to the server
  
92   MongoClient.connect(url, function(err, db) {
  
93         let updateJson = {'_id':new ObjectID(req.body._id)};
  
94         let setJson = {'time':req.body.time,'content':req.body.content,'name':req.body.name,'sts':req.body.sts};
  
95         updateDocument(db, updateJson, setJson,'note',function(result) {
  
96             db.close();
  
97             res.json(result);
  
98         });
  
99   });
  
100 });
页: [1]
查看完整版本: [nodejs,mongodb,angularjs2]我的便利贴