记录mongoose aggregate $lookup 不生效问题

nodejs mongoose aggregate $lookup from 必须为数据库名称

定义模型itemIn

// 1. 引入mongoose
const mongoose = require('mongoose');
// 2. 实例化Schema
const Schema = mongoose.Schema;

// 3. 创建Schema
const ItemInSchema = new Schema(
    {
        operator: {
            type: Schema.Types.ObjectId,
            ref: 'users' // 跟哪一个表进行关联
        }, 
        total: {
            type: Number
            // min: 0
            // required: [false, '请填写收入金额']
        }, 
        ...
    },
    {timestamps: true}
);


module.exports = ItemIn = mongoose.model('itemIn', ItemInSchema);

复制代码

定义模型itemProject

// 1. 引入mongoose
const mongoose = require('mongoose');
// 2. 实例化Schema
const Schema = mongoose.Schema;
// 3. 创建Schema
const ItemProjectSchema = new Schema(
    {
        operator: {
            type: Schema.Types.ObjectId,
            ref: 'users' // 跟哪一个表进行关联
        },
        // 关联收支项目Id  [或者 账户id (涉及转账)]
        itemOutInId: {
            type: Schema.Types.ObjectId,
            ref: 'itemOutIn',
            // ref: 'itemOutIn', // 跟哪一个表进行关联
            required: [true, '请填写关联收支项目Id']
        },
        ...
    },
    {timestamps: true}
);

ItemProjectSchema.set('toObject', {virtuals: true});
ItemProjectSchema.set('toJSON', {virtuals: true});

module.exports = ItemProject = mongoose.model('itemProject', ItemProjectSchema);

复制代码

不生效的写法,原因是$lookupfrom字段对应的是数据库名称而不是model名称

解决方法:

进入数据库,查看实际存储的数据库名称

# 1.进入数据库安装目录的bin目录,输入mongo 回车
PS D:\tools\mongodb\bin> ./mongo

# 2.show dbs
> show dbs
admin     0.000GB
config    0.000GB
help-app  0.002GB
local     0.000GB
wxlt-api  0.000GB
# 3.use your-db-name
> use wxlt-api
# 4.show tables
> show tables
itemins
itemoutins
itemouts
itemprojects

复制代码
 const result = await ModelItemProject.aggregate([
        {
            $lookup: {
                from: 'itemIn', 
                localField: 'projectId', // field in the orders collection
                foreignField: '_id', // field in the items collection
                as: 'itemins'
            }
        },
  ]);
复制代码

据查 mongoose.model('itemIn', ItemInSchema) 对应的数据库名称为itemins

把上面的from: 'itemIn',改为from: 'itemins',即可生效

代码如下:

 const result = await ModelItemProject.aggregate([
        {
            $lookup: {
                from: 'itemins', // 数据库名称
                localField: 'projectId', // field in the orders collection
                foreignField: '_id', // field in the items collection
                as: 'itemins'
            }
        },
        {
            $lookup: {
                from: 'itemouts',
                localField: 'projectId', // field in the orders collection
                foreignField: '_id', // field in the items collection
                as: 'itemouts'
            }
        },

        {
            $match: queryParamsFileds
            // $match: {$or: [{type: '1'}, {type: '2'}]}
            // $match: {type: {$in: ['1', '2']}} // 收入、支出
        },
        {
            $unwind: {path: '$itemins', preserveNullAndEmptyArrays: true}
        },
        {
            $unwind: {path: '$itemouts', preserveNullAndEmptyArrays: true}
        },
        {
            $group: {
                _id: '$itemOutInId',
                countMoney: {$sum: '$money'},
                count: {$sum: 1}
            }
        },
        {
            $project: {
                _id: 0,
                itemOutInId: '$_id',
                count: '$count',
                value: '$countMoney'
            }
        },
        {
            $lookup: {
                from: 'itemoutins',
                localField: 'itemOutInId', // field in the orders collection
                foreignField: '_id', // field in the items collection
                as: 'itemOutIn'
            }
        },
        {
            $unwind: {path: '$itemOutIn', preserveNullAndEmptyArrays: true}
        }
    ]);
复制代码

猜你喜欢

转载自juejin.im/post/7036616747893391391