日期:2014-05-16  浏览次数:20422 次

mongodb 搜索语句 与 SQL 语句对比
所有操作都是在mongo 客户端实施:

先向数据库中插入一些数据作为样本:
//文件集为test.test

>show dbs
local   (empty)
> use test
> db.test.insert({"TimeStamp":500001,"ProcessID":1001,"ThreadID":2001,"CPU":12})
> db.test.insert({"TimeStamp":500002,"ProcessID":1001,"ThreadID":2001,"CPU":13,"Descreption":"Hello World 2"})
> db.test.insert({"TimeStamp":500003,"ProcessID":1001,"ThreadID":2001,"CPU":13,"Descreption":"Hello World 3"})
> db.test.insert({"TimeStamp":500004,"ProcessID":1001,"ThreadID":2001,"CPU":13,"Descreption":"Hello World 4"})
> db.test.insert({"TimeStamp":500005,"ProcessID":1001,"ThreadID":2001,"CPU":13,"Descreption":"Hello World 5"})
> db.test.insert({"TimeStamp":500006,"ProcessID":1001,"ThreadID":2001,"CPU":13,"Descreption":"Hello World 6"})
>db.test.insert({"TimeStamp":500007,"ProcessID":1001,"ThreadID":2001,"CPU":13,"Descreption":"Hello World 7"})
> db.test.insert({"TimeStamp":500008,"ProcessID":1001,"ThreadID":2001,"CPU":13,"Descreption":"Hello World 8"})
> db.test.insert({"TimeStamp":500009,"ProcessID":1001,"ThreadID":2001,"CPU":13,"Descreption":"Hello World 9"})
> db.test.insert({"TimeStamp":500010,"ProcessID":1001,"ThreadID":2001,"CPU":13,"Descreption":"Hello World 10"})


//select TimeStamp from test where Descreption like '%1%'
按找指定字段按条件,并可指定排序 1:返回字段,0不返回字段

//列显示用1,不显示用0
> db.test.find({"Descreption":{"$regex":".*1.*"}},{"TimeStamp":1})
{ "_id" : ObjectId("4f66a9a2ad4aa301dccb47ab"), "TimeStamp" : 500001 }
{ "_id" : ObjectId("4f66aa11ad4aa301dccb47b4"), "TimeStamp" : 500010 }


> db.test.find({"Descreption":{"$regex":".*1.*"}},{"TimeStamp":0})
{ "_id" : ObjectId("4f66a9a2ad4aa301dccb47ab"), "ProcessID" : 1001, "ThreadID" : 2001, "CPU" : 12, "Descreption" : "Hell
o World 1" }
{ "_id" : ObjectId("4f66aa11ad4aa301dccb47b4"), "ProcessID" : 1001, "ThreadID" : 2001, "CPU" : 13, "Descreption" : "Hell
o World 10" }
>


更多内容请查看下面的链接:
http://www.mongodb.org/display/DOCS/Advanced+Queries
http://www.mongodb.org/display/DOCS/Querying#Querying-QueryExpressionObjects
http://www.cnblogs.com/jiangzhichao/archive/2011/10/24/2223186.html