时间:2021-07-01 10:21:17 帮助过:4人阅读
public class MongoTest { public static void main(String[] args) throws UnknownHostException { MongoClient mongoClient = new MongoClient( "localhost" , 30000 ); DB db = mongoClient.getDB( "mytest" ); // 返回当前的数据库名称 // Set<String> colls = db.getCollectionNames(); // // for (String s : colls) { // System.out.println(s); // } DBCollection collection=db.getCollection("test"); BasicDBObject doc = new BasicDBObject("name", "xiaohua2"); collection.insert(doc); System.out.println(collection.count()); mongoClient.close(); /* //得到一个集合。这个集合就是用来做crud的接口 DBCollection coll = db.getCollection("mydb"); //插入一个document,和sql的表差点儿相同 //The _id element has been added automatically by MongoDB to your document. //Remember, MongoDB reserves element names that start with “_”/”$” for internal use BasicDBObject doc = new BasicDBObject("name", "MongoDB") .append("type", "database") .append("count", 1) .append("info", new BasicDBObject("x", 203).append("y", 102)); coll.insert(doc); //得到第一条document DBObject myDoc = coll.findOne(); System.out.println(myDoc); //多条数据插入 for (int i=0; i < 100; i++) { coll.insert(new BasicDBObject("i", i)); } //统计document的行数 System.out.println(coll.getCount()); //使用游标 DBCursor cursor = coll.find(); try { while(cursor.hasNext()) { System.out.println(cursor.next()); } } finally { cursor.close(); } mongoClient.close(); //查询 // BasicDBObject query = new BasicDBObject("i", 71); // cursor = coll.find(query); // // try { // while(cursor.hasNext()) { // System.out.println(cursor.next()); // } // } finally { // cursor.close(); // } */ } }
注意记得将mongoclient close掉
备注,假设转载:请标明出处blog.csdn.net/xh199110 飞天博客
假设有写的不正确的地方,欢迎指正。
作者也是看官方网站,查找信息,再加上自己的理解,我写这篇文章,这样我们就一起来学习。谢谢
版权声明:本文博主原创文章。博客,未经同意,不得转载。
Mongodb 之insert瞬时完成,测试数据---飞天博客
标签: