补全代码public class Mongo{ public static void main( String args[] ){ try{ MongoClient mongoClient = new MongoClient("Iocalhost",27017); MongoDatabase mongoDatabase = mongoClient.getDatabase(" mydb2"); mongoDatabase.createCollection("_ (1)_ "); MongoC ollection<Document> collection = mongoDatabase.getCollection("test2"); Document document1 = new Document(; document1._ (2)_ document1.append(" name","Xiaoming"); document1.append(" sex",' man"); document1.append(" age" ,21); Document document2 = new Document(); document2.append("_ id","2"); document2.append("name","Xiaohong"); document2.append(" sex' ;' woman"); document2.append(" age" ,20); Document document3 = new Document0; document3.append(" id","3"); document3.append("name","Xiaoliang"); document3.append("sex"," man"); document3.append(" age" ,22); List <Document> documents = new ArrayList <Document>0; (3)__ ; documents.add(document2); documents. add(document3); collection.__ (4)__ ; Findlterable <Document> iter =__ (5)__ ; System.out.printn("文档插入结果如下: "); iter.forEach(new Block <Document>() { public void apply(Document _doc) { System.out.printIn( _doc.tolson0); ); collection.updateMany(Filters.eq( 'name", (6)__ ), collection.updateMany(Filters.eq("name",___ (6)_ ), new Document(" $set",new Document(" age" ,3))); Findlterable <Document> findlterable = collection.find0; MongoCursor<Document> mongoCursor =__ (Z)__ ; System.out.printIn("更新后文档内容如下: "); while(mongoCursor.hasNext0){ System.out.println(mongoCursor.next0); }; collection.deleteMany ( (8)_ );
时间: 2024-04-01 11:33:05 浏览: 68
public class Mongo {
public static void main( String args[] ){
try{
MongoClient mongoClient = new MongoClient("localhost", 27017);
MongoDatabase mongoDatabase = mongoClient.getDatabase("mydb2");
mongoDatabase.createCollection("test2");
MongoCollection<Document> collection = mongoDatabase.getCollection("test2");
Document document1 = new Document();
document1.append("name", "Xiaoming");
document1.append("sex", "man");
document1.append("age", 21);
Document document2 = new Document();
document2.append("_id", "2");
document2.append("name", "Xiaohong");
document2.append("sex", "woman");
document2.append("age", 20);
Document document3 = new Document();
document3.append("id", "3");
document3.append("name", "Xiaoliang");
document3.append("sex", "man");
document3.append("age", 22);
List<Document> documents = new ArrayList<Document>();
documents.add(document2);
documents.add(document3);
collection.insertMany(documents);
FindIterable<Document> iter = collection.find();
System.out.println("文档插入结果如下: ");
iter.forEach(new Block<Document>() {
public void apply(Document doc) {
System.out.println(doc.toJson());
}
});
collection.updateMany(Filters.eq("name", "Xiaohong"), new Document("$set", new Document("age", 3)));
FindIterable<Document> findIterable = collection.find();
MongoCursor<Document> mongoCursor = findIterable.iterator();
System.out.println("更新后文档内容如下: ");
while(mongoCursor.hasNext()){
System.out.println(mongoCursor.next());
}
collection.deleteMany(Filters.eq("name", "Xiaohong"));
} catch (Exception e) {
System.err.println( e.getClass().getName() + ": " + e.getMessage() );
}
}
}
阅读全文