Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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: ''}
}
like image 333
Hoàng Nck Avatar asked Apr 26 '17 09:04

Hoàng Nck


People also ask

What is the $addfields stage in MongoDB?

The $addFields stage is equivalent to a $project stage that explicitly specifies all existing fields in the input documents and adds the new fields. Starting in version 4.2, MongoDB adds a new aggregation pipeline stage $set that is an alias for $addFields. { $addFields: { < newField >: < expression >, ...

How do I create a MongoDB aggregation pipeline in Studio 3T?

To build our MongoDB aggregation example, we will be using the Aggregation Editor, the stage-by-stage aggregation pipeline editor in Studio 3T. Build accurate aggregation queries and make debugging easier by defining stage operators and checking inputs and outputs at each stage. Download it here, or if you have already done so, skip to the example.

Is the $search aggregation pipeline stage available in Atlas?

Correct, the $search Aggregation Pipeline stage is only available in MongoDB Atlas as an Atlas Search index is required, which cannot be created in a local deployment. If you are looking to create a Text Index these can be queried using the $text operator as follows:

How do I use MongoDB aggregation query in Python?

Here’s our MongoDB aggregation query, in Python: Read about Query Code in full here. Views are a great shortcut to accessing the data you need without having to run the same queries. Right-click anywhere in the Pipeline and Stage tabs and choose Create view from this aggregation query.


1 Answers

$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 }})
});
like image 80
sidgate Avatar answered Oct 07 '22 02:10

sidgate