下载mongodb地址[url]http://www.mkyong.com/mongodb/how-to-install-mongodb-on-windows/[/url],mongodb数据库包括10个可执行exe文件,启动数据库并创建一个数据库名mongodb --dbpath "d:\mongodb" 即可。启动后可用通过http:localhost:[url]http://localhost:28017/[/url]可以访问mongodb运行情况
java连接mongodb,
如何让mongo作为系统服务运行,启动命令:
mongod -dbpath E:\mongodbpath -logpath E:\mongodbpath\log\log.txt -port 27017 -install
java连接mongodb,
public static void main(String[] args) {
try {
// connect to mongoDB, ip and port number
Mongo mongo = new Mongo("localhost", 27017);
// get database from MongoDB,
// if database doesn't exists, mongoDB will create it automatically
DB db = mongo.getDB("mongod");
// Get collection from MongoDB, database named "yourDB"
// if collection doesn't exists, mongoDB will create it
// automatically
DBCollection collection = db.getCollection("yourCollection");
// create a document to store key and value
BasicDBObject document = new BasicDBObject();
document.put("id", 1001);
document.put("msg", "hello world mongoDB in Java");
// save it into collection named "yourCollection"
collection.insert(document);
// search query
BasicDBObject searchQuery = new BasicDBObject();
searchQuery.put("id", 1001);
// query it
DBCursor cursor = collection.find(searchQuery);
// loop over the cursor and print it retrieved result
while (cursor.hasNext()) {
System.out.println(cursor.next());
}
System.out.println("Done");
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (MongoException e) {
e.printStackTrace();
}
}
如何让mongo作为系统服务运行,启动命令:
mongod -dbpath E:\mongodbpath -logpath E:\mongodbpath\log\log.txt -port 27017 -install