技术开发 频道

Mongodb快速入门之使用Java操作Mongodb

  更新Document

  假设如下的JSON格式的数据已经保存到Mongodb中去了,现在要更新相关的数据。

  {"_id" : {"$oid" : "x"} , "hosting" : "hostA" , "type" : "vps" , "clients" : 1000}
  {
"_id" : {"$oid" : "x"} , "hosting" : "hostB" , "type" : "dedicated server" , "clients" : 100}
  {
"_id" : {"$oid" : "x"} , "hosting" : "hostC" , "type" : "vps" , "clients" : 900}

   假设现在要将hosting中值为hostB的进行更新,则可以使用如下的方法:

  BasicDBObject newDocument =new BasicDBObject();
  newDocument.put(
"hosting", "hostB");
  newDocument.put(
"type", "shared host");
  newDocument.put(
"clients", 111);
  collection.update(
new BasicDBObject().append("hosting", "hostB"), newDocument);

   可以看到,这里依然使用了BasicDBObject对象,并为其赋值了新的值后,然后使用collection的update方法,即可更新该对象。

  更新后的输出如下:

  {"_id" : {"$oid" : "x"} , "hosting" : "hostA" , "type" : "vps" , "clients" : 1000}
  {
"_id" : {"$oid" : "x"} , "hosting" : "hostB" , "type" : "shared host" , "clients" : 111}
  {
"_id" : {"$oid" : "x"} , "hosting" : "hostC" , "type" : "vps" , "clients" : 900}

   另外,还可以使用mongodb中的$inc修饰符号去对某个值进行更新,比如,要将hosting值为hostB的document的clients的值得更新为199(即100+99=199),可以这样:

  BasicDBObject newDocument =new BasicDBObject().append("$inc",
  
new BasicDBObject().append("clients", 99));
  collection.update(
new BasicDBObject().append("hosting", "hostB"), newDocument);

   则输出如下:

  {"_id" : {"$oid" : "x"} , "hosting" : "hostA" , "type" : "vps" , "clients" : 1000}
  {
"_id" : {"$oid" : "x"} , "hosting" : "hostB" , "type" : "dedicated server" , "clients" : 199}
  {
"_id" : {"$oid" : "x"} , "hosting" : "hostC" , "type" : "vps" , "clients" : 900}

   接下来,讲解$set修饰符的使用。比如要把hosting中值为hostA的document中的

  type的值进行修改,则可以如下实现:

  BasicDBObject newDocument3 =new BasicDBObject().append("$set",
  
new BasicDBObject().append("type", "dedicated server"));
  collection.update(
new BasicDBObject().append("hosting", "hostA"), newDocument3);

   则输出如下,把type的值从vps改为dedicated server:

  {"_id" : {"$oid" : "x"} , "hosting" : "hostB" , "type" : "dedicated server" , "clients" : 100}
  {
"_id" : {"$oid" : "x"} , "hosting" : "hostC" , "type" : "vps" , "clients" : 900}
  {
"_id" : {"$oid" : "x"} , "hosting" : "hostA" , "clients" : 1000 , "type" : "dedicated server"}

   要注意的是,如果不使用$set的修饰符,而只是如下代码:

  BasicDBObject newDocument3 =new BasicDBObject().append("type", "dedicated server");
  collection.update(
new BasicDBObject().append("hosting", "hostA"), newDocument3);

   则会将所有的三个document的type类型都改为dedicated server了,因此要使用$set以更新特定的document的特定的值。

  如果要更新多个document中相同的值,可以使用$multi,比如,要把所有vps为type的document,将它们的clients的值更新为888,可以如下实现:

  BasicDBObject updateQuery =new BasicDBObject().append("$set",
  
new BasicDBObject().append("clients", "888"));
  collection.update(
new BasicDBObject().append("type", "vps"), updateQuery, false, true);
 

  输出如下:

  {"_id" : {"$oid" : "x"} , "hosting" : "hostA" , "clients" : "888" , "type" : "vps"}
  {
"_id" : {"$oid" : "x"} , "hosting" : "hostB" , "type" : "dedicated server" , "clients" : 100}
  {
"_id" : {"$oid" : "x"} , "hosting" : "hostC" , "clients" : "888" , "type" : "vps"}
1
相关文章