首先是简单查询find findone(默认返回20行)
> db.events.findOne(); { "_id" : ObjectId("5465ba77414a6548e7c705cd"), "type" : "IPA", "OCCURTIME" : ISODate("2014-08-21T05:59:13Z"), "DESC" : "全麻", "SEQNO" : "1", "INPATNO" : "1430701:4195298300" } > db.events.find(); { "_id" : ObjectId("5465ba77414a6548e7c705cd"), "type" : "IPA", "OCCURTIME" : IS ODate("2014-08-21T05:59:13Z"), "DESC" : "全麻", "SEQNO" : "1", "INPATNO" : "1430 701:4195298300" } { "_id" : ObjectId("5465ba77414a6548e7c705c8"), "type" : "IPI", "OCCURTIME" : IS ODate("2014-08-21T06:58:50Z"), "DESC" : "慢性肾病", "INPATNO" : "-7665:419529830 0" } { "_id" : ObjectId("5465ba77414a6548e7c705ce"), "type" : "REG", "OCCURTIME" : IS ODate("2014-08-31T01:18:50Z"), "DESC" : "急诊(内科)", "OUTPATNO" : "201408310158 5:4195298300" } { "_id" : ObjectId("5465ba77414a6548e7c705c9"), "type" : "OTDPS", "OCCURTIME" : ISODate("2014-09-10T03:45:39Z"), "DESC" : "颜华东", "OUTPATNO" : "2013080800122: 4195298312", "SEQNO" : "1" } { "_id" : ObjectId("5465ba77414a6548e7c705ca"), "type" : "IPO", "OCCURTIME" : IS ODate("2014-08-21T07:13:46Z"), "DESC" : "超敏C反应蛋白测定", "SEQNO" : "1", "INP ATNO" : "-76654195298300", "GROUP" : "3" } { "_id" : ObjectId("5465ba77414a6548e7c705cb"), "type" : "EXO", "OCCURTIME" : IS ODate("2012-03-07T00:20:47Z"), "DESC" : "腹部超声", "OUTPATNO" : "2012030700144: 4195298312", "SEQNO" : "1", "REQUESTNO" : "2012030700124:4195298312" } { "_id" : ObjectId("5465ba77414a6548e7c705cc"), "type" : "IPOP", "OCCURTIME" : I SODate("2014-08-22T14:00:00Z"), "DESC" : "股动脉切开取栓术; ", "SEQNO" : "1", "I NPATNO" : "1435583:4195298300" } { "_id" : ObjectId("5465ba77414a6548e7c705d9"), "type" : "IPA", "OCCURTIME" : IS ODate("2014-08-25T06:05:14Z"), "DESC" : "全麻", "SEQNO" : "2", "INPATNO" : "1435 583:4195298300" } { "_id" : ObjectId("5465ba77414a6548e7c705db"), "type" : "IPI", "OCCURTIME" : IS ODate("2014-08-21T01:02:13Z"), "DESC" : "精索静脉曲张", "INPATNO" : "-7666:41952 98300" } { "_id" : ObjectId("5465ba77414a6548e7c705dd"), "type" : "OTDPS", "OCCURTIME" : ISODate("2014-09-01T03:26:10Z"), "DESC" : "邵宏祥", "OUTPATNO" : "2014082301074: 4195298300", "SEQNO" : "2" } { "_id" : ObjectId("5465ba77414a6548e7c705de"), "type" : "EXO", "OCCURTIME" : IS ODate("2012-03-07T00:20:47Z"), "DESC" : "腹部超声", "OUTPATNO" : "2012030700144: 4195298312", "SEQNO" : "1", "REQUESTNO" : "2012030700124:4195298312" } { "_id" : ObjectId("5465ba77414a6548e7c705df"), "type" : "IPOP", "OCCURTIME" : I SODate("2014-08-25T04:00:00Z"), "DESC" : "右下肢膝上截肢术; ", "SEQNO" : "2", "I NPATNO" : "1435583:4195298300" } { "_id" : ObjectId("5465ba77414a6548e7c705e0"), "type" : "IPO", "OCCURTIME" : IS ODate("2014-08-21T07:13:46Z"), "DESC" : "血清唾液酸测定", "SEQNO" : "2", "INPATN O" : "-76654195298300", "GROUP" : "3" } { "_id" : ObjectId("5465ba77414a6548e7c705e5"), "type" : "IPI", "OCCURTIME" : IS ODate("2014-08-22T00:06:13Z"), "DESC" : "直肠恶性淋巴瘤", "INPATNO" : "-7667:419 5298300" } { "_id" : ObjectId("5465ba77414a6548e7c705e6"), "type" : "IPA", "OCCURTIME" : IS ODate("2014-09-02T14:34:09Z"), "DESC" : "全麻", "SEQNO" : "3", "INPATNO" : "1437 885:4195298300" } { "_id" : ObjectId("5465ba77414a6548e7c705ee"), "type" : "IPOP", "OCCURTIME" : I SODate("2014-09-02T11:10:00Z"), "DESC" : "左腋动脉探查,血肿清除; ", "SEQNO" : " 3", "INPATNO" : "1437885:4195298300" } { "_id" : ObjectId("5465ba77414a6548e7c705ef"), "type" : "EXO", "OCCURTIME" : IS ODate("2012-03-07T00:20:47Z"), "DESC" : "腹部超声", "OUTPATNO" : "2012030700144: 4195298312", "SEQNO" : "1", "REQUESTNO" : "2012030700124:4195298312" } { "_id" : ObjectId("5465ba77414a6548e7c705f1"), "type" : "IPO", "OCCURTIME" : IS ODate("2014-08-21T07:13:46Z"), "DESC" : "血清低密度脂蛋白胆固醇测定", "SEQNO" : "3", "INPATNO" : "-76654195298300", "GROUP" : "3" } { "_id" : ObjectId("5465ba77414a6548e7c705fc"), "type" : "IPI", "OCCURTIME" : IS ODate("2014-08-22T00:02:14Z"), "DESC" : "埋伏多生牙", "INPATNO" : "-7668:4195298 300" } { "_id" : ObjectId("5465ba77414a6548e7c705fe"), "type" : "IPA", "OCCURTIME" : IS ODate("2014-08-21T05:59:13Z"), "DESC" : "全麻", "SEQNO" : "1", "INPATNO" : "1430 701:4195298300" } Type "it" for more >
指定条件And
> db.events.find({"type":"IPA","SEQNO":"1"}); { "_id" : ObjectId("5465ba77414a6548e7c705cd"), "type" : "IPA", "OCCURTIME" : IS ODate("2014-08-21T05:59:13Z"), "DESC" : "全麻", "SEQNO" : "1", "INPATNO" : "1430 701:4195298300" } { "_id" : ObjectId("5465ba77414a6548e7c705fe"), "type" : "IPA", "OCCURTIME" : IS ODate("2014-08-21T05:59:13Z"), "DESC" : "全麻", "SEQNO" : "1", "INPATNO" : "1430 701:4195298300" } { "_id" : ObjectId("5465ba78414a6548e7c70639"), "type" : "IPA", "OCCURTIME" : IS ODate("2014-09-25T05:53:37Z"), "DESC" : "全麻", "SEQNO" : "1", "INPATNO" : "1438 736:4195298300" } { "_id" : ObjectId("5465ba78414a6548e7c7065b"), "type" : "IPA", "OCCURTIME" : IS ODate("2014-08-27T02:11:00Z"), "DESC" : "椎管内麻醉", "SEQNO" : "1", "INPATNO" : "1439459:4195298300" }
返回指定的字段,比如像返回类型和描述,1表示要显示的字段,0表示不显示的字段
> db.events.find({},{"type":"IPA","SEQNO":"1"}); { "_id" : ObjectId("5465ba77414a6548e7c705cd"), "type" : "IPA", "SEQNO" : "1" } { "_id" : ObjectId("5465ba77414a6548e7c705c8"), "type" : "IPI" } { "_id" : ObjectId("5465ba77414a6548e7c705ce"), "type" : "REG" } { "_id" : ObjectId("5465ba77414a6548e7c705c9"), "type" : "OTDPS", "SEQNO" : "1" } { "_id" : ObjectId("5465ba77414a6548e7c705ca"), "type" : "IPO", "SEQNO" : "1" } { "_id" : ObjectId("5465ba77414a6548e7c705cb"), "type" : "EXO", "SEQNO" : "1" } { "_id" : ObjectId("5465ba77414a6548e7c705cc"), "type" : "IPOP", "SEQNO" : "1" } { "_id" : ObjectId("5465ba77414a6548e7c705d9"), "type" : "IPA", "SEQNO" : "2" } { "_id" : ObjectId("5465ba77414a6548e7c705db"), "type" : "IPI" } { "_id" : ObjectId("5465ba77414a6548e7c705dd"), "type" : "OTDPS", "SEQNO" : "2" } { "_id" : ObjectId("5465ba77414a6548e7c705de"), "type" : "EXO", "SEQNO" : "1" } { "_id" : ObjectId("5465ba77414a6548e7c705df"), "type" : "IPOP", "SEQNO" : "2" } { "_id" : ObjectId("5465ba77414a6548e7c705e0"), "type" : "IPO", "SEQNO" : "2" } { "_id" : ObjectId("5465ba77414a6548e7c705e5"), "type" : "IPI" } { "_id" : ObjectId("5465ba77414a6548e7c705e6"), "type" : "IPA", "SEQNO" : "3" } { "_id" : ObjectId("5465ba77414a6548e7c705ee"), "type" : "IPOP", "SEQNO" : "3" } { "_id" : ObjectId("5465ba77414a6548e7c705ef"), "type" : "EXO", "SEQNO" : "1" } { "_id" : ObjectId("5465ba77414a6548e7c705f1"), "type" : "IPO", "SEQNO" : "3" } { "_id" : ObjectId("5465ba77414a6548e7c705fc"), "type" : "IPI" } { "_id" : ObjectId("5465ba77414a6548e7c705fe"), "type" : "IPA", "SEQNO" : "1" } Type "it" for more
> db.events.find({},{"type":1,"_id":0}); { "type" : "IPA" } { "type" : "IPI" } { "type" : "REG" } { "type" : "OTDPS" } { "type" : "IPO" } { "type" : "EXO" } { "type" : "IPOP" } { "type" : "IPA" } { "type" : "IPI" } { "type" : "OTDPS" } { "type" : "EXO" } { "type" : "IPOP" } { "type" : "IPO" } { "type" : "IPI" } { "type" : "IPA" } { "type" : "IPOP" } { "type" : "EXO" } { "type" : "IPO" } { "type" : "IPI" } { "type" : "IPA" } Type "it" for more
查询条件还可以使用$lt $gt,$lte ,$gte比较操作符
比如
> db.events.find({"type":"IPA","SEQNO" :{"$gte": "2"}}); { "_id" : ObjectId("5465ba77414a6548e7c705d9"), "type" : "IPA", "OCCURTIME" : IS ODate("2014-08-25T06:05:14Z"), "DESC" : "全麻", "SEQNO" : "2", "INPATNO" : "1435 583:4195298300" } { "_id" : ObjectId("5465ba77414a6548e7c705e6"), "type" : "IPA", "OCCURTIME" : IS ODate("2014-09-02T14:34:09Z"), "DESC" : "全麻", "SEQNO" : "3", "INPATNO" : "1437 885:4195298300" } { "_id" : ObjectId("5465ba77414a6548e7c70611"), "type" : "IPA", "OCCURTIME" : IS ODate("2014-08-25T06:05:14Z"), "DESC" : "全麻", "SEQNO" : "2", "INPATNO" : "1435 583:4195298300" } { "_id" : ObjectId("5465ba78414a6548e7c70622"), "type" : "IPA", "OCCURTIME" : IS ODate("2014-09-02T14:34:09Z"), "DESC" : "全麻", "SEQNO" : "3", "INPATNO" : "1437 885:4195298300" } { "_id" : ObjectId("5465ba78414a6548e7c70647"), "type" : "IPA", "OCCURTIME" : IS
Or查询有两张方式实现,第一种使用$in
> db.events.find({"type":"IPA","SEQNO" :{"$in": ["1","2"]}}); { "_id" : ObjectId("5465ba77414a6548e7c705cd"), "type" : "IPA", "OCCURTIME" : IS ODate("2014-08-21T05:59:13Z"), "DESC" : "全麻", "SEQNO" : "1", "INPATNO" : "1430 701:4195298300" } { "_id" : ObjectId("5465ba77414a6548e7c705d9"), "type" : "IPA", "OCCURTIME" : IS ODate("2014-08-25T06:05:14Z"), "DESC" : "全麻", "SEQNO" : "2", "INPATNO" : "1435 583:4195298300" } { "_id" : ObjectId("5465ba77414a6548e7c705fe"), "type" : "IPA", "OCCURTIME" : IS ODate("2014-08-21T05:59:13Z"), "DESC" : "全麻", "SEQNO" : "1", "INPATNO" : "1430 701:4195298300" } { "_id" : ObjectId("5465ba77414a6548e7c70611"), "type" : "IPA", "OCCURTIME" : IS ODate("2014-08-25T06:05:14Z"), "DESC" : "全麻", "SEQNO" : "2", "INPATNO" : "1435 583:4195298300" } { "_id" : ObjectId("5465ba78414a6548e7c70639"), "type" : "IPA", "OCCURTIME" : IS ODate("2014-09-25T05:53:37Z"), "DESC" : "全麻", "SEQNO" : "1", "INPATNO" : "1438 736:4195298300" }
第二种为Or
关于limit skip sort
mongodb虽然为非关系型的,但是也不建议采用skip 和limit来实现分组,应为数据量的大时候也是跑不动的,建议在集合设计的时候加入ID或者时间列,在应用里记住上一次的查询ID来实现。
索引是关系型数据库不可或缺的东西,好处在于它可以提升查询速度,当然过多的索引对磁盘空间的占用也是很可怕的,在Mongo里面也一样,建议在需要排序的字段上建索引。