package mongodb;
import java.net.UnknownHostException;
import java.util.List;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.Mongo;
public class MongoCRUD {
public static void main(String[] args) {
try {
这样就创建了一个MongoDB的数据库连接对象,它默认连接到当前机器的localhost地址,端口是27017。
Mongo mongo = new Mongo();
也可以这样 连接数据库
//MongoClient mongoClient = new MongoClient( "localhost" , 27017 );
//获得所有数据库名字
List<String> list = mongo.getDatabaseNames();
for (String dbName : list) {
System.out.println("DataBaseName="+dbName);
}
//得到数据库 如果没有,则创建
DB db = mongo.getDB("myTest");
//获取db数据库的所有表名(在这里用集合表示)
Set<String> set = db.getCollectionNames();
for (String string : set) {
System.out.println("tablesName="+string);
}
//得到该数据库的表 如果没有,则创建
DBCollection coll = db.getCollection("myTables");
//第一种方式用BasicDBObject插入JSON格式数据
BasicDBObject dbObject1 = new BasicDBObject();
dbObject1.append("id", 1).append("name", "jerry").append("age", 23).append("job", "程序员").append("address", "深圳");
BasicDBObject dbObject2 = new BasicDBObject();
dbObject2.put("id", 2);
dbObject2.put("name", "enen");
dbObject2.put("age", 22);
dbObject2.put("address", "惠州");
dbObject2.put("jerry", dbObject1);
coll.insert(dbObject2);
//第二种用BasicDBObjectBuilder
BasicDBObjectBuilder documentBuilder = BasicDBObjectBuilder.start()
.add("database", "mkyongDB")
.add("table", "hosting");
BasicDBObjectBuilder documentBuilderDetail = BasicDBObjectBuilder.start()
.add("records", "99")
.add("index", "vps_index1")
.add("active", "true");
documentBuilder.add("detail", documentBuilderDetail.get());
coll.insert(documentBuilder.get());
//第三种用Map对象
Map documentMap =new HashMap();
documentMap.put("database", "mkyongDB");
documentMap.put("table", "hosting");
Map documentMapDetail =new HashMap();
documentMapDetail.put("records", "99");
documentMapDetail.put("index", "vps_index1");
documentMapDetail.put("active", "true");
documentMap.put("detail", documentMapDetail);
coll.insert(new BasicDBObject(documentMap));
// 第四种方法,也就是最简单的,即直接插入JSON格式数据
String json ="{‘database‘ : ‘mkyongDB‘,‘table‘ : ‘hosting‘,"+
"‘detail‘ : {‘records‘ : 99, ‘index‘ : ‘vps_index1‘, ‘active‘ : ‘true‘}}}";
DBObject dbObject =(DBObject)JSON.parse(json);
coll.insert(dbObject);
//查询所有的数据
DBCursor cursor = coll.find();
System.out.println(cursor.count());
while(cursor.hasNext()){
System.out.println(cursor.next());
}
//删除 找到第一个删除
DBObject findOne = coll.findOne();
coll.remove(findOne);
System.out.println("修改前------------------------");
DBObject updateCondition=new BasicDBObject();
updateCondition.put("name", "enen");
DBObject updatedValue=new BasicDBObject();
updatedValue.put("age", 3);
updatedValue.put("address", "boss");
DBObject updateSetValue=new BasicDBObject("$set",updatedValue);
coll.update(updateCondition, updateSetValue,true,false);//后面两个参数设置,前面一个是where条件不存在时,插入数据。后面一个是是否修改多条记录。
System.out.println("修改后---------------");
DBCursor find = coll.find();
while(find.hasNext()){
DBObject next = find.next();
System.out.println(next);
}
System.out.println(JSON.serialize(cursor));
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
在这里因为没什么时间,所有没有抽取到方法,通过方法调用,挤在一个主类中,有时间在把他重构。