MongoError:无法识别的管道阶段名称:' $ addFields'

时间:2017-04-26 09:58:00

标签: mongodb aggregation-framework

MongoError:无法识别的管道阶段名称:' $ addFields'。 " mongoose":" ^ 4.5.8" 我的源代码:

                Post.aggregate(
                    [{
                        $addFields: {
                            userName: { $concat: [ "$author.firstName", " ", "$author.lastName" ] }
                        }
                        //$project: { userName: { $concat: [ "$author.firstName", " ", "$author.lastName" ] } } //this is ok!
                    }],
                    function (err, result) {
                        if (err) {
                            console.log(err);
                            return;
                        }
                        console.log(result);
                    }
                )

发布模型:

let schema = {
id: "post",
properties: {
    content: {type: "string"},
    author: {
        type: "object",
        id: {type: "string"},
        avatar: {type: "string"},
        firstName: {type: "string"},
        lastName: {type: "string"},
        status: {type: "string"}
    },
    category: {
        type: "object",
        id: {type: "string"},
        name: {type: "string"}
    },
    images: {
        type: "array",
        items: {
            type: "object",
            properties: {
                filePath: {type: "string"},
            }
        }
    },
    video: {
        type: "object",
        thumbnail: {type: "string"},
        filePath: {type: "string"}
    },
    likes: {
        type: "array",
        items: {
            type: "object",
            properties: {
                userId: {type: "string"},
                status: {type: "string"},
                _id   : {type: "string", default: null}
            }
        }
    },
    shares: {
        type: "array",
        items: {
            type: "object",
            properties: {
                userId: {type: "string"},
                status: {type: "string"},
                destination: {type: "string"}, //FACEBOOK|TWISTER|GOOGLE
                _id        : {type: "string", default: null}
            }
        }
    },
    favorites: {
        type: "array",
        items: {
            type: "object",
            properties: {
                userId: {type: "string"},
                status: {type: "string"},
                _id   : {type: "string", default: null}
            }
        }
    },
    comments: {
        type: "array",
        items: {
            type: "object",
            properties: {
                commentId: {type: "string"},
                _deleted: {type: "Date", default: ''},
                _id     : {type: "string", default: null}
            }
        }
    },
    _created: {type: "Date", default: Date.now},
    _deleted: {type: "Date", default: ''},
    _updated: {type: "Date", default: ''}
}

1 个答案:

答案 0 :(得分:8)

Mongo 3.4版本中引入了

$addFields。正如您所评论的那样,您使用的是mongo 3.2.9,所提到的查询将无效。

如果由于某种原因无法更新mongo版本,则必须使用以下方法,您需要迭代每个文档并设置新字段

Post.find({}).forEach(function(post){
  post.findOneAndUpdate({_id: post._id}, 
      {$set: {userName: post.author.firstName + " " + post.author.lastName }})
});