mangodb显示数据库数据

api.js

const bugs = require('../model/db').bugs;
const ideas = require('../model/db').ideas;
const express = require('express');
const router = express.Router(http://www.amjmh.com/v/);

router.get('/api/bugs/find', (function (req, res) {
bugs.aggregate([{$sort: {create_date: -1}}] , function (err, results) {
if(err){
console.log("Error:" + err);
res.send(err)
}else{
console.log(JSON.stringify(results));
res.header('Access-Control-Allow-Origin', '*');
res.send(JSON.stringify(results))
}
})
}));

router.post('/api/bugs/findSelf', (function (req, res) {
bugs.aggregate([{$match: {"name": req.body.name}},{$sort: {create_date: -1}}] , function (err, results) {
console.log(req.body.name);
if(err){
console.log("Error:" + err);
res.send(err)
}else{
res.header('Access-Control-Allow-Origin', '*');
res.send(results)
}
})
}));

router.get('/api/bugs/find_group', (function (req, res) {
bugs.aggregate([{$group : {_id: "$name", _sum: {$sum : 1}}},{$sort: {_sum:-1}}] , function (err, results) {
if(err){
console.log("Error:" + err);
res.send(err)
}else{
console.log(JSON.stringify(results));
res.header('Access-Control-Allow-Origin', '*');
res.send(JSON.stringify(results))
}
})
}));

router.post('/api/bugs/delete', (function (req, res) {
bugs.deleteOne({"desc": req.body.desc}, function (err,results) {
console.log(req.body.desc);
if(err){
console.log("Error:" + err);
res.send(err)
}else{
res.header('Access-Control-Allow-Origin', '*');
res.send(results)
}
})
}));

router.get('/api/ideas/find', (function (req, res) {
ideas.aggregate([{$sort: {create_date: -1}}] , function (err, results) {
if(err){
console.log("Error:" + err);
res.send(err)
}else{
console.log(JSON.stringify(results));
res.header('Access-Control-Allow-Origin', '*');
res.send(JSON.stringify(results))
}
})
}));

router.post('/api/ideas/findSelf', (function (req, res) {
ideas.aggregate([{$match: {"name": req.body.name}},{$sort: {create_date: -1}}] , function (err, results) {
console.log(req.body.name);
if(err){
console.log("Error:" + err);
res.send(err)
}else{
res.header('Access-Control-Allow-Origin', '*');
res.send(results)
}
})
}));

router.get('/api/ideas/find_group', (function (req, res) {
ideas.aggregate([{$group : {_id: "$name", _sum: {$sum : 1}}},{$sort: {_sum:-1}}] , function (err, results) {
if(err){
console.log("Error:" + err);
res.send(err)
}else{
console.log(JSON.stringify(results));
res.header('Access-Control-Allow-Origin', '*');
res.send(JSON.stringify(results))
}
})
}));

router.post('/api/ideas/delete', (function (req, res) {
ideas.deleteOne({"desc": req.body.desc}, function (err,results) {
console.log(req.body.desc);
if(err){
console.log("Error:" + err);
res.send(err)
}else{
res.header('Access-Control-Allow-Origin', '*');
res.send(results)
}
})
}));

module.exports = router;
---------------------

posted @ 2019-08-09 09:58  水至清明  阅读(213)  评论(0编辑  收藏  举报