2017-04-26 10 views
3

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

Postモデル:

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

のMongoDBのバージョンは何ですか? '$ addFields'は3.4 – sidgate

+0

にMongoDBのバージョンである導入された^ 4.5.8 –

+0

packge.jsonで: "マングース": "^ 4.5.8"、 "マングース-jsonの選択": "^ 0.2.1"、 "mongoose-unique-validator": "^ 1.0.2"、 –

答えて

7

$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 }}) 
}); 
+0

元の文書を変更するのは貧弱な回避策です。 OPの質問にコメントされた '$ project'が正しい回避策です。 – JohnnyHK

+0

ありがとうございます!私はmongoのバージョンを更新し、私の問題を解決しました –

関連する問題