java操作mongodb的插入和修改
2013-08-06 11:13 来源:www.chinab4c.com 作者:ecshop专家
java操作mongodb的增加和修改过程,主要是通过使用本地的java来操作远程mongodb数据库,来完成对mongodb数据库的新增,以及修改的mongodb基本操作
package taobao;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.Mongo;
import com.mongodb.MongoException;
import com.mongodb.DBObject;
import com.mongodb.util.JSON;
class java2 {
public static void main(String[] args) {
try{
Mongo mg = new Mongo("127.0.0.1",27017);
DB db = mg.getDB("admin");
boolean ok = db.authenticate("system","system".toCharArray());
//mg.auth("system","system");
//查询所有的Database
for (String name : mg.getDatabaseNames()) {
//System.out.println("dbName: " + name);
}
db = mg.getDB("test");
DBCollection goods = db.getCollection("goods");
BasicDBObject ob = new BasicDBObject();
//ob.put("id",1);
// goods.update(new BasicDBObject().append("name",1),ob,true);
//查询所有的数据
DBCursor cur = goods.find(new BasicDBObject("id",1));
while (cur.hasNext()) {
DBObject x = cur.next();
System.out.println(x.get("name"));
}
/*for(int i=1;i<5;i++) {
BasicDBObject x = new BasicDBObject();
x.put("id",i);
x.put("name","name"+i);
goods.insert(x);
}*/
BasicDBObject a = new BasicDBObject();
BasicDBObject b = new BasicDBObject();
a.put("id",1);
b.put("name","ri");
goods.update(a,new BasicDBObject("$set",b));
}catch(Exception e){
e.printStackTrace();
}
}
}
最近更新
常用插件
- ecshop2.7.0utf给属性增加库存
插件介绍:本插件适合ecshop2.7.0的utf8编码对商品的属性进行库存管理...
- ecshop商品库存缺货发货量
ecshop商品库存缺货发货量数据倒出插件上海汽车租赁www.shrentbus.cn...
- ecshop直接购买流程二次开
ecshop当全场免除运费的时候,其实大可以开发新的直接购买流程,方便...
- ecshop二次开发商品购买备
ecshop销售统计 插件介绍:ecshop二次开发订单销售统计和商品购买备注服...
- ecshop导出会员邮件和手机
ecshop导出会员邮件和手机号插件,主要是用于ecshop系统中促销活动中。获...