2017-04-26 3 views
3

MongoError: Unbekannter Name der Pipelinestufe: '$ addFields'. "Mungo": "^ 4.5.8" Mein 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); 
        } 
       ) 

Beitrag Modell:

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

was ist die Version auf mongodb? '$ addFields' wird in 3.4 eingeführt – sidgate

+0

mongodbs Version ist^4.5.8 –

+0

in packge.json: " mongoose ":"^4.5.8 ", " mongoose-json-select ":"^0.2.1 ", "mongoose-unique-validator": "^ 1.0.2", –

Antwort

7

$addFields wird in Mongo 3.4 Version eingeführt. Nachdem Sie bemerkt haben, dass Sie mongo 3.2.9 verwenden, wird die erwähnte Abfrage nicht funktionieren.

Wenn Sie nicht die Mongo Version aus irgendeinem Grund aktualisieren, dann müssen Sie folgende Vorgehensweise verwenden, in denen Sie über jedes Dokument zu durchlaufen müssen und legen Sie das neue Feld

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

Das Ändern der Originaldokumente ist eine schlechte Problemumgehung. Das auskommentierte "$ -Projekt" in der OP-Frage ist der richtige Workaround. – JohnnyHK

+0

Vielen Dank! Ich habe die Mongo-Version aktualisiert und mein Problem gelöst –

Verwandte Themen