Javascript 在mongoose中填充条件返回null
我需要得到机器名为“CircuitPrinter”的所有生产结果 ProductionResult架构:Javascript 在mongoose中填充条件返回null,javascript,node.js,mongodb,mongoose,Javascript,Node.js,Mongodb,Mongoose,我需要得到机器名为“CircuitPrinter”的所有生产结果 ProductionResult架构: import { Schema, model } from 'mongoose'; const schemaOptions = { timestamps: { createdAt: 'created_at', updatedAt: false }, }; const productionResultSchema = new Schema({ machineId: { ty
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"
}
}
{
"_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
}
示例数据生成结果文档:
{
"_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"
}
}
{
"_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
}
你能展示一些样本数据吗?@CuongLeNgoc我在上面的帖子中添加了样本数据。机器名不是“Foo”。呃。。这是唯一的例子——Foo,Bar。我编辑的机器的itId与ProductionResult的machineId不匹配