【问题标题】:projections and index on 2D nested arrays in mongodbmongodb中二维嵌套数组的投影和索引
【发布时间】:2014-01-21 02:58:25
【问题描述】:
> db.foo.save({'foo': [{f0: 'a', f1: 'b'}, {f0: 'c', f1: 'd'}]})
> db.foo.save({'foo': [{f0: 'a', f1: 'e'}, {f0: 'f', f1: 'g'}]})
> db.foo.save({'foo': [['a', 'b'], ['c', 'd']]})
> db.foo.save({'foo': [['a', 'e'], ['f', 'g']]})
> db.foo.find({}, {'foo.f1': 1})
{ "_id" : ObjectId("52dddf7cbeb971f4081ea48a"), "foo" : [ { "f1" : "b" }, { "f1" : "d" } ] }
{ "_id" : ObjectId("52dddf83beb971f4081ea48b"), "foo" : [ { "f1" : "e" }, { "f1" : "g" } ] }
{ "_id" : ObjectId("52dddf88beb971f4081ea48c"), "foo" : [ [ ], [ ] ] }
{ "_id" : ObjectId("52dddf8dbeb971f4081ea48d"), "foo" : [ [ ], [ ] ] }
> db.foo.find({}, {'foo.1': 1})
{ "_id" : ObjectId("52dddf7cbeb971f4081ea48a"), "foo" : [ { }, { } ] }
{ "_id" : ObjectId("52dddf83beb971f4081ea48b"), "foo" : [ { }, { } ] }
{ "_id" : ObjectId("52dddf88beb971f4081ea48c"), "foo" : [ [ ], [ ] ] }
{ "_id" : ObjectId("52dddf8dbeb971f4081ea48d"), "foo" : [ [ ], [ ] ] }

我有几个与这样的嵌套数组相关的问题(请注意,几乎所有标题中带有嵌套数组的 SO 问题实际上都是指嵌套在根文档中的单个数组,而不是 2D 嵌套数组。尽我所能能够分辨,这不是重复的)。

  • 有没有办法在二维嵌套数组上执行投影,如上所述?
  • 如何在 foo 数组的第二个元素上创建索引?同样,大概 foo.1 不起作用。

我知道正确的答案 (TM) 是不这样做并使用一组子文档,虚拟 (NDTAUAAOSD) 但 a) 好奇心 - 我似乎找不到答案 b) 不幸的是,情况超出了我的控制指定文档结构。

更新:澄清我希望从预测中看到的内容:

db.foo.find({}, {'foo.1': 1})
{ "_id" : ObjectId("52dddf88beb971f4081ea48c"), "foo" : [ ['b'], ['d'] ] }
{ "_id" : ObjectId("52dddf8dbeb971f4081ea48d"), "foo" : [ ['e'], ['g'] ] }

基本上是对内部数组进行切片。

【问题讨论】:

    标签: mongodb multidimensional-array mongodb-indexes


    【解决方案1】:

    您可以使用位置$ 运算符进行投影: http://docs.mongodb.org/manual/reference/operator/projection/positional/

    虽然我不完全确定您的查询试图预测什么,但这里有一个示例:

    > db.foo.find()
    { "_id" : ObjectId("5321ac073ac852396029fb90"), "foo" : [  {  "f0" : "a",  "f1" : "b" },  {  "f0" : "c",  "f1" : "d" } ] }
    { "_id" : ObjectId("5321ac073ac852396029fb91"), "foo" : [  {  "f0" : "a",  "f1" : "e" },  {  "f0" : "f",  "f1" : "g" } ] }
    { "_id" : ObjectId("5321ac073ac852396029fb92"), "foo" : [  {  "f0" : "a",  "f1" : "b" },  {  "f0" : "c",  "f1" : "d" },  { "f0" : "a", "f1" : "z" } ] }
    { "_id" : ObjectId("5321ac073ac852396029fb93"), "foo" : [  [  "a",  "b" ],  [  "c",  "d" ] ] }
    { "_id" : ObjectId("5321ac073ac852396029fb94"), "foo" : [  [  "a",  "e" ],  [  "f",  "g" ] ] }
    { "_id" : ObjectId("5321ac073ac852396029fb95"), "foo" : [  [  "a",  "e" ],  [  "f",  "g" ],  [  "a",  "z" ] ] }
    
    // get the array document which has a field "f0" which matches "a"
    b.foo.find({ "foo.f0" : "a" }, { "foo.$" : 1 })
    { "_id" : ObjectId("5321ac073ac852396029fb90"), "foo" : [  {  "f0" : "a",  "f1" : "b" } ] }
    { "_id" : ObjectId("5321ac073ac852396029fb91"), "foo" : [  {  "f0" : "a",  "f1" : "e" } ] }
    { "_id" : ObjectId("5321ac073ac852396029fb92"), "foo" : [  {  "f0" : "a",  "f1" : "b" } ] }
    // ^ see that the last return document only returns the first array element match
    
    // get the array element of the foo array
    > db.foo.find({ "foo" : { "$elemMatch" : { "$in" : [ "a" ] } } }, { "foo.$" : 1 })
    { "_id" : ObjectId("5321ac073ac852396029fb93"), "foo" : [  [  "a",  "b" ] ] }
    { "_id" : ObjectId("5321ac073ac852396029fb94"), "foo" : [  [  "a",  "e" ] ] }
    { "_id" : ObjectId("5321ac073ac852396029fb95"), "foo" : [  [  "a",  "e" ] ] }
    // ^ see that the last return document only returns the first array element match
    

    【讨论】:

    • 澄清了 OQ 以指定我希望重新投影 2D 数组的内容。
    猜你喜欢
    • 2015-05-12
    • 2016-02-14
    • 1970-01-01
    • 2015-02-01
    • 2018-02-20
    • 2016-07-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-28
    相关资源
    最近更新 更多