mongodb中2D嵌套数组的投影和索引

时间:2014-01-21 02:58:25

标签: mongodb multidimensional-array mongodb-indexes

> 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嵌套数组。尽我所知能告诉,这不是重复的。)

  • 有没有办法在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'] ] }

基本上切片内部数组。

1 个答案:

答案 0 :(得分:0)

您可以使用位置$运算符进行投影: 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