1.获取表
import com.mongodb.MongoClient;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.MongoIterable;
public class GetCollection {
public static void main(String[] args) {
MongoClient mc = new MongoClient("localhost", 27017);
MongoDatabase db = mc.getDatabase("student");
MongoIterable<String> names = db.listCollectionNames();
MongoCursor<String> iterator = names.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
}
}
2.新增
import java.util.Date;
import org.bson.Document;
import com.ape.bean.Student;
import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
public class AddStudent {
public static void main(String[] args) {
Student s = new Student();