Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.6k views
in Technique[技术] by (71.8m points)

mongodb - MongoError: Unrecognized pipeline stage name: '$addFields'

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);
                    }
                )

Post model:

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: ''}
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

$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.

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 }})
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...