mongodb中的java编程
实现 连接本地mongodb,数据的插入,数据的查询
import java.net.UnknownHostException;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.Mongo;
import com.mongodb.MongoException;
public class App {
public static void main(String[] args) {
try {
//实例化Mongo对象,连接27017端口
Mongo mongo = new Mongo("localhost", 27017);
DB db = mongo.getDB("yourdb");
DBCollection collection = db.getCollection("yourCollection");
BasicDBObject document = new BasicDBObject();
document.put("id", 1001);
document.put("msg", "some msg in mongo");
document.put("age", 16);
collection.insert(document);
BasicDBObject searchQuery = new BasicDBObject();
DBCursor cursor = collection.find(searchQuery);
while (cursor.hasNext()) {
System.out.println(cursor.next());
}
System.out.println("Done");
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (MongoException e) {
e.printStackTrace();
}
}
}