logo
down
shadow

push into deeply nested array


push into deeply nested array

By : rhamille
Date : November 22 2020, 01:01 AM
this one helps. Based on my research and understanding, this is not currently possible to do within mongodb in a single operation. According to this JIRA ticket, it is desired for an upcoming version of mongodb.
Here is a tedious way of doing in the shell what you are attempting to accomplish:
code :
var school = db.schools.findOne({name: "high school"}) //assuming unique
school.departments.forEach(function(department) {
    if(department.name === "math") {
        department.courses.forEach(function(course) {
            if(course.name === "algebra") {
                course.lessons.forEach(function(lesson) {
                    if(lesson.name === "algebra 1a") {
                        lesson.students.push({name: "Jon", id: "43953"});
                    }
                });
            }
        });
    }
})
db.schools.save(school)


Share : facebook icon twitter icon
How do I push an object in to an array that is contained within a deeply embedded document?

How do I push an object in to an array that is contained within a deeply embedded document?


By : Bennett Gale
Date : March 29 2020, 07:55 AM
will be helpful for those in need You are missing the match on the positional $ operator in the update section. You will also want a "dot notation" form of this for the key to identify the "comments" field you are pushing into at the top level:
code :
app.post('/comment/:comment_id/sub-comment', function(req, res) {
    var updateData = {
        "comments.$.sub_comments": {
            title: req.body.title,
            sub_comment: req.body.sub_comment,
            user: req.user._id 
        }
    };
    Post.update(
        { "comments._id" : req.params.comment_id }, 
        { $push : updateData }, 
        function(err,data) {
            return res.json(data);
        }
    );
});
how to push a new item into a deeply nested Record in immutableJS

how to push a new item into a deeply nested Record in immutableJS


By : Mahmoud Abu Alyazid
Date : March 29 2020, 07:55 AM
this one helps. You're missing return statement in a function you pass to updateIn (note that Array.push does NOT return a resulting array!). It should be:
code :
const myNewMap = myMap.updateIn(
  ['nested', 'someKey'], 
  (fav) => {
    fav.push({id:3,name:'dan'})
    return fav
  })
mongoose cannot push to deeply nested document multiple positional operator

mongoose cannot push to deeply nested document multiple positional operator


By : Amusing Alias
Date : March 29 2020, 07:55 AM
will be helpful for those in need I want to push element to subfeatures nested document by using $push. but when i use like this ''modules.$.features.$.subFeatures'' it gives me the multiple positional operator error. so I get the features document index as x but i cannot figure out which way I can append that value to the query. 'modules.$.features.x.subFeatures' doesn't working. , You can do it in several ways:
use es6:
code :
$push:{
             [`modules.$.features.${x}.subFeatures`]: {
                name: data.name,
                value: false,
                icon: data.icon
             }
          }
var push = {$push: {}};
push.$push['modules.$.features.' + x + '.subFeatures'] = {
                name: data.name,
                value: false,
                icon: data.icon
             };
RolePermission.update({
    'role': roleID,
     'modules.name' : data.module,
     'modules.features.name': data.feature

},
push, 
{
    upsert: true
}, function (err, event) {

});
Does array.push copies deeply?

Does array.push copies deeply?


By : user1875563
Date : March 29 2020, 07:55 AM
this one helps. No, Array.push does not cause deep copying.
this.threads.push(worker) will not modify the worker you are passing to it but will just add it as-is to the array this.threads.
Push an item to a deeply nested array in MongoDb

Push an item to a deeply nested array in MongoDb


By : Mohd Zulfadli
Date : March 29 2020, 07:55 AM
Does that help In order to push to a nested array, you must make use of the positional operator $ in order to specify a matching outer array element to apply the operation to. For example:
code :
db.collection.update(
    {"my_array._id": myTargetId},
    {$push: {"my_array.$.my_inner_array": myArrayElem}}
);
// Index chain found to be Departments.0 and Managers.0
db.collection.update(
    {
        "Id": 12345,
        "Departments.0.Managers.0.Id": 5555 // Specify index chain 0,0 and ensure that our target still has Id 5555.
    },
    { $push: {
        "Departments.0.Managers.0.Employees": myNewEmployee // Push to index chain 0,0
    }}

);
Related Posts Related Posts :
  • Badly placed ()'s error with the following shell script
  • Kotlin: Can I access a nested array from inside another array?
  • minimum complexity to find if a number is present n/2 times or not in a sorted array
  • Powershell: Comparing multiple values in an array
  • Add Elements to nullable Vector
  • How to get elements larger than x in a given range?
  • How to call Array-valued Functions in fortran?
  • How do I create and pass string array to a sub in Excel VBA?
  • How can I auto-sort an array in Swift by property type?
  • Trying to test collisions with objects in array
  • Sort an array of arrays in Erlang
  • Encapsulate strings in an array in PowerShell
  • Send Specific variables through socket.io to client with socket.send
  • Max within groups in Matlab
  • when preparing for segue array contains no data (using swift)
  • How do you access the attributes of a class instance from inside an array? (Swift)
  • Array.Find and IndexOf for multiple elements that are exactly the same object
  • How to find array items that occur at least K times
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org