MongoDB的:在阵列的不存在的文件的增量$ [英] mongoDB: $inc of a nonexistent document in an array

查看:172
本文介绍了MongoDB的:在阵列的不存在的文件的增量$的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不能写一个code,这将能够在一个数组递增一个不存在的价值。

I was not able to write a code, which would be able to increment a non-existent value in an array.

让我们考虑一个如下的结构在蒙戈集合。 (这不是我们用实际的结构,但它保持了问题)

Let's consider a following structure in a mongo collection. (This is not the actual structure we use, but it maintains the issue)

{
        "_id" : ObjectId("527400e43ca8e0f79c2ce52c"),
        "content" : "Blotted Science",
        "tags_with_ratings" : [
                {
                        "ratings" : {
                                "0" : 6154,
                                "1" : 4974
                        },
                        "tag_name" : "math_core"
                },
                {
                        "ratings" : {
                                "0" : 154,
                                "1" : 474,
                        },
                        "tag_name" : "progressive_metal"
                }
        ]
}

问题的例子:我们想添加这个文件到tags_with_ratings属性的等级标签,这是没有添加在阵列中的一个增量。例如,我们将要增加一个TAG_NAME的dubstep0值。

Example issue: We want to add to this document into the tags_with_ratings attribute an incrementation of a rating of a tag, which is not yet added in the array. For example we would want to increment a "0" value for a tag_name "dubstep".

所以,预期的行为是,这将蒙哥UPSERT这样的文档到tags_with_ratings属性:

So the expected behaviour would be, that mongo would upsert a document like this into the "tags_with_ratings" attribute:

            {
                    "ratings" : {
                            "0" : 1
                    },
                    "tag_name" : "dubstep"
            }

目前,我们需要有一个读操作时,它检查是否为标记的嵌套的文件是存在的。如果不是,我们拉数组tags_with_ratings出来,创建一个新的,从previous一个重新添加值,并在那里加入新的嵌套文档。我们是不是应该能够与一个UPSERT操作要做到这一点,而无需昂贵的读取发生的呢?

At the moment, we need to have one read operation, which checks if the nested document for the tag is there. If it's not, we pull the array tags_with_ratings out, create a new one, re-add the values from the previous one and add the new nested document in there. Shouldn't we be able to do this with one upsert operation, without having the expensive read happen?

值的增量占据了90%的过程中,超过一半是通过读取消耗,因为我们无法使用创建属性的$ INC能力,如果它是不存在的数组中为止。

The incrementation of the values takes up 90% of the process and more than half of it is consumed by reading, because we are unable to use $inc capability of creating an attribute, if it is non-existent in the array.

推荐答案

您不能达到你想要使用此架构一步到位的。

You cannot achieve what you want with one step using this schema.

您可以做到这一点但是如果你使用 TAG_NAME 作为键名,而不是使用评分在那里,但随后查询时,你可能有一个不同的问题。

You could do it however if you used tag_name as the key name instead of using ratings there, but then you may have a different issue when querying.

如果该TAG_NAME值的字段名(替换评级)你不得不 {的dubstep:{0:1}} 而不是 {收视率:{0:1},TAG_NAME:的dubstep} 您可以动态更新你想要的方式。只要记住,这个架构将变得更加困难查询 - 你必须知道要收视率事先什么能够通过键名查询

If the tag_name value was the field name (replacing ratings) you'd have {"dubstep":{"0":1}} instead of { "ratings" : {"0" : 1},"tag_name" : "dubstep"} which you can update dynamically the way you want to. Just keep in mind that this schema will make it more difficult to query - you have to know what the ratings are in advance to be able to query by keyname.

这篇关于MongoDB的:在阵列的不存在的文件的增量$的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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