【问题标题】:MongoError: Unrecognized pipeline stage name: '$addFields'MongoError:无法识别的管道阶段名称:'$addFields'
【发布时间】:2017-04-26 09:57:59
【问题描述】:

MongoError:无法识别的管道阶段名称:'$addFields'。 “猫鼬”:“^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: ''}
}

【问题讨论】:

  • mongodb 上的版本是什么? $addFields 在 3.4 中引入
  • mongodb的版本是^4.5.8
  • 在 packge.json 中:“mongoose”:“^4.5.8”,“mongoose-json-select”:“^0.2.1”,“mongoose-unique-validator”:“^1.0 .2",
  • 使用这个问题stackoverflow.com/questions/15311305/…987654321@中的答案检查版本
  • 我查了一下,mongodb的版本是3.2.9。谢谢!

标签: mongodb aggregation-framework


【解决方案1】:

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

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

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

【讨论】:

  • 修改原始文档是一个糟糕的解决方法。在 OP 的问题中注释掉的 $project 是正确的解决方法。
  • 非常感谢!我更新了 mongo 版本并解决了我的问题
  • @JohnnyHK 你能解释一下为什么 $project 解决方案是正确的吗?我正在寻找这个问题的答案:stackoverflow.com/questions/49989101/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-18
  • 2021-02-10
  • 2019-09-01
  • 2020-10-07
  • 2016-11-09
相关资源
最近更新 更多