猿问

Mongodb 聚合在终端中运行,但在使用 mongoose 在 node.js 中运行时

当我在 mongo throw 终端中运行聚合时,它返回正确的数据,但是当我在代码中运行相同的聚合时,它返回一个错误。


我曾尝试仅使用 match 和 group 进行测试,但即使我使用了猫鼬函数,我仍然遇到相同的错误,但没有任何运气


我计划尝试改进我的综合比赛,但就目前而言,我需要运行它,而我在过去几天被困在这里。


"APIError: Arguments must be aggregate pipeline operators\n    at new ExtendableError (/vagrant/server/helpers/APIError.js:15:11)\n    at new APIError (/vagrant/server/helpers/APIError.js:31:5)\n    at app.use (/vagrant/config/express.js:64:22)\n    at Layer.handle_error (/vagrant/node_modules/express/lib/router/layer.js:71:5)\n    at trim_prefix (/vagrant/node_modules/express/lib/router/index.js:315:13)\n    at /vagrant/node_modules/express/lib/router/index.js:284:7\n    at Function.process_params (/vagrant/node_modules/express/lib/router/index.js:335:12)\n    at next (/vagrant/node_modules/express/lib/router/index.js:275:10)\n    at /vagrant/node_modules/express/lib/router/index.js:635:15\n    at Immediate.next (/vagrant/node_modules/express/lib/router/index.js:260:14)\n    at Immediate._onImmediate (/vagrant/node_modules/express/lib/router/index.js:635:15)\n    at runCallback (timers.js:706:11)\n    at tryOnImmediate (timers.js:676:5)\n    at processImmediate (timers.js:658:5)"

客户模型


const mongoose = require('mongoose');

const customerContactsSchema = require('./customerContacts.model');

const appSettings = require('../../config/appSettings');


/**

 * Customer Schema

 */

const CustomerSchema = new mongoose.Schema({

  avatar: {

    type: String,

    required: false

  },

  firstName: {

    type: String,

    required: false

  },

  lastName: {

    type: String,

    required: false

  },

  password: {

    type: String,

    required: false,

    select: false

  },

  address: {

    country: {

      type: mongoose.Schema.Types.ObjectId,

      ref: 'Country',

      required: false

    },

   

HUH函数
浏览 168回答 2
2回答

largeQ

Customer.aggregate([    {      $match: {        gender: new RegExp(`.*${'' || ''}.*`, 'i'),      }    }  ], { cursor: {} }) // ERROR IS HERE使用 mongoose 和 .aggregate,没有像在 .find 上那样的选项参数。而是试试这个Customer.aggregate([{  $match: {    gender: new RegExp(`.*${'' || ''}.*`, 'i'),  }}]).cursor()更新Customer.aggregate([{  $match: {    gender: new RegExp(`.*${'' || ''}.*`, 'i'),  }}]).exec((error, docs) => {    console.log(docs)    res.json(docs)})

慕少森

这对我有用吗async function search(req, res) {  try {    const customers = await Customer.aggregate([      {        $match: {          gender: 'male'        }      }      ]    )      .cursor({})      .exec();    customers.get(function(err, ress){      if (ress) {        res.json(Response.success(ress));      } else {        res.json(Response.success('klklk'));      }    });  } catch(e) {    res.status(httpStatus.INTERNAL_SERVER_ERROR) .json(Response.failure(e));  }}但我无法弄清楚它为什么工作以及我以前的代码有什么问题
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答