首页 > 解决方案 > 在猫鼬中填充条件返回 null

问题描述

我需要获取机器名称为“CircuitPrinter”的所有生产结果

ProductionResult 架构:

import { Schema, model } from 'mongoose';

const schemaOptions = {
    timestamps: { createdAt: 'created_at', updatedAt: false },
};

const productionResultSchema = new Schema({
    machineId: { type: Schema.Types.ObjectId, ref: 'Machine' },
    operatorId: { type: Schema.Types.ObjectId, ref: 'Operator' },
    orderId: { type: Schema.Types.ObjectId, ref: 'Order' },
    elementId: { type: Schema.Types.ObjectId, ref: 'Element' },
}, schemaOptions);

export default model('ProductionResult', productionResultSchema);

机器架构

import { Schema, model } from 'mongoose';

const schemaOptions = {
    timestamps: { createdAt: 'created_at', updatedAt: 'updated_at' },
};

const machineSchema = new Schema({
    name: { type: String },
    status: { type: String },
    date: { type: Date },
    amount: { type: Number },
}, schemaOptions);

export default model('Machine', machineSchema);

服务功能:

async getBar() {
        return ProductionResult.find()
            .populate({
                path: 'machineId',
                match: { name: 'CircuitPrinter' },
            });
    }

但是作为响应,我得到了带有 null 的 machineId 没有match我得到完整机器数据的正确响应。我的条件有什么问题。具有CircuitPrinter名称的机器存在于数据库中。

{
    "_id": "608c532e4051960ef05176f4",
    "machineId": null,
    "elementId": "608c4e914051960ef05176cc",
    "operatorId": "608c4e914051960ef05176d2",
    "orderId": "608c4e914051960ef05176eb",
    "created_at": "2021-04-30T18:57:50.771Z",
    "__v": 0
}

编辑:

示例数据机器文档:

{
    "_id": {
        "$oid": "608c4e914051960ef05176ed"
    },
    "name": "CircuitPrinter",
    "status": "Running",
    "__v": 0,
    "created_at": {
        "$date": "2021-04-30T18:38:09.326Z"
    },
    "updated_at": {
        "$date": "2021-04-30T18:38:09.326Z"
    }
}

样本数据 ProductionResult 文档:

{
    "_id": {
        "$oid": "608c532e4051960ef05176f4"
    },
    "machineId": {
        "$oid": "608c4e914051960ef05176f0"
    },
    "elementId": {
        "$oid": "608c4e914051960ef05176cc"
    },
    "operatorId": {
        "$oid": "608c4e914051960ef05176d2"
    },
    "orderId": {
        "$oid": "608c4e914051960ef05176eb"
    },
    "created_at": {
        "$date": "2021-04-30T18:57:50.771Z"
    },
    "__v": 0
}

标签: javascriptnode.jsmongodbmongoose

解决方案


推荐阅读