对使用Populate的MongooseJS Schema Array进行更新

时间:2013-08-23 18:54:40

标签: node.js mongodb mongoose mlab

我正在使用mongoose人口加入文件

var UsersSchema = Schema({
        username: String,
        password: {type: String, select: false },
        fname: String,
        lname: String,
        role: String,
        apps: [
            {
                //using populate here
                app: {type:ObjectId, ref: "applications"},
                pinned: Boolean
            }
        ]
    }, { collection : 'user' });

这就像这样被称为

findById: function(id, items, callback){
            User.findById(id, items , function(err, doc){
                callback(doc);
            }).populate("apps.app");
        }

这一切都很常见。我的问题是当我想将新应用程序推入正在填充的值时。这就是我在做的事。

客户端:

$.ajax({
            type: "PUT",
            url: userUrl + userId,
            contentType: "application/json",
            data: JSON.stringify({
                "app": currentApp.attributes._id,
                "pinned": false
            }),
            success: function(){
                console.log("Success");
            }
        })

节点服务器:

User.findByIdAndUpdate(id,
                    {
                        $push : {
                            apps : {
                                pinned:updateObj.pinned,
                                app:updateObj.app
                            }
                        }
                    }, options, function(err, data){
                    callback(data);
                });

这似乎也更正了更新。问题是它被添加到我的Mongo文档的方式

{
    "_id": {
        "$oid": "520953dde4b00c03eeb39950"
    },
    "apps": [
        {
            "app": "5217685be4b061d31fe3cc41",
            "pinned": true
        },
        {
            "app": "5208edb2e4b0b41ab826aca1",
            "pinned": false
        },
//this is what was added
        {
            "pinned": false,
            "app": {
                "$oid": "5208ed90e4b0b41ab826ac9f"
            },
            "_id": {
                "$oid": "5217ac368d334b020000000a"
            }
        }
    ],
    "fname": "Frank",
    "lname": "Miggz",
    "password": "password123",
    "role": "2",
    "username": "blah"
}

我如何:

删除_id 只有app的值显示,所以看起来像这样

"app": "5208ed90e4b0b41ab826ac9f"

2 个答案:

答案 0 :(得分:0)

刚刚意识到我预先填充的值应该采用我要回复的格式,因为它在Schema中设置为“type:ObjectId”。我相信我必须重新填充以匹配ObjectId结构

答案 1 :(得分:0)

另一个库可能会使这类操作更容易https://www.npmjs.com/package/@coolgk/mongo

文档中的示例:

原始数据:

{
    _id: '5a8c16f3c452fd2c0d3687c6',
    _dateModified: '2018-02-20T12:39:15.258Z', // auto generated
    title: 'Support Ticket 1',
    messages: [{
            _id: '5a8c16f3c452fd2c0d3687c9', // auto generated
            user: 'customer',
            message: 'I found a bug',
            _dateModified: '2018-02-20T12:39:15.259Z' // auto generated
        },
        {
            _id: '5a8c16f3c452fd2c0d3687c8', // auto generated
            user: 'support',
            message: 'Restart your computer',
            _dateModified: '2018-02-20T12:39:15.259Z' // auto generated
        },
        {
            _id: '5a8c16f3c452fd2c0d3687c7', // auto generated
            user: 'developer',
            message: 'That\'s not a bug, it\'s a feature',
            _dateModified: '2018-02-20T12:39:15.259Z' // auto generated
        }
    ]
}
更新子文档

下面的脚本将更新"message"数组中seconnd文档的"messages"字段的值。

model.updateOne({
    _id: '5a8c16f3c452fd2c0d3687c6', // find the document by _id in the collection
    messages: [
        {
            _id: '5a8c16f3c452fd2c0d3687c8', // find the document by _id in the array
            message: 'Turn on your computer' // update only the message field, other fields will not change
        }
    ]
});

集合中的数据变为

{
    _id: '5a8c16f3c452fd2c0d3687c6',
    title: 'Support Ticket 1',
    messages: [{
            _id: '5a8c16f3c452fd2c0d3687c9',
            user: 'customer',
            message: 'I found a bug',
            _dateModified: '2018-02-20T12:39:15.259Z',
        },
        {
            _id: '5a8c16f3c452fd2c0d3687c8',
            user: 'support',
            message: 'Turn on your computer', // new value
            _dateModified: '2018-02-20T12:53:55.890Z' // new modified date
        },
        {
            _id: '5a8c16f3c452fd2c0d3687c7',
            user: 'developer',
            message: 'That\'s not a bug, it\'s a feature',
            _dateModified: '2018-02-20T12:39:15.259Z'
        }
    ],
    _dateModified: '2018-02-20T12:53:55.889Z' // new modified date
}
删除文档

下面的脚本将删除"messages"数组中的seconnd文档。

model.updateOne({
    _id: '5a8c16f3c452fd2c0d3687c6', // find the document by _id in the collection
    messages: {
        $delete: [ '5a8c16f3c452fd2c0d3687c8' ] // $delete operator: delete by _id
    }
});

集合中的数据变为

{
    _id: '5a8c16f3c452fd2c0d3687c6',
    title: 'Support Ticket 1',
    messages: [{
            _id: '5a8c16f3c452fd2c0d3687c9',
            user: 'customer',
            message: 'I found a bug',
            _dateModified: '2018-02-20T12:39:15.259Z'
        },
        {
            _id: '5a8c16f3c452fd2c0d3687c7',
            user: 'developer',
            message: 'That\'s not a bug, it\'s a feature',
            _dateModified: '2018-02-20T12:39:15.259Z'
        }
    ],
    _dateModified: '2018-02-20T12:59:05.602Z' // new modified date
}
替换或全部删除
model.updateOne({
    _id: '5a8c16f3c452fd2c0d3687c6', // find the document by _id in the collection
    messages: {
        $replace: [] // replace the entire array with a new value
    }
});
添加子文档

与更新类似,但在子文档中没有_id。下面的脚本会将新文档添加到messages数组中。

model.updateOne({
    _id: '5a8c16f3c452fd2c0d3687c6', // find the document by _id in the collection
    messages: [
        { // documents without _id = insert new
            user: 'Support',
            message: 'Please Ctrl + F5'
        }
    ]
});

集合中的数据变为

{
    _id: '5a8c16f3c452fd2c0d3687c6',
    title: 'Support Ticket 1',
    messages: [{
            _id: '5a8c16f3c452fd2c0d3687c9',
            user: 'customer',
            message: 'I found a bug',
            _dateModified: '2018-02-20T12:39:15.259Z'
        },
        {
            _id: '5a8c16f3c452fd2c0d3687c7',
            user: 'developer',
            message: 'That\'s not a bug, it\'s a feature',
            _dateModified: '2018-02-20T12:39:15.259Z'
        },
        { // new document
            _id: '5a8c1d6b082a652c35eb17d6', // auto generated
            user: 'Support',
            message: 'Please Ctrl + F5',
            _dateModified: '2018-02-20T13:06:51.244Z' // auto generated
        }
    ],
    _dateModified: '2018-02-20T13:06:51.243Z' // new modified date
}
多个操作

添加,更新和删除可以在一个查询中进行。

model.updateOne({
    _id: '5a8c16f3c452fd2c0d3687c6', // find the document by _id in the collection
    messages: {
        $update: [
            { // doc contains _id value, this is an update
                _id: '5a8c1d6b082a652c35eb17d6', // find the document by _id in the array
                message: 'Clear Your Cache' // update only the message field, other fields will not change
            },
            { // doc has no _id value, this is an insert
                user: 'developer',
                message: 'cannot replicate, not a bug!'
            }
        ],
        $delete: [ '5a8c16f3c452fd2c0d3687c9' ] // delete by _id (the first doc)
    }
});

最终结果

{
    _id: '5a8c16f3c452fd2c0d3687c6',
    title: 'Support Ticket 1',
    messages: [{
            _id: '5a8c16f3c452fd2c0d3687c7',
            user: 'developer',
            message: 'That\'s not a bug, it\'s a feature',
            _dateModified: '2018-02-20T12:39:15.259Z'
        },
        {
            _id: '5a8c1d6b082a652c35eb17d6',
            user: 'Support',
            message: 'Clear Your Cache', // new value
            _dateModified: '2018-02-20T13:30:07.123Z' // new modified date
        },
        { // new doc
            _id: '5a8c22df4656722c3fd787fa',// auto generated
            user: 'developer',
            message: 'cannot replicate, not a bug!',
            _dateModified: '2018-02-20T13:30:07.123Z' // auto generated
        }
    ],
    _dateModified: '2018-02-20T13:30:07.121Z' // new modified date
}