1

I have an array defined in mongoose. In that array there are some fields and one array. All those fields are filled except the array which empty. Now I want to push the new elements into that array. Below is the syntax in mongoose:

{
    "_id" : ObjectId("5f426fb0c4057b2ae87e6a8c"),
    "hash" : "1cdde2c528d9ac6770d1cb735e89f57b99bf0bc442269013f9df492ea9a2b70ee88c1220a71ac31c650c1cda42573ef9c2f452d572d99866e8a1bd7ed5ea439a",
    "salt" : "677468d92cff400e39ed4ad484be3fb8",
    "tag" : "Client",
    "email" : "vikas.zv95@gmail.com",
    "name" : "Vikas Yadav",
    "Addtasks" : [ 
        {
            "commonID" : "2iwxtrwnap9",
            "status" : "Done",
            "Date" : "Sun Aug 23 2020 19:02:08 GMT+0530 (India Standard Time)",
            "exampleRadios" : "option1",
            "otherdetails" : "Not much thanks!",
            "website" : "dsfdd.com",
            "keywords" : "article importance, article generation, article quality",
            "words" : 12345,
            "topic" : "How article is generated?",
            "_id" : ObjectId("5f426fd8c4057b2ae87e6a90"),
            "Bigpaths" : [ 
             
            ]
        }, 
        {
            "commonID" : "xtcsrnfbvg",
            "status" : "Done",
            "Date" : "Sun Aug 23 2020 19:02:31 GMT+0530 (India Standard Time)",
            "exampleRadios" : "option1",
            "otherdetails" : "haha great!",
            "website" : "grumpytext.com",
            "keywords" : "anxiety disorders for children, anxiety disorders for adults",
            "words" : 2345,
            "topic" : "How to fight anxiety?",
            "_id" : ObjectId("5f426fefc4057b2ae87e6a94"),
            "Bigpaths" : [ 
               
            ]
        }
    ],
    "__v" : 0
}

I want to push only Bigpaths array of Addtasks array only this time. below is my code I am trying:

var filesArray = req.files;
var Bigpaths2 = [];
   

   filesArray.forEach(element => {


     paths = {
      "path": element.path,
      "name": element.filename
        };

  Bigpaths2.push(paths);
  });
User.updateMany({'Addtasks.commonID':cid}, {$push: {Addtasks.Bigpaths: Bigpaths2}},
  function (error, WriterData) {
        if (error) {
            console.log("error");
        }  else {
  console.log("Task added for the writer.")
  }
  });

I am getting this error:

\Desktop\grumpytext\routes\index.js:254
User.updateMany({'Addtasks.commonID':cid}, {$push: {Addtasks.Bigpaths: B
igpaths2}},
                                                            ^
SyntaxError: Unexpected token '.'
Biku7
  • 450
  • 6
  • 16
  • Maybe try `{$push: {'Addtasks.Bigpaths': Bigpaths2}}` ? I don't know if that's correct Mongo-wise, but it'd correct the syntax. – AKX Aug 25 '20 at 09:13
  • its getting the same error :( Check updated. – Biku7 Aug 25 '20 at 09:14
  • No, you simply can't be getting the same syntax error for that change. (See how `'Addtasks.Bigpaths'` is now wrapped in quotes?) – AKX Aug 25 '20 at 09:16
  • it isnt wrapped in quotes anywhere. – Biku7 Aug 25 '20 at 09:24
  • See my comment above. It should probably be wrapped in quotes in the `$push` operation. – AKX Aug 25 '20 at 09:34
  • ya I wrapped it. And now i am getting this error: Error [MongoError]: Cannot create field 'Bigpaths' in element {Addtasks: [ { commonID: "oi6xiiuk9a", status: "Done", Date: "Tue Aug 25 2020 15:07:06 GMT – Biku7 Aug 25 '20 at 09:42
  • Glad I could help. That's a different question you can probably find an answer to somewhere. – AKX Aug 25 '20 at 09:48
  • ya i already solved it @Tahero thanks! – Biku7 Aug 26 '20 at 06:54

0 Answers0