安装过程不说了,参考:http://blog.csdn.net/rishengcsdn/article/details/7406149
以下程序试验用环境为Win7,64位+MongDB 2.6.0
数据插入程序:
import com.mongodb.Mongo;
import com.mongodb.MongoException;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import java.net.UnknownHostException;
import java.util.Set;
import java.util.Date;
public class DbTools {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Mongo mongoClient;
try {
mongoClient = new Mongo("127.0.0.1",27017);
DB db = mongoClient.getDB("mydb");
//boolean auth = db.authenticate(myUserName, myPassword); //可选的权限控制
Set<String> colls = db.getCollectionNames();
for (String s : colls) {
System.out.println(s); //取得所有文档打印
}
DBCollection coll = db.getCollection("testCollection"); //按名字取文档
BasicDBObject doc0 = new BasicDBObject();
doc0.put("name", "MongoDB");
doc0.put("type", "database");
doc0.put("count", 1);
BasicDBObject info = new BasicDBObject();
info.put("x", 203);
info.put("y", 102);
doc0.put("info", info);
coll.insert(doc0); //对指定文档插数据,
DBObject myDoc = coll.findOne(); //查找一个数据
System.out.println(myDoc);
for (long i = 1; i <= 30000; i++) {
BasicDBObject doc = new BasicDBObject();
doc.put("order_id", i);
doc.put("company_id", 505 + i);
doc.put("user_id", 180225429 + i);
doc.put("fetcher_id", 59803 + i);
doc.put("fetch_schedule_begin", new Date());
doc.put("fetch_schedule_end", new Date());
doc.put("sender_id", 59803 + i);
doc.put("mail_no", "000000");
doc.put("mail_type", "301");
doc.put("order_code", "LP10012700003959" + i);
doc.put("order_status", 30);
doc.put("prev_order_id", 0);
doc.put("trade_id", 2010012706189794L + i);
doc.put("goods_remark", "");
doc.put("receiver_name", " 凯撒");
doc.put("receiver_wangwang_id", "sanglin01");
doc.put("receiver_mobile_phone", "13021525841");
doc.put("receiver_zip_code", "650045");
doc.put("receiver_telephone", "13868117135");
doc.put("receiver_county_id", 350102);
doc.put("receiver_address", "福建省^^^福州市^^^鼓楼区^^^的萨芬萨芬萨芬的12号");
doc.put("gmt_create", new Date());
doc.put("gmt_modified", new Date());
doc.put("status_reason", "");
doc.put("logis_type", 0);
doc.put("seller_wangwang_id", "tbtest943" + i);
doc.put("seller_send_confirm", 0);
doc.put("shipping", 2);
doc.put("company_code", "");
doc.put("taobao_trade_id", "2232358300" + i);
doc.put("options", 2);
doc.put("shipping2", 0);
doc.put("order_source", 0);
doc.put("status_date", new Date());
doc.put("timeout_status", 2);
doc.put("feature", "ip=127.0.0.1;SFID=");
doc.put("service_fee", 0);
doc.put("seller_store_id", "23100");
doc.put("items_value", 23100);
doc.put("pre_status", 0);
doc.put("ticket_id", "");
doc.put("tfs_url", "T1DoBbXctCXXXXXXXX");
coll.insert(doc);
}
mongoClient.close();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MongoException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
以上程序将生成3万条数据,插入testCollection数据表中。
import com.mongodb.Mongo;
import com.mongodb.MongoException;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.mongodb.DBCursor;
import java.net.UnknownHostException;
import java.util.Set;
import java.util.regex.Pattern;
public class DbTool2 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Mongo mongoClient;
try {
mongoClient = new Mongo("127.0.0.1", 27017);
DB db = mongoClient.getDB("mydb");
// boolean auth = db.authenticate(myUserName, myPassword); //可选的权限控制
Set<String> colls = db.getCollectionNames();
for (String s : colls) {
System.out.println(s); // 取得所有文档打印
}
DBCollection coll = db.getCollection("testCollection"); // 按名字取文档
DBObject myDoc = coll.findOne(); // 查找一个数据
System.out.println(myDoc);
System.out.println(coll.getCount()); // 查找数据量
// 游标查询
DBCursor cursor = coll.find();
try {
while (cursor.hasNext()) {
System.out.println(cursor.next());
DBObject myDoca1 = cursor.curr();
Long temp = (Long) myDoca1.get("order_id");
if (temp == null) {
} else {
long i = temp;
if (i > 10)
break;
}
}
} finally {
cursor.close();
}
// 指定条件查询
BasicDBObject query = new BasicDBObject("order_id", 71);
cursor = coll.find(query);
try {
while (cursor.hasNext()) {
System.out.println("order_id=71: ");
System.out.println(cursor.next());
}
} finally {
cursor.close();
}
// 用正则表达式模糊查询
Pattern pattern = Pattern.compile("^.*" + 7199 + ".*$");
BasicDBObject query2 = new BasicDBObject("order_code", pattern);
cursor = coll.find(query2);
try {
while (cursor.hasNext()) {
System.out.println("order_code=*7199。: ");
System.out.println(cursor.next());
}
} finally {
cursor.close();
}
Set<String> colls2 = db.getCollectionNames(); // 取得表名
for (String s : colls2) {
System.out.println(s);
}
mongoClient.close();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MongoException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
以上程序将查询插入的数据,注意模糊查询只支持字符串类型。