Mongoose聚合$match返回空结果



当我使用"findOne";作用我得到了正确的结果。

但当我使用聚合和$match时,我得到的结果是空的。有错误的代码吗?

我应该使用聚合,所以我不能使用findOne函数。我试了很多代码。但我找不到解决办法。

//resolver.js

const Mongoose = require('mongoose');
const { ObjectId } = Mongoose.Types;
const { GraphQLScalarType } = require('graphql');
const resolvers = {
Query: {
getClinic: (_, { _id }) => {
console.log("getClinic", _id);
const data = Clinic.aggregate([
{ $match: { _id: ObjectId(_id) } },
{
$lookup: {
// do something
},
},
]);
return data;
},  
}
}

//schema.js

import mongoose from 'mongoose';
const { Schema } = mongoose;
const clinicSchema = new Schema(
{
id: String,
crn: String,
name: String,
},
{ collection: 'clinic' },
);
export default mongoose.model('Clinic', clinicSchema);

//typedefs.graphql

type Clinic {
_id: ID
crn: String
name: String
//...
}
type Query {
getClinic(_id: ID!): Clinic!
}
// Query
query  {
getClinic(_id: "5fcf19678c032a999f9e9dbc") {
_id
crn
name
}
}
// return 
{
"data": {
"getClinic": {
"_id": null,
"crn": null,
"name": null
}
}
}

感谢

与要匹配的id进行比较的两种不同类型。所以您似乎正在尝试将字符串和对象id 进行比较

诊所架构id=>像这样Schema.Types.ObjectId

我希望它能帮助

最新更新