// 获取数据库连接 // val url = MongoClientURI("mongodb://192.168.56.160:27017/") // val mc = MongoClient(url) val mc = MongoClient("192.168.56.160", 27017) val db = mc("mydemo") // 加载数据库 val tab = db("userinfos") // todo 查 val where = MongoDBObject("name" -> "zhangsan") tab.find(where).foreach(println) // todo 查询姓张 tab.filter(_.get("name").toString.startsWith("zhang")).foreach(println) // todo 查所有 val rdd = tab.map(x=>{ (x.get("id"),x.get("name"),x.get("age")) }) // todo 增加 val row = MongoDBObject("id" -> "5", "name" -> "zhaoyun", "age" -> 1300) tab.insert(row)