MongoDB(Java驱动程序)一次性在子集合中添加元素

MongoDB(Java驱动程序)一次性在子集合中添加元素,mongodb,collections,updates,Mongodb,Collections,Updates,我有一个文件,看起来像这样: { "id":1, "layers": [{ "id" : 100, "files": [{ "id":1, "name":"test" }] }] } @Document(collections="tests") public

我有一个文件,看起来像这样:

{
 "id":1,
 "layers": 
           [{
            "id" : 100,
            "files": [{
                       "id":1,
                       "name":"test"
                     }]
            }]
   }

@Document(collections="tests")
public class Test() {
   string id
   Set<..> layers;
}

public class Layers() {
 List<..> files;
}
当我尝试上述操作时,会出现如下错误: 无法使用零件(contentLayers…)遍历元素

db.test.update(
  { $and: [{_id: "1"}, {"layers.id": "100" }]},
  { $push:
     {
       "layers.$.files": 1
     }
  }
)
这是非java的版本,重要的一点是在数组之前有这个$

注意“可能”还有其他的方法,但上面的方法对我很有效

db.test.update(
  { $and: [{_id: "1"}, {"layers.id": "100" }]},
  { $push:
     {
       "layers.$.files": 1
     }
  }
)