Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Java connection mongodb

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

First add the driver package for mongodb

And opened the mongodb service.

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)

/ / query all Database

For (String name: mg.getDatabaseNames ()) {

System.out.println ("dbName:" + name)

}

DB db = mg.getDB ("mircroblog")

/ / query all aggregate collections

For (String name: db.getCollectionNames ()) {

System.out.println ("collectionName:" + name)

}

DBCollection users = db.getCollection ("citizens")

/ / query all data

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))

}

}

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report