Reputation: 505
When I use "findOne" function. I get the correct result.
But when I use aggregate and $match, I get empty results. Is there any wrong code?
I should use aggregate, so I can't use findOne function. I tried a lot of code. But I can't find the solution.
// 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
}
}
}
Thanks
Upvotes: 0
Views: 155
Reputation: 134
Two different types that you compare with the id you're trying to match. So you seem to be trying to compare string with object id
Clinic schema id => like this Schema.Types.ObjectId
I hope it helped
Upvotes: 1