mongoDB 插入数据 用java实现

import java.net.UnknownHostException;

import com.mongodb.BasicDBObject;

import com.mongodb.DB;

import com.mongodb.DBCollection;

import com.mongodb.DBObject;

import com.mongodb.Mongo;

/**  *

用java 往mongoDB插入数据  * @author wwd

 *  */

public class InsertData {

public static void main(String[]args) throws UnknownHostException{

 Mongo mongo=new Mongo("127.0.0.1",27017);//链接mongoDB  

DB db=mongo.getDB("test");//获取test数据库  

DBCollection collection=db.getCollection("user");//获取user集合

 //向user集合插入一条数据

 DBObject object=new BasicDBObject();

 object.put("name", "李四");  

object.put("age",23);

 collection.insert(object);

}

}

你可能感兴趣的:(mongodb)