0

我的用户架构是:

{
    "_id": {"$oid": "515774f2c24007eb57000001"},
    "login": "Ilya",
    "password": "30947a8c9f39b27290a11eabaeec8c89",
}

我正在向用户对象动态添加研讨会信息。在这段代码之后:

exports.signSeminar = function(user_id, seminar_id, callback) {
    var user_obj_id = BSON.ObjectID.createFromHexString(String(user_id));
    db.collection("users", function(error, collection) {
        collection.findAndModify({
            _id: user_obj_id
        }, [], {
             $set: {
                "seminars": [{
                    "id:": String(seminar_id)
                }]
             }
        }, {}, function(err, result) {
            callback(200);
        });
    });
};

我的对象变成:

{
    "_id": {"$oid": "515774f2c24007eb57000001"},
    "login": "Ilya",
    "password": "30947a8c9f39b27290a11eabaeec8c89",
    "seminars": [
        {
            "id:": "51562509b781ae1d53000001"
        }
    ],
}

在我对该代码进行两次以上外壳之后,它只是在“研讨会”中重新编写了子对象。我需要像这样插入另一个研讨会对象:

{
    "_id": {"$oid": "515774f2c24007eb57000001"},
    "login": "Ilya",
    "password": "30947a8c9f39b27290a11eabaeec8c89",
    "seminars": [
        {
            "id:": "51562509b781ae1d53000001"
        },
        {
            "id:": "#another_object_id#"
        }
    ],
}

我做错了什么?谢谢。

PS尝试findAndModify,更新,取消插入,没有运气((

4

1 回答 1

2

$set设置一个值,你$push想追加到一个数组

 {
    $push: {
            "seminars": {
                "id:": String(seminar_id)
           }
 }
于 2013-03-30T23:43:27.647 回答