从文档中检索单个属性 [英] Retrieve a single property from document

查看:52
本文介绍了从文档中检索单个属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好. 假设我们有一个集合和一个看起来像这样的文档:

Good day everyone. Suppose we have a collection and a document which looks something like this:

    test_doc = {
    "ID" : "123",
    "a" : [
                    {
                        'x' : "/",
                        'y' : "2000",
                        'z' : "1000"
                    },
                    {
                        'x' : "/var",
                        'y' : "3500",
                        'z' : "3000"
                    }
           ]

      }

我需要检索a.z的单个属性. 在MongoDB中,我使用以下查询:

What i need is to retrieve a single property a.z . In MongoDB i'm using the following query:

db.testcol.find({"ID":"123","a.x":"/"},{'a.z':1})

返回以下内容:

{ "_id" : ObjectId("skipped"), "a" : [ { "z" : "1000" }, { "z" : "3000" } ] }

如您所见,它返回所有z属性,但是当条件为{"ID":"123","a.x":"/var"}时,我只需要第一个或第二个 所以,问题是:在这种情况下我如何获得单一财产?这仅仅是设计不好的问题还是我应该以某种方式在代码(python)中处理返回的文档?任何建议将不胜感激.

As you can see it returns all the z properties, but i need only the first one or the second when condition is {"ID":"123","a.x":"/var"} So, the question is: how do i get a single property in this situation? Is it just a matter of bad design or should i somehow process the returned document in code (python)? Any suggestions will be much appreciated.

推荐答案

在MongoDB 2.0及更低版本中,这是不可能的.您想要做的是返回数组的特定元素-但这不是您的投影实际执行的操作,它只会返回整个数组,然后返回每个数组的z元素.

In MongoDB 2.0 and older, this is not possible. What you want to do is return a specific element of the array - but that is not what your projection is actually doing, it will just return the whole array and then the z element of each one.

但是,在2.2(编写此答案时为rc2)的情况下,情况有所改善.现在,您可以将 $ elemMatch 用作投影的一部分(有关详细信息,请参见 SERVER-2238 ),这样您只需拉回所需的数组元素即可.因此,尝试这样的事情:

However, with 2.2 (rc2 as of writing this answer), things have gotten a bit better. You can now use $elemMatch as part of your projection (see SERVER-2238 for details) so that you only pull back the required array element. So, try something like this:

db.foo.find({"ID":"123",'a':{$elemMatch:{'x':"/"}}},{_id : 0, 'a.$': 1})
//returns
{ "a" : [ { "x" : "/", "y" : "2000", "z" : "1000" } ] }

或者,只需在投影本身中使用$ elemMatch,您可能认为它更干净:

Or, just use $elemMatch in the projection itself, which you may think is cleaner:

db.foo.find({"ID":"123"},{_id : 0, 'a':{$elemMatch:{'x':"/"}}})
//returns 
{ "a" : [ { "x" : "/", "y" : "2000", "z" : "1000" } ] }

因此,现在,至少返回的数组仅是仅包含所需条目的数组,您可以简单地引用相关的z元素(尚不支持子文档上的elemMatch投影).

So, now, at least the array returned is only the one containing only the entries you want and you can simply reference the relevant z element (elemMatch projections on a subdocument are not yet supported).

最后但并非最不重要的一点是,在2.2中,我们有了聚合框架,它可以做的一件事情(使用$project运算符,是对文档进行整形并将子文档和数组元素更改为顶级数组.得到您想要的结果,您将执行以下操作:

Last but not least, in 2.2 we have the aggregation framework, and one of the things it can do (with the $project operator, is to reshape your documents and change sub documents and array elements into top level arrays. To get your desired result, you would do something like this:

db.foo.aggregate( 
        {$match : {"ID":"123"}},  
        {$unwind : "$a"},  
        {$match : {"a.x":"/"}},  
        {$project : {_id : 0, z : "$a.z"}}
)

结果如下:

{ "result" : [ { "z" : "1000" } ], "ok" : 1 }

这篇关于从文档中检索单个属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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