更新数组中的文档

更新数组中的文档

本文介绍了MongoDB - 更新数组中的文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:我建议阅读我接受的答案,它为您提供了解决此问题的不同方法.

我有以下收藏.

{
        "_id" : ObjectId("51fa56408e843042c0c24e3d"),
        "uniqDates" : [
                ISODate("2013-07-30T18:30:00Z"),
                ISODate("2013-07-31T18:30:00Z")
        ]
}
{
        "_id" : ObjectId("51fa5648c424ea3e37199502"),
        "events" : [
                {
                        "eid" : 1,
                        "title" : "Event 1",
                        "start" : ISODate("2013-08-04T18:30:00Z")
                },
                {
                        "eid" : 2,
                        "title" : "Event 2",
                        "start" : ISODate("2013-08-08T18:30:00Z")
                }
        ]
}

如果存在,我想更新events"数组中的文档(我想通过 eid 检查它),如果它不存在,我想插入该文档.

I want to update a document in "events" array if it exists (i want to check it by eid), if it doesn't exists i want to insert that document.

目前我正在通过两个查询执行此操作.

currently i am doing this with two queries.

db.col.update({events: {$exists: 1}}, {$pull: {"events": {"eid": 2}}});

db.schedule.update({events: {$exists: 1}}, {$addToSet:
{"events": {"eid": 2, "title": "Event 4", "start": new Date(2013, 08, 02)}}});

有没有办法通过单个查询来做到这一点?一些带有 upsert 操作的东西.

Is there a way to do this with single query? Something with upsert operation.

推荐答案

UPDATE
使用上面的集合来对文档进行分组是非常明显的心态.但是将文档放在数组中会导致其他一些问题.你不得不忍受 Array Update 操作符来更新每个文档.相反,我重组了我的收藏,如下所示.还,请参阅此 google 小组讨论.正如那里指定的idbentley"有可能超过文档大小限制 (16 MB),这将导致数据碎片化.因此,在为数据建模时要注意.

UPDATE
It is pretty obvious mindset to have collection like above to group documents.But having documents in array will result in some other problems. Youhave to suffer with Array Update operators to update eachdocument. Instead, i restructured my collection as shown below. Also,see this google group discussion. As "idbentley" specified thereis a possibility to exceed document size limit (16 MB) which willlead to data fragmentation. So be conscious while you model your data.

原始答案

似乎无法在文档数组中实现 upsert 操作.所以改变了我的架构设计,如下所示.

It seems like it is not possible to achieve upsert operation in document array. So changed my schema design like below.

{
        "_id" : ObjectId("51fa619a8e843042c0c24e46"),
        "uniqDates" : [
                ISODate("2013-07-30T18:30:00Z"),
                ISODate("2013-07-31T18:30:00Z")
        ]
}
{
        "_id" : ObjectId("51fa61b38e843042c0c24e47"),
        "eid" : 1,
        "title" : "Event 1",
        "start" : ISODate("2013-08-04T18:30:00Z")
}
{
        "_id" : ObjectId("51fa61b38e843042c0c24e48"),
        "eid" : 2,
        "title" : "Event 2",
        "start" : ISODate("2013-08-08T18:30:00Z")
}

要查找事件:

db.schedule.find({"eid": {$exists: 1}});

查找唯一日期:

db.schedule.find({"uniqDates": {$exists: 1}});

实现'upsert'操作:

To achieve 'upsert' operation:

db.schedule.update({"eid": 3}, {"eid": 3, "title": "Event 3",
                                "start": new Date(2013, 05, 06)}, true);

这看起来很有希望.

这篇关于MongoDB - 更新数组中的文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 03:30