errorreplicating
errorreplicating

Reputation: 611

mongo how to update a document

Given the following schema from a collection named user:

{
    "email": "[email protected]", 
    "classes": [
        {
            "className": "AAAA1111",
            "marks": [72, 85, 64],
            "grades": [20, 40, 30]
        },
        {
            "className": "BBBB2222",
            "marks": [12, 25, 43],
            "grades": [32, 42, 32]
        }
    ]
}

How would I add another class to the person with the email of [email protected]?

For example, how do I add the subdocument

{
    "className": "CCCC3333",
    "marks": [75, 85, 95],
    "grades": [20, 30, 50]
}

whilst keeping the existing information? How would I delete class AAAA1111 without affecting the rest of the document? I am new to mongo and have had trouble with the solutions I have tried. What is the best practice?

Upvotes: 2

Views: 84

Answers (3)

chridam
chridam

Reputation: 103375

To add another another class to the person with the email of [email protected], use the positional $ operator in your update document since this identifies an element in an array to update without explicitly specifying the position of the element in the array. As you are updating a field within an embedded document, you would require the use of the dot notation. When using the dot notation, enclose the whole dotted field name in quotes. The following updates the classes array in the embedded users document.

Define an updateClasses function as follows:

var updateClasses = function(db, callback) {
   db.collection('classes').updateOne(
        { "users.email" : "[email protected]" },
        {
            "$push": {
                "users.$.classes": {
                    "className": "CCCC3333",
                    "marks": [75, 85, 95],
                    "grades": [20, 30, 50]
                }
            }
        }, function(err, results) {
        console.log(results);
        callback();
   });
};

Call the updateClasses function.

MongoClient.connect(url, function(err, db) {
    // handle error
    if (err) handleError(err);

    updateClasses(db, function() {
        db.close();
    });
});

To delete class "AAAA1111" without affecting the rest of the document, use teh $pull operator which removes from an existing array all instances of a value or values that match a specified condition:

db.collection('classes').updateOne(
    { "users.email" : "[email protected]" },
    {
        "$push": {
            "users.$.classes": {
                "className": "AAAA1111"
            }
        }
    }, function(err, results) {
    console.log(results);
    callback();
});

Upvotes: 1

Rashad Ibrahimov
Rashad Ibrahimov

Reputation: 3309

Push operation to add new class to array:

db.users.update({"email": "[email protected]"}, {
    $push: {
        "classes": {
            "className": "CCCC3333",
            "marks": [75, 85, 95],
            "grades": [20, 30, 50]
        }
    }
});

Pull operation to remove specific class from array:

db.users.update({"email": "[email protected]"}, {
    $pull: {
        "classes": {
            "className": "AAAA1111"
        }
    }
});

Upvotes: 1

michelem
michelem

Reputation: 14590

To add a class to classes you can retrieve the document, push the new class array into classes and save the document:

users.findOne({
    email: '[email protected]'
}).exec(function(err, user) {
    var newClass = {
        className: 'CCCC3333',
        marks: [75, 85, 95],
        grades: [20, 30, 50]
    };

    user.classes.push(newClass);
    user.save(function(err) {
        if(err) console.log(err);
        console.log('Updated!');
    });
});

Upvotes: 0

Related Questions