MongoError:无法识别的管道阶段名称:'$addFields' [英] MongoError: Unrecognized pipeline stage name: '$addFields'

查看:25
本文介绍了MongoError:无法识别的管道阶段名称:'$addFields'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

MongoError: Unrecognized pipeline stage name: '$addFields'. "mongoose": "^4.5.8" My sourcecode:

                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: ''}
}

推荐答案

$addFields 在 Mongo 3.4 版本中引入.正如您评论说您使用的是 mongo 3.2.9,上述查询将不起作用.

$addFields is introduced in Mongo 3.4 version. As you have commented that you are using mongo 3.2.9, the mentioned query won't work.

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

If you cannot update the mongo version for some reason, then you have to use following approach where you need to iterate over each document and set the new field

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

这篇关于MongoError:无法识别的管道阶段名称:'$addFields'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆