2

I have a scheme with nested arrays. I want to push an element to the 2rd dimension array. How can I do?

//working
db.findOneAndUpdate({_id:req.params._universtiyid,'faculties._id':req.params._facultiesid},
    { $push:
        {  'faculties.$.points':point}},


//not working
db.findOneAndUpdate({_id:req.params._universtiyid,'faculties._id':req.params._facultiesid},
    { $push:
        {  'faculties.$.department.$.points':points}},

//schema
faculties:[
        {   
            name:{type:String},
            department:[
                {
                    name:{type:String},
                    points:[]
                }
            ]
        }
    ],
  • 1
    You can only use $ once. One way potentially of achieving what you want is to overwrite the entire second array using aggregation pipeline update operators and $out/$merge. – D. SM Aug 05 '20 at 19:49
  • @D.SM Thanks for quick response. Can you write a little sample code for me? – irfan çetin Aug 05 '20 at 19:51
  • 1
    you can do this way using arrayFilters https://stackoverflow.com/a/63041332/8987128 – turivishal Aug 05 '20 at 19:51
  • @turivishal its working thanks. I've been working for a day, you solved it in 2 minutes. I'm grateful. – irfan çetin Aug 05 '20 at 20:01
  • 1
    yup it is easy to use and ready-mate for nested array.. – turivishal Aug 05 '20 at 20:03
  • Does this answer your question? [How can I return the element I'm looking for inside a nested array?](https://stackoverflow.com/questions/62988071/how-can-i-return-the-element-im-looking-for-inside-a-nested-array) – Joe Aug 06 '20 at 00:30

0 Answers0