为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

HUH函数 2021-08-20 18:25:41
当我在 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    },   
查看完整描述

2 回答

?
largeQ

TA贡献2039条经验 获得超7个赞

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)

})


查看完整回答
反对 回复 2021-08-20
?
慕少森

TA贡献2019条经验 获得超9个赞

这对我有用吗


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));

  }


}

但我无法弄清楚它为什么工作以及我以前的代码有什么问题


查看完整回答
反对 回复 2021-08-20
  • 2 回答
  • 0 关注
  • 167 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信