node.js - 将 $concat 与 $project 一起使用会出现错误 : 'MongoError: $concat only supports strings, not double' ?

标签 node.js mongodb mongoose aggregate mean-stack

我有一个 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/

相关文章:

javascript - Promise 并将数组中的元素添加到另一个数组中的对象中

node.js - 更改 Amazon EC2 上的 ulimit

node.js - 将 TCP 服务器部署到 Heroku

javascript - 灰尘 : what about performance?

mongodb - php70-mongo 安装 Doctrine/mongodb-odm 失败

node.js - mongoose.model 函数中的名称与 mongodb 中的集合名称不同

javascript - 在保存之前清理用户输入或从数据库检索后清理输入?

Mongodb聚合查询对累积值进行减法和分组

node.js - 如何获得 Mongoose 模型的所有计数?

node.js - GeoJSON 在 Node.js 中与 Mongoose/Mongodb 配合使用