<dependency> <groupId>org.mongodb</groupId> <artifactId>mongodb-driver</artifactId> <version>3.2.2</version> </dependency>
package cn.netkiller.controller; import java.net.UnknownHostException; import com.mongodb.BasicDBObject; import com.mongodb.DB; import com.mongodb.DBCollection; import com.mongodb.MongoClient; public final class Tracker { public Tracker() { } public static void main(String[] args) { MongoClient mongo = null; try { mongo = new MongoClient("192.168.4.1", 27017); DB db = mongo.getDB("finance"); DBCollection table = db.getCollection("tracker"); BasicDBObject document = new BasicDBObject(); document.put("test", "helloworld"); table.insert(document); } catch (UnknownHostException e) { e.printStackTrace(); } } }
原文出处:Netkiller 系列 手札
本文作者:陈景峯
转载请与作者联系,同时请务必标明文章原始出处和作者信息及本声明。