如何通过get方法返回mongodb管道的结果?

我有一个管道及其结果,我想通过一个表达的方法返回它,无论是否获取,我都知道通过套接字发送它是否更可取

这是我的文件pipeline.js:

function getModel(model) {
     model.aggregate([{
         $group: {
             _id: null,"price": {
                 $sum: "$price",}
         }
     }]).exec((e,d) => {
         return JSON.stringify(d)
     })
 }

 module.exports = getModel;

在model.js文件中,我将调用pipeline.js文件,并因此调用函数

model.js:

const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const getModel = require('./pipeline');
const mySchema = new Schema({
    user: {
        type: Schema.ObjectId,ref: 'User'
    },namepet: String,type_of_service: String,characteristic_of_pet: String,price: Number

});

const model = mongoose.model('Cites',mySchema);
here is the function-> getModel(model);

module.exports = model;

它对我有用,因为我想要的问题是我必须通过get方法发送它的结果,而我不知道该怎么做

如何通过get方法返回mongodb管道的结果?

如何通过get方法发送指示图像红色箭头的结果?

WOXP111 回答:如何通过get方法返回mongodb管道的结果?

var express = require('express');
var app = express();


function getModel(model) {
  model.aggregate([{
    $group: {
      _id: null,"price": {
        $sum: "$price",}
    }
  }]).exec((e,d) => {
    return JSON.stringify(d)
  })
}
app.get('/',function(req,res) {
  console.log('marhaba');
  res.send(getModel( ** Model ** ))) //== > here call the getModel function
});


app.listen(3000,function() {
  console.log("Working on port 3000");
});

本文链接:https://www.f2er.com/2707993.html

大家都在问