聚合查询在mongodb中运行,而不在mongoose中运行

用户名

我有一个myCollection集合,其中包含成员数组和位数:

{
   name : String,
   members: [{status : Number, memberId : {type: Schema.Types.ObjectId, ref: 'members'}],
   nbPlaces : Number
}

我有这个数据

"_id" : ObjectId("5e83791eb49ab07a48e0282b")
   "members" : [ 
        {
            "status" : 1,
            "_id" : ObjectId("5e83791eb49ab07a48e0282c"),
            "memberId" : ObjectId("5e7dbf5b257e6b18a62f2da9")
        }, 
        {
            "status" : 2,
            "_id" : ObjectId("5e837944b49ab07a48e0282d"),
            "memberId" : ObjectId("5e7de2dbe027f43adf678db8")
        }
    ],
  "nbPlaces": 5

我做一个$匹配由添加的字段$ addFields,但这个结果返回MongoDB中,而不是在猫鼬,我怎样才能在猫鼬的结果?谢谢。

猫鼬版本:^ 5.9.4“

myCollection.aggregate([
  {
    $project: {
      nbMembers: {
        $size: {
          $filter: {
            input: "$members",
            as: "member",
            cond: { $eq: ["$$member.status", 1] },
          },
        },
      },

      nbPlaces: 1,
    },
  },
  {
    $addFields: {
      charged: {
        $cond: {
          if: { $eq: [{ $subtract: ["$nbPlaces", "$nbMembers"] }, 0] },
          then: true,
          else: false,
        },
      },
    },
  },
  { $match: { _id: ObjectId("5e83791eb49ab07a48e0282b"), charged: false } },
]);
苏莱曼·萨

我可以用猫鼬得到这样的结果:

const mongoose = require("mongoose");
const ObjectId = mongoose.Types.ObjectId;

router.get("/test", async (req, res) => {
  const result = await myCollection.aggregate([
    {
      $project: {
        nbMembers: {
          $size: {
            $filter: {
              input: "$members",
              as: "member",
              cond: { $eq: ["$$member.status", 1] },
            },
          },
        },
        nbPlaces: 1,
      },
    },
    {
      $addFields: {
        charged: {
          $cond: {
            if: { $eq: [{ $subtract: ["$nbPlaces", "$nbMembers"] }, 0] },
            then: true,
            else: false,
          },
        },
      },
    },
    { $match: { _id: ObjectId("5e83791eb49ab07a48e0282b"), charged: false } },
  ]);

  res.send(result);
});

得到以下结果:

[
    {
        "_id": "5e83791eb49ab07a48e0282b",
        "nbPlaces": 5,
        "nbMembers": 1,
        "charged": false
    }
]

请检查您的myCollection模式是否与您的数据匹配,我使用了以下方法:

const mongoose = require("mongoose");

const schema = new mongoose.Schema({
  name: String,
  members: [
    {
      status: Number,
      memberId: { type: mongoose.Schema.Types.ObjectId, ref: "members" },
    },
  ],
  nbPlaces: Number,
});

module.exports = mongoose.model("myCollection", schema);

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

MySQL查询不在cronjob中运行

是否可以在Firestore中运行聚合查询?

SQL查询从IDE运行,但不在phpmyadmin中运行

Mongodb 聚合在终端中运行,但在使用 mongoose 在 node.js 中运行时,它返回“APIError:参数必须是聚合管道运算符”

MongoDB聚合查询与不在

查询在 myphpsql 页面中运行但不在 php 中

MongoDB聚合中的Mongoose虚拟

如何在Java中运行MongoDB查询

使用不在MSSQL中运行的变量选择查询

选择查询在“ Visual Studio”中运行,但不在浏览器中运行

For 循环不在 Java 中运行

git不在PowerShell中运行

PhantomJS() 不在 pyCharm 中运行

onCreate不在服务中运行

RapidMiner中的MongoDB聚合查询

INSERT 查询在 SQL 视图上运行,但不在 Visual Basic 中运行

查询在MySql工作台中运行,但不在Java eclipse中运行

如何在运行Mongodb的Mongoose中执行脚本?

在Codeigniter中运行查询

C#问题获取LINQ查询结果-查询在LINQpad中运行,但不在Visual Studio中运行

mongoose 在 MongoDB 中的 $project 查询

如何在MongoDB中对聚合查询结果进行分页并获得文档总数(Node.js + Mongoose)?

XSLT在anypoint studio中运行,但不在CloudHub中运行

ZSH命令在Shell中运行,但不在脚本中运行

在R中运行,但不在Rscript中运行

脚本在ISE中运行,但不在Powershell中运行

命令在终端中运行,但不在Bash脚本中运行

在intelliJ中运行但不在cmd中运行的Java应用

opencv在python中运行,但不在spyder中运行