|
actor" } { "_id" : ObjectId("4f2f473cc9031ac66270a281"), "age" : 33, "dob" : "09/12/1934", "first" : "judi", "gender" : "f", "hair_colour" : "white", "last" : "dench", "nationality" : "english", "occupation" : "actress" }
如果需要查询年龄小于等于40的呢?运行如下: db.gbin1.find( { "age" : { "$lte" : 40 } } );
输出: { "_id" : ObjectId("4f2f473bc9031ac66270a27b"), "age" : 18, "dob" : "21/04/1978", "first" : "matthew", "gender" : "m", "hair_colour" : "brown", "last" : "setter", "nationality" : "australian", "occupation" : "developer" } { "_id" : ObjectId("4f2f473bc9031ac66270a27f"), "age" : 22, "dob" : "22/11/1958", "first" : "jamie lee", "gender" : "f", "hair_colour" : "brown", "last" : "curtis", "nationality" : "american", "occupation" : "actor" } { "_id" : ObjectId("4f2f473cc9031ac66270a281"), "age" : 33, "dob" : "09/12/1934", "first" : "judi", "gender" : "f", "hair_colour" : "white", "last" : "dench", "nationality" : "english", "occupation" : "actress" }
$gt/$gte
现在查询演员年龄大于47的,运行如下查询: db.gbin1.find( { "age" : { "$gt" : 47 } } );
输出: { "_id" : ObjectId("4ef224bf0fec2806da6e9b29"), "age" : 65, "dob" : "03/06/1925", "first" : "arnold", "gender" : "m", "hair_colour" : "brown", "last" : "schwarzenegger", "nationality" : "american", "occupation" : "actor" }
如果包括47的呢? db.gbin1.find( { "age" : { "$gte" : 47 } } );
输出: { "_id" : ObjectId("4ef224bf0fec2806da6e9b29"), "age" : 65, "dob" : "03/06/1925", "first" : "arnold", "gender" : "m", "hair_colour" : "brown", "last" : "schwarzenegger", "nationality" : "american", "occupation" : "actor" }
$in/$nin
如果查询指定规则的数据呢?这里我们看看那些人是演员或者开发者,为了更简单的查看,我们只返回姓和名: db.gbin1.find( { 'occupation' : { '$in' : [ "actor", "developer" ] } }, { "first" : 1, "last" : 1 } );
输出: { "_id" : ObjectId("4f2f473bc9031ac66270a27b"), "first" : "matthew", "last" : "setter" } { "_id" : ObjectId("4f2f473bc9031ac66270a27c"), "first" : "james", "last" : "caan" } { "_id" : ObjectId("4f2f473bc9031ac66270a27d"), "first" : "arnold", "last" : "schwarzenegger" } { "_id" : ObjectId("4f2f473bc9031ac66270a27e"), "first" : "tony", "last" : "curtis" } { "_id" : ObjectId("4f2f473bc9031ac66270a27f"), "first" : "jamie lee", "last" : "curtis" } { "_id" : ObjectId("4f2f473bc9031ac66270a280"), "first" : "michael", "last" : "caine" }
可以看到我们可以使用$nin得到相反的结果: db.gbin1.find( { 'occupation' : { '$nin' : [ "actor", "developer" ] } }, { "first" : 1, "last" : 1 } );
输出: { "_id" : ObjectId("4f2f473cc9031ac66270a281"), "first" : "judi", "last" : "dench" }
接下来我们组合一些运算符,比如,我们想查询所有是男性或者是开发人员,并且年纪小于40的人。如下: db.gbin1.find( { $or : [ { "gender" : "m", "occupation" : "developer" } ], "age" : { "$gt" : 40 } }, { "first" : 1, "last" : 1, "occupation" : 1, "dob" : 1 } );
输出: { "_id" : ObjectId("4f2f473bc9031ac66270a27e"), "dob" : "21/04/1978", "first" : "tony", "last" : "curtis", "occupation" : "developer" }
正则表单式
不清楚是否大家满足于此,我承诺帮助大家查询更加复杂高级的组合,这里我们使用正则表达式。这里我们查询所有名里以“ma"或者”to”开始并且姓里以“se"或者"de"开始的人。如下: db.gbin1.find( { "first" : /(ma|to)*/i, "last" : /(se|de)/i } );
输出: { "_id" : ObjectId("4f2f473bc9031ac66270a27b"), "age" : 18, "dob" : "21/04/1978", "first" : "matthew", "gender" : "m", "hair_colour" : "brown", "last" : "setter", "nationality" : "australian", "occupation" : "developer" } { "_id" : ObjectId("4f2f473cc9031ac66270a281"), "age" : 33, "dob" : "09/12/1934", "first" : "judi", "gender" : "f", "hair_colour" : "white", "last" : "dench", "nationality" : "english", "occupation" : "actress" }
以上查询中,我们在名中使用了一个正则表单如下: "first" : /(ma|to)*/i
//i意味着我们使 |