0

我正在使用猫鼬种群来加入文档

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"
4

2 回答 2

0

刚刚意识到我预先填充的值应该是我返回的格式,因为它在模式中设置为“type:ObjectId”。我相信我将不得不重新填充以匹配 ObjectId 结构

于 2013-08-23T19:13:46.963 回答
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"中第二个文档的字段值。"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"数组中的第二个文档。

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
}
于 2018-02-24T08:31:53.860 回答