#存入数据
#基于document的数据
*定义一个文档对象@
---
> doc = { author: 'joe', created : new Date('03/28/2009'), title : 'Yet another blog post', text : 'Here is the text...', tags : [ 'example', 'joe' ], comments : [ { author: 'jim', comment: 'I disagree' }, { author: 'nancy', comment: 'Good post' } ] }
*保存该文档@
> db.posts.insert(doc);
*检索该文档@
> db.posts.find({"comments.author":"jim"}); { "_id" : ObjectId("4ee9550cbd3e00000000337e"), "author" : "joe", "created" : "Sat Mar 28 2009 00:00:00 GMT+0800", "title" : "Yet another blog post", "text" : "Here is the text...", "tags" : [ "example", "joe" ], "comments" : [ { "author" : "jim", "comment" : "I disagree" }, { "author" : "nancy", "comment" : "Good post" } ] }
ps:在使用java driver 的时候,可以通过WriteConcern来返回异常信息,如@
db.test.insert(obj, WriteConcern.SAFE);
#mongodb优化
*使用索引@
db.things.ensureIndex({i:1});
*限制返回数据条数@
db.things.find().sort({i:1}).limit(10);
*只返回需要的列@
db.things.find({},{i2:true}).sort({i:-1}).limit(10);
返回i2域
db.things.find({},{i2:false}).sort({i:-1}).limit(10);
返回除i2的域,即i,i3
db.things.find({},{i2:false,_id:0}).sort({i:-1}).limit(10);
返回除i2、_id的剩余域
*还可以这么写(照搬官网示例)@
db.posts.find({}, {comments:{$slice: 5}}) // first 5 comments db.posts.find({}, {comments:{$slice: -5}}) // last 5 comments db.posts.find({}, {comments:{$slice: [20, 10]}}) // skip 20, limit 10 db.posts.find({}, {comments:{$slice: [-20, 10]}}) // 20 from end, limit 10
*使用mongodb profiler(剖面测量仪,性能监视)@
> db.commandHelp("profile"); help for: profile enable or disable performance profiling { profile : <n> } 0=off 1=log slow ops 2=log all http://www.mongodb.org/display/DOCS/Database+Profiler > db.setProfilingLevel(2); { "was" : 0, "slowms" : 100, "ok" : 1 }
首选查一下帮助信息,然后将profillingLevel设置为2,was表示以前的设置。查看当前设置@
> db.getProfilingLevel(); 2
slowms值>100ms即被认为慢,可以通过如下命令修改默认值@
> db.setProfilingLevel(2,20) { "was" : 2, "slowms" : 20, "ok" : 1 }
*明确的指明需要使用的索引@
> db.things.find({i:4}).hint({i:1}); { "_id" : ObjectId("4ee8510a0c16000000006ec9"), "i" : 4, "i2" : 16 } { "_id" : ObjectId("4ee8514d0c16000000006edd"), "i" : 4, "i2" : 16, "i3" : 64 }
PS:贴一段使用索引的性能测试代码:(测试环境:window7 64 4G RAM)
import com.mongodb.BasicDBObject; import com.mongodb.DB; import com.mongodb.DBCollection; import com.mongodb.DBCursor; import com.mongodb.DBObject; import com.mongodb.Mongo; import com.mongodb.MongoException; public class PerformanceTest { private Mongo mongo = null; public PerformanceTest() { } private Mongo getInstance() { if (mongo == null) try { mongo = new Mongo(); } catch (UnknownHostException e) { e.printStackTrace(); } catch (MongoException e) { e.printStackTrace(); } return mongo; } /** * 插入简单数据 */ public static void Performance() { PerformanceTest pt = new PerformanceTest(); Mongo mongo = pt.getInstance(); DB db = mongo.getDB("mytest"); /*// make a document and insert it BasicDBObject doc = new BasicDBObject(); doc.put("name", "MongoDB"); doc.put("type", "database"); doc.put("count", 1); BasicDBObject info = new BasicDBObject(); info.put("x", 203); info.put("y", 102); doc.put("info", info); Set<String> colls = db.getCollectionNames(); for (String s : colls) { System.out.println(s); }*/ DBCollection coll = db.getCollection("testCollection"); int i = 0; DBObject myDoc = coll.findOne(); // System.out.println(myDoc); for (i = 0; i < 6000000; i++) { coll.insert(new BasicDBObject().append("i", i).append("j", i).append("k", i)); } /*DBCursor cur = coll.find(); while (cur.hasNext()) { System.out.println(cur.next()); } coll.drop();*/ System.out.println(coll.count()); } public static void creatIndex(){ PerformanceTest pt = new PerformanceTest(); Mongo mongo = pt.getInstance(); DB db = mongo.getDB("mytest"); DBCollection coll = db.getCollection("testCollection"); coll.createIndex(new BasicDBObject("i", 1)); } public static void query(){ PerformanceTest pt = new PerformanceTest(); Mongo mongo = pt.getInstance(); DB db = mongo.getDB("mytest"); BasicDBObject query = new BasicDBObject(); query.put("i", new BasicDBObject("$gt", 1234).append("$lte", 1240)); // i.e. 20 < i <= 30 DBCollection coll = db.getCollection("testCollection"); DBCursor cur = coll.find(query); while(cur.hasNext()) { System.out.println(cur.next()); } } public static void main(String[] args) { Long cost = System.currentTimeMillis(); PerformanceTest pt = new PerformanceTest(); Mongo mongo = pt.getInstance(); DB db = mongo.getDB("mytest"); DBCollection coll = db.getCollection("testCollection"); System.out.println("count:"+coll.count()); System.out.println(Integer.MAX_VALUE); System.out.println(Long.MAX_VALUE); //创建索引后,效率有大幅度的提升,6000000条数据(仅有一列)没有索引时查询耗时2865毫秒, //创建索引后,查询耗时111毫秒。 // creatIndex(); query(); // Performance(); cost = System.currentTimeMillis() - cost; System.out.println("cost:"+cost); /* coll.dropIndexes(); coll.drop(); db.dropDatabase(); */ } }