我有一个 Mongoose 模型,其中一些字段如下:
var AssociateSchema = new Schema({
personalInformation: {
familyName: { type: String },
givenName: { type: String }
}
})
我想对 familyName 和 givenName 的串联执行“$regex”(类似于“familyName + ""+ 'givenName'),为此我在 $project 中使用带有 $concat 的聚合框架来生成一个 'fullName' 字段,然后是 $match 中的 '$regex' 以搜索该字段。我查询的 Mongoose 代码是:
Associate.aggregate([
{ $project: {fullName: { $concat: [
'personalInformation.givenName','personalInformation.familyName']}}},
$match: { fullName: { 'active': true, $regex: param, $options: 'i' } }}
])
但它给我错误:
MongoError: $concat only supports strings, not double on the first stage of my aggregate pipeline i.e $project stage.
谁能指出我做错了什么?
最佳答案
我也遇到了这个错误,然后发现确实是集合中的一个文件造成的。我把它捞出来的方法是按字段类型过滤为 explained in the docs :
db.addressBook.find( { "zipCode" : { $type : "double" } } )
我发现该字段的值为 NaN
,在我看来这不是数字,但 mongodb 将其解释为数字。
关于node.js - 将 $concat 与 $project 一起使用会出现错误 : 'MongoError: $concat only supports strings, not double' ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49304533/