获取每个索引的子文档元素的计数的数组内并更新子文档的关键 - 在阵列子文档(在MongoDB中) [英] Getting subdocument element's count per index inside an array and updating the subdocument key - subdocument in array(IN MONGODB)

查看:167
本文介绍了获取每个索引的子文档元素的计数的数组内并更新子文档的关键 - 在阵列子文档(在MongoDB中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获得内部数组子文档元素的数量和如何更新子文档的MongoDB中键

How to get subdocument element's count inside an array and how to update the subdocument's key in MongoDB

有关例如,以下是存储在MongoDB的整个文档:

For eg, following is the whole doc stored in mongodb:

{
    "CompanyCode" : "SNBN",
    "EventCode" : "ET00008352",
    "EventName" : "Sunburn Presents Avicii India Tour",
    "TktDetail" : [ 
        {
            "Type" : "Category I",
            "Qty" : {
                "10-Dec" : {
                    "value" : 58
                },
                "11-Dec" : {
                   "value" : 83
                },
                "12-Dec" : {
                   "value" : 100
                } 
            }
        }, 
        {
            "Type" : "Category II",
            "Qty" : {
                "10-Dec" : {
                   "value" : 4
                },
                "11-Dec" : {
                    "value" : 7
                },
                "12-Dec" : {
                    "value" : 8
                }
            }
        }, 
        {
            "Type" : "PRICE LEVEL 1",
            "Qty" : {
                "11-Dec" : {
                    "value" : 2
                }
            }
        }, 
        {
            "Type" : "CatIV",
            "Qty" : {
                "18-Dec" : {
                    "value" : 20
                }
            }
        }
    ],
    "TransDate" : [ 
        "10-Dec-2013", 
        "11-Dec-2013", 
        "12-Dec-2013", 
    ],
    "VenueCode" : "SNBN",
    "VenueName" : "Sunburn",
    "_id" : ObjectId("52452db273b92012c41ad612")
}

下面TktDetail是一个数组,其内部有一个数量subdoc的,其中包含多个元素,我想知道如何获得每个元素的索引中的数量算?

Here TktDetail is an array, inside which there is a Qty subdoc which contains multiple elements, I want to know how to get the elements count inside Qty per index?

例如,TktDetail数组的第0个索引包含1数量subdoc的,其还具有3的元素个数,而第三个指标在数量subdoc的1元计。

For example, the 0th index of TktDetail array contains 1 Qty subdoc, which further has a element count of 3, whereas 3rd index has element count of 1 in Qty subdoc.

如果我想更新subdoc的关键一样,我想从10 - 12到10 - 12月2013​​更新数量日期,怎么可能?

If I want to update the subdoc key, like, I want to update the date in Qty from "10-Dec" to "10-Dec-2013", how is it possible?

在此先感谢,找了尽快答复..

Thanks in advance, looking for a reply ASAP..

推荐答案

所以这里的第一件事情是,你居然问两个的问题,是的我怎么弄的计数在数量项目吗?的和的我怎么能改名字呢?的。而现在通常无关我要去把它们当作一回事。

So the first thing here is that you actually asked two questions, being "how do I get a count of the items under Qty?" and "how can I change the names?". Now while normally unrelated I'm going to treat them as the same thing.

你需要做的是更改您的模式,并在此过程中我将让你得到的项目数和我要鼓励你改变这些字段名好。具体来说,你需要这样一个模式:

What you need to do is change your schema and in doing so I'm going to allow you to get the count of items and I'm going to encourage you to change those field names as well. Specifically you need a schema like this:

"TktDetail" : [ 
    {
        "Type" : "Category I",
        "Qty" : [
            { "date": ISODate("2013-12-10T00:00:00.000Z") , "value" : 58  },
            { "date": ISODate("2013-12-11T00:00:00.000Z"), "value" : 83  },
            { "date": ISODate("2013-12-01T00:00:00.000Z"), "value" : 100 },
        ]
    },

所有血淋淋的细节都在我的答案这里类似的问题。但问题主要是,当你使用的子文件的的方式,你有你正在毁掉你做这个有意义的查询操作的机会,因为在你的每个元素得到必须指定完整路径到那里。

All the gory details are in my answer here to a similar question. But the problem basically is that when you use sub-documents in the way you have you are ruining your chances of doing any meaningful query operations on this, as to get at each element you must specify the full path to get there.

回答有更多的细节,但案件是你真正想要的数组。权衡,这是一个有点难以更新,特别是考虑到你的嵌套阵列的,但它是一个更容易添加和更容易查询。

That answer has more detail, but the case is you really want an array. The trade-off, it's a little harder to update, especially considering you have nested arrays, but it's a lot easier to add and much easier to query.

另外,和相关的,改变你的日期是日期和不可以字符串。这些字符串,是的没有好作为内MongoDB的比较。有了它们设置为正确的日期BSON(注意我修剪他们一天的开始),你可以比较,和查询范围,做有用的事情。您的应用程序code将竭诚为司机会返回一个真正日期对象,而不是你拥有的东西来处理两种方式。

Also, and related, change your dates to be dates and not strings. The strings, are no good for comparisons inside MongoDB. With them set as proper BSON dates (noting I clipped them to the start of day) you can compare, and query ranges and do useful things. Your application code will be happy to as the driver will return a real date object, rather than something you have to manipulate "both ways".

所以一次您已经阅读过,理解和执行这一点,上计数:

So once you have read through, understood and implemented this, on to counting:

db.collection.aggregate([

    // Unwind the TktDetail array to de-normalize
    {"$unwind": "$TktDetail"},

    // Also Unwind the Qty array
    {"$unwind": "$Qty" },

    // Get some group information and count the entries
    {"$group": { 
        "_id": {
            "_id": "$_id,
            "EventCode": "$EventCode",
            "Type": "$TktDetail.Type"
        },
        "Qty": {"$sum": 1 }
    }},

    // Project nicely
    {"$project": { 
        "_id": 0,
        "EventCode": "$_id.EventCode",
        "Type: "$_id.Type",
        "Qty": 1,
    }},

    // Let's even sort it 
    {"$sort": { "EventCode": 1, "Qty" -1 }}

])

所以这使我们能够在数量获得的计数项目的每个事件code 键入数量命令higest到最低。

So that allowed us to get a count of the items in Qty for each EventCode by Type with the Qty ordered higest to lowest.

这就是无法您目前的模式,而不加载和遍历每个文件code。

And that is not possible on your current schema without loading and traversing each document in code.

所以,有这样的情形。现在,如果你想的忽略此,只是去改变的子文件的键名,那么你需要做的删除键和底层文档,并与更换新项的名称,使用更新:

So there is the case. Now if you want to ignore this and just go about changing the sub-document key names, then you'll need to do remove the key and underlying document and replace with the new key name, using update:

db.collection.update(
    { EventCode: "ET00008352"},
    { $unset:{ "TktDetail.0.Qty.10-Dec": "" }}
)

db.collection.update(
    { EventCode: "ET00008352"},
    { $set:{ "TktDetail.0.Qty.10-Dec-2013": { value: 58 } }}
)

和你需要做就是每一个的项目,你有。

And you'll need to do that for every item that you have.

让你无论是制定出该模式转换或以其他方式,以改变键有工作的很多反正。至于我自己,我会做的正确,只有做到这一点的一次所以我没有碰到一个问题后。

So you either work out that schema conversion or otherwise have a lot of work anyway in order to change the keys. For myself, I'd do it properly, and only do it once so I didn't run into the next problem later.

这篇关于获取每个索引的子文档元素的计数的数组内并更新子文档的关键 - 在阵列子文档(在MongoDB中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆