MongoDB Java基本操作
2017-03-07 10:00
399 查看
try{
// 连接到 mongodb 服务
MongoClient mongoClient = new MongoClient( "localhost" , 27017 );//(localhost:ip,27017 :端口号)
// 连接到数据库
MongoDatabase mongoDatabase = mongoClient.getDatabase("test");
System.out.println("Java连接mongodb数据库成功");
//创建集合
// mongoDatabase.createCollection("test");
//System.out.println("创建test集合成功");
MongoCollection<Document> collection = mongoDatabase.getCollection("col");
// MongoCollection<DBObject> collections=mongoDatabase.getWriteConcern();
System.out.println("集合 col选择成功:");
//插入文档
/* Document document=new Document("title", "员工信息").append("name", "蔡勇").append("sex", "男").append("age", 23).append("address", "北京市昌平区沙河高教园北街家园八区");
List<Document> list=new ArrayList<Document>();
list.add(document);
collection.insertMany(list);
System.out.println("插入文档成功");*/
//批量插入
List<Document> dblist=new LinkedList<Document>();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
for(int a=1;a<10;a++){
for(int i=1;i<100;i++){
Document obj=new Document();
obj.put("title", "员工信息");
obj.put("age", 23);
obj.put("sex", "男");
obj.put("tel", "18423560131");
obj.put("time", new Date());
dblist.add(obj);
}
}
collection.insertMany(dblist);
//查询所有文档
FindIterable<Document> findIterable= collection.find();
MongoCursor<Document> mongoCursor = findIterable.iterator();
while(mongoCursor.hasNext()){
System.out.println(mongoCursor.next());
}
//更新文档
// collection.updateMany(Filters.eq("name", "张三"), new Document("$set",new Document("name","张三")));
//删除第一个文档
//删除符合条件的第一个文档
// collection.deleteOne(Filters.eq("name", "张三"));
//删除所有符合条件的文档
collection.deleteMany (Filters.eq("age", 23));
}catch(Exception e){
System.err.println( e.getClass().getName() + ": " + e.getMessage() );
}
// 连接到 mongodb 服务
MongoClient mongoClient = new MongoClient( "localhost" , 27017 );//(localhost:ip,27017 :端口号)
// 连接到数据库
MongoDatabase mongoDatabase = mongoClient.getDatabase("test");
System.out.println("Java连接mongodb数据库成功");
//创建集合
// mongoDatabase.createCollection("test");
//System.out.println("创建test集合成功");
MongoCollection<Document> collection = mongoDatabase.getCollection("col");
// MongoCollection<DBObject> collections=mongoDatabase.getWriteConcern();
System.out.println("集合 col选择成功:");
//插入文档
/* Document document=new Document("title", "员工信息").append("name", "蔡勇").append("sex", "男").append("age", 23).append("address", "北京市昌平区沙河高教园北街家园八区");
List<Document> list=new ArrayList<Document>();
list.add(document);
collection.insertMany(list);
System.out.println("插入文档成功");*/
//批量插入
List<Document> dblist=new LinkedList<Document>();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
for(int a=1;a<10;a++){
for(int i=1;i<100;i++){
Document obj=new Document();
obj.put("title", "员工信息");
obj.put("age", 23);
obj.put("sex", "男");
obj.put("tel", "18423560131");
obj.put("time", new Date());
dblist.add(obj);
}
}
collection.insertMany(dblist);
//查询所有文档
FindIterable<Document> findIterable= collection.find();
MongoCursor<Document> mongoCursor = findIterable.iterator();
while(mongoCursor.hasNext()){
System.out.println(mongoCursor.next());
}
//更新文档
// collection.updateMany(Filters.eq("name", "张三"), new Document("$set",new Document("name","张三")));
//删除第一个文档
//删除符合条件的第一个文档
// collection.deleteOne(Filters.eq("name", "张三"));
//删除所有符合条件的文档
collection.deleteMany (Filters.eq("age", 23));
}catch(Exception e){
System.err.println( e.getClass().getName() + ": " + e.getMessage() );
}
相关文章推荐
- Java MongoDB基本操作
- java对mongoDB的基本操作
- java访问mongodb的基本操作
- Java MongoDB 基本操作
- 用java实现对MongoDB的基本操作(增删改查)
- MongoDB 3.x java基本操作-CRUD
- java对mongoDB 的基本操作
- mongDB基本命令和Java操作MongoDB
- mongodb基本命令和Java操作API示例
- 用java实现对MongoDB的基本操作(增删改查)
- MongoDB 3.X JAVA基本操作
- mongodb之java基本操作
- Java 操作MongoDB 基本操作
- Java 中对Mongodb 的基本操作
- Java mongodb 基本操作入门
- java操作mongodb:基本的增删改查
- MongoDB在java中的操作-添,删,查 基本数据 文件存储 可使用
- MongoDB-Java的两个基本操作Upsert和insertMany
- Java基本文件操作大全
- java对基本数据类型的流的操作