Java MongoDb更改数组中的值

时间:2017-03-19 15:34:23

标签: java arrays mongodb

我正在寻找一种方法来改变数组中只有1个整数的值。 我不想获取整个数组,更改值并将其重新放回,因为另一个线程可以更改整数的值。

[
  {
    "row 1": [
      0,
      0,
      0,
      0,
      0
    ],
    "row 2": [
      0,
      0,
      0,
      0,
      0
    ],
    "row 3": [
      0,
      0,
      0,
      0,
      0
    ],
    "row 4": [
      0,
      0,
      0,
      0,
      0
    ],
    "row 5": [
      0,
      0,
      0,
      0,
      0
    ],
    "row 6": [
      0,
      0,
      0,
      0,
      0
    ],
    "row 7": [
      0,
      0,
      0,
      0,
      0
    ],
    "row 8": [
      0,
      0,
      0,
      0,
      0
    ]
  }
]

这是“showInstance”文档中的“席位”文档,“showInstance”文档位于“show”文档中。

这是展示文件

{
  "_id": 1,
  "name": "Great rock show",
  "description": "The party will roll all night long during the North American WorldWired tour as we’re psyched to announce that award-winning DJ and producer Mix Master Mike will be spinning some of your favorite (and ours!) hard rock and metal tracks throughout the evening at all of the stadium dates this summer. A pioneer in the hip-hop and DJ communities, you’ll be able to catch his skillful, hard-hitting scratch work once the doors open each night and between acts before we hit the stage.",
  "band_id": 3,
  "instances": [
    {
      "_id": {
        "$oid": "58cea4779f3489310471b7db"
      },
      "date": {
        "$date": "2017-04-07T15:00:00.000Z"
      },
      "price": 140,
      "theater_id": 3,
      "seats": [
        {
          "row 1": [
            0,
            0,
            0,
            0,
            0
          ],
          "row 2": [
            0,
            0,
            0,
            0,
            0
          ],
          "row 3": [
            0,
            0,
            0,
            0,
            0
          ],
          "row 4": [
            0,
            0,
            0,
            0,
            0
          ],
          "row 5": [
            0,
            0,
            0,
            0,
            0
          ],
          "row 6": [
            0,
            0,
            0,
            0,
            0
          ],
          "row 7": [
            0,
            0,
            0,
            0,
            0
          ],
          "row 8": [
            0,
            0,
            0,
            0,
            0
          ]
        }
      ]
    },
    {
      "_id": {
        "$oid": "58cea4779f3489310471b7dc"
      },
      "date": {
        "$date": "2017-05-04T15:00:00.000Z"
      },
      "price": 758,
      "theater_id": 5,
      "seats": [
        {
          "row 1": [
            0,
            0,
            0,
            0,
            0,
            0
          ],
          "row 2": [
            0,
            0,
            0,
            0,
            0,
            0
          ],
          "row 3": [
            0,
            0,
            0,
            0,
            0,
            0
          ],
          "row 4": [
            0,
            0,
            0,
            0,
            0,
            0
          ],
          "row 5": [
            0,
            0,
            0,
            0,
            0,
            0
          ],
          "row 6": [
            0,
            0,
            0,
            0,
            0,
            0
          ],
          "row 7": [
            0,
            0,
            0,
            0,
            0,
            0
          ],
          "row 8": [
            0,
            0,
            0,
            0,
            0,
            0
          ],
          "row 9": [
            0,
            0,
            0,
            0,
            0,
            0
          ],
          "row 10": [
            0,
            0,
            0,
            0,
            0,
            0
          ]
        }
      ]
    },
    {
      "_id": {
        "$oid": "58cea4779f3489310471b7dd"
      },
      "date": {
        "$date": "2017-05-23T15:00:00.000Z"
      },
      "price": 634,
      "theater_id": 9,
      "seats": [
        {
          "row 1": [
            0,
            0,
            0,
            0,
            0
          ],
          "row 2": [
            0,
            0,
            0,
            0,
            0
          ],
          "row 3": [
            0,
            0,
            0,
            0,
            0
          ],
          "row 4": [
            0,
            0,
            0,
            0,
            0
          ]
        }
      ]
    },
    {
      "_id": {
        "$oid": "58cea4779f3489310471b7de"
      },
      "date": {
        "$date": "2017-06-02T15:00:00.000Z"
      },
      "price": 398,
      "theater_id": 3,
      "seats": [
        {
          "row 1": [
            0,
            0,
            0,
            0,
            0
          ],
          "row 2": [
            0,
            0,
            0,
            0,
            0
          ],
          "row 3": [
            0,
            0,
            0,
            0,
            0
          ],
          "row 4": [
            0,
            0,
            0,
            0,
            0
          ],
          "row 5": [
            0,
            0,
            0,
            0,
            0
          ],
          "row 6": [
            0,
            0,
            0,
            0,
            0
          ],
          "row 7": [
            0,
            0,
            0,
            0,
            0
          ],
          "row 8": [
            0,
            0,
            0,
            0,
            0
          ]
        }
      ]
    },
    {
      "_id": {
        "$oid": "58cea4779f3489310471b7df"
      },
      "date": {
        "$date": "2017-06-29T15:00:00.000Z"
      },
      "price": 36,
      "theater_id": 9,
      "seats": [
        {
          "row 1": [
            0,
            0,
            0,
            0,
            0
          ],
          "row 2": [
            0,
            0,
            0,
            0,
            0
          ],
          "row 3": [
            0,
            0,
            0,
            0,
            0
          ],
          "row 4": [
            0,
            0,
            0,
            0,
            0
          ]
        }
      ]
    },
    {
      "_id": {
        "$oid": "58cea4779f3489310471b7e0"
      },
      "date": {
        "$date": "2017-08-08T15:00:00.000Z"
      },
      "price": 377,
      "theater_id": 11,
      "seats": [
        {
          "row 1": [
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0
          ],
          "row 2": [
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0
          ],
          "row 3": [
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0
          ],
          "row 4": [
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0
          ],
          "row 5": [
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0
          ],
          "row 6": [
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0
          ],
          "row 7": [
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0
          ],
          "row 8": [
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0
          ],
          "row 9": [
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0
          ],
          "row 10": [
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0
          ]
        }
      ]
    },
    {
      "_id": {
        "$oid": "58cea4779f3489310471b7e1"
      },
      "date": {
        "$date": "2017-08-28T15:00:00.000Z"
      },
      "price": 885,
      "theater_id": 12,
      "seats": [
        {
          "row 1": [
            0,
            0,
            0,
            0,
            0,
            0,
            0
          ],
          "row 2": [
            0,
            0,
            0,
            0,
            0,
            0,
            0
          ],
          "row 3": [
            0,
            0,
            0,
            0,
            0,
            0,
            0
          ],
          "row 4": [
            0,
            0,
            0,
            0,
            0,
            0,
            0
          ],
          "row 5": [
            0,
            0,
            0,
            0,
            0,
            0,
            0
          ],
          "row 6": [
            0,
            0,
            0,
            0,
            0,
            0,
            0
          ],
          "row 7": [
            0,
            0,
            0,
            0,
            0,
            0,
            0
          ],
          "row 8": [
            0,
            0,
            0,
            0,
            0,
            0,
            0
          ],
          "row 9": [
            0,
            0,
            0,
            0,
            0,
            0,
            0
          ],
          "row 10": [
            0,
            0,
            0,
            0,
            0,
            0,
            0
          ],
          "row 11": [
            0,
            0,
            0,
            0,
            0,
            0,
            0
          ],
          "row 12": [
            0,
            0,
            0,
            0,
            0,
            0,
            0
          ]
        }
      ]
    },
    {
      "_id": {
        "$oid": "58cea4779f3489310471b7e2"
      },
      "date": {
        "$date": "2017-09-26T15:00:00.000Z"
      },
      "price": 853,
      "theater_id": 3,
      "seats": [
        {
          "row 1": [
            0,
            0,
            0,
            0,
            0
          ],
          "row 2": [
            0,
            0,
            0,
            0,
            0
          ],
          "row 3": [
            0,
            0,
            0,
            0,
            0
          ],
          "row 4": [
            0,
            0,
            0,
            0,
            0
          ],
          "row 5": [
            0,
            0,
            0,
            0,
            0
          ],
          "row 6": [
            0,
            0,
            0,
            0,
            0
          ],
          "row 7": [
            0,
            0,
            0,
            0,
            0
          ],
          "row 8": [
            0,
            0,
            0,
            0,
            0
          ]
        }
      ]
    }
  ],
    }
  }
}

1 个答案:

答案 0 :(得分:0)

如果您知道元素位置,则可以使用它来更新值,例如

BasicDBObject setQuery = new BasicDBObject("$set", new BasicDBObject("instances.$.seats.0.row 1.0", 1));
collection.update(someSearchQuery, setQuery);

<强>更新

以下是查询和更新的示例:

DBCollection collection = db.getCollection("shows");
BasicDBObject query = new BasicDBObject("_id", 1);
query.put("instances._id", new ObjectId("58cea4779f3489310471b7db"));

BasicDBObject setQuery = new BasicDBObject("$set", new BasicDBObject("instances.$.seats.0.row 1.0", 1));
collection.update(query, setQuery);

Mongo查询

db.seats.update({"_id":1,"instances._id": ObjectId("58cea4779f3489310471b7db")}, {$set : {"instances.$.seats.0.row 1.0" : 100}})

上面的查询应该将“row 1”元素更新为100,我也更新了java exaple。