CastError: Cast to embedded failed for value. Error thrown during an aggregation pipeline that works elsewhere (in MongoDB Compass)
Do you want to request a feature or report a bug? Bug
What is the current behavior?
Mongoose throws a CastError: Cast to embedded failed for value. during an aggregation pipeline i.e. a model.aggregate().
The aggregation pipeline is complex, and was constructed using MongoDB compass (GUI/desktop database interface) "aggregations constructor/editor" and works fine here- the software validates native mongo shell syntax in the aggregation stages, and the final document output is what I want. The problem is when I export the aggregation in Node.JS format, and put it in a model.aggregate() call within my node program that connects with my database, the error is thrown.
If the current behavior is a bug, please provide the steps to reproduce.
Error is not very verbose, but says:
CastError: Cast to embedded failed for value "\'https://www.bristolcreatives.co.uk/user/login\'" at path "auditedPages"
Aggregation function:
LighthouseAudit.aggregate(
[
{
'$match': {
'domain': domain,
'userId': userId
}
}, {
'$facet': {
'pages': [
{
'$project': {
'report.page': 1
}
}
],
'docs': [
{
'$project': {
'domain': 1,
'report': 1,
'userId': 1,
'completed': 1
}
}
]
}
}, {
'$project': {
'pages': '$pages.report.page',
'docs': 1
}
}, {
'$unwind': {
'path': '$docs'
}
}, {
'$replaceRoot': {
'newRoot': {
'$mergeObjects': [
'$docs', {
'auditedPages': '$pages'
}
]
}
}
}, {
'$unwind': {
'path': '$report.pageIssues'
}
}, {
'$group': {
'_id': '$report.pageIssues.id',
'domain': {
'$first': '$domain'
},
'count': {
'$sum': 1
},
'timeTaken': {
'$sum': '$report.timeTaken'
},
'urls': {
'$push': '$report.page'
},
'userId': {
'$first': '$userId'
},
'completed': {
'$max': '$completed'
},
'auditedPages': {
'$first': '$auditedPages'
}
}
}, {
'$sort': {
'_id': 1
}
}, {
'$group': {
'_id': null,
'domain': {
'$first': '$domain'
},
'issues': {
'$push': {
'id': '$_id',
'count': '$count',
'urls': '$urls'
}
},
'userId': {
'$first': '$userId'
},
'timeTaken': {
'$first': '$timeTaken'
},
'completed': {
'$max': '$completed'
},
'auditedPages': {
'$first': '$auditedPages'
}
}
}, {
'$project': {
'_id': 0
}
}
]
)
What is the expected behavior? For the aggregation to match documents in a collection, perform a bunch of trickery on them and eventually spit out a single document with lots of information from the matched documents embedded.
Please mention your node.js, mongoose and MongoDB version. Node.Js v10.14.1 Mongoose 5.4.4 mongodb 3.1.12
Mongoose aggregation doesn't do any casting, so I'm fairly certain this error isn't caused by your aggregation. Is there any other code that might be causing this issue? Are you creating a new document and running validate() somewhere?
I have facing with below error message errors: { geometry: { CastError: Cast to Embedded failed for value "[72.591760, 23.014510]" at path "geometry" This is define schema // Create Geo Location Schema const GeoSchema = new Schema({ type: {type: String, default: 'point'}, coordinates: {type: [Number], index: '2dsphere'} });
// Create Uer Location Schema const UserLocationSchema = new Schema({ name: {type: String}, geometry: GeoSchema, createdAt: Date, updatedAt: Date });
MongoDB shell version v4.0.6 Node.JS v11.11.0 `-- [email protected]
Mongoose aggregation doesn't do any casting, so I'm fairly certain this error isn't caused by your aggregation. Is there any other code that might be causing this issue? Are you creating a new document and running
validate()somewhere?
This issue has been resolved by me, unfortunately I can't remember exactly what I did but it was fiddly and wierd. I was not calling validate() anywhere.
I'm pretty sure it was a datatype issue
Good luck!