首先添加mongoDB的驱动包并开启了monGodb的服务package com.zzq.test;import java.net.UnknownHostException;import com.mongo
首先添加mongoDB的驱动包
并开启了monGodb的服务
package com.zzq.test;
import java.net.UnknownHostException;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.Mongo;
import com.mongodb.MongoException;
import com.mongodb.util.JSON;
public class MongDBTest {
public static void main(String[] args) throws UnknownHostException, MongoException {
Mongo mg = new Mongo("localhost", 27017);
//查询所有的Database
for (String name : mg.getDatabaseNames()) {
System.out.println("dbName: " + name);
}
DB db = mg.getDB("mircroblog");
//查询所有的聚集集合
for (String name : db.getCollectionNames()) {
System.out.println("collectionName: " + name);
}
DBCollection users = db.getCollection("citizens");
//查询所有的数据
DBCursor cur = users.find();
while (cur.hasNext()) {
System.out.println(cur.next());
}
System.out.println(cur.count());
System.out.println(cur.getCursorId());
System.out.println(jsON.serialize(cur));
}
}
--结束END--
本文标题: Java连接mongodb
本文链接: https://lsjlt.com/news/43607.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-10-23
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0