我需要获取机器名称为“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
}