11

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: ''}
}
Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
Hoàng Nck
  • 111
  • 1
  • 1
  • 3

1 Answers1

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.

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 }})
});
sidgate
  • 14,650
  • 11
  • 68
  • 119
  • 3
    Modifying the original documents is a poor workaround. The commented out `$project` in the OP's question is the right workaround. – JohnnyHK Apr 26 '17 at 13:47
  • Thank you very much! I updated the mongo version and solved my problem – Hoàng Nck Apr 27 '17 at 06:19
  • @JohnnyHK can you explain why the $project solution is correct? I am looking for answers to this one: https://stackoverflow.com/questions/49989101/mongoerror-unrecognized-pipeline-stage-name-changestream – Alexander Mills Apr 23 '18 at 20:04