当前位置:数据库 > mongodb >>

Nosql数据库教程之初探MongoDB - 第二部分

大家都听说过nosql解决方案吧?现在是web开发中很流行的技术。 是不是也有听说过MongoDB? 你是不是准备好了来学习一下web开发中最酷的技术之一MongoDB?

上一篇文章我们介绍了MongoDB基础知识,包括安装,创建基本的Database,及其执行简单的查询操作,今天我们接着介绍一些高级特性,包括如何使用selecor与数据库互动。Selector赋予了我们更细颗粒的控制和查询的能力。

如果你想真正的使用MongoDB开发一个应用,你需要学习更多。

今天我们将扩展介绍俩个关键的MongoDB特性:

  • 高级查询
  • Mapreduce

高级查询

上一篇文章我们介绍了基本的查询并且介绍了Selector(选择器)。现在我们需要学习更复杂的查询,基于上一次的介绍我们介绍俩个关键的使用:

  • 条件运算符
  • 正则表达式

条件运算符

正如它的名字,条件运算符用来收集查询并且匹配查询内容。这里有很多,今天我们主要介绍9个关键的运算符,如下:

  • $lt - 必须小于
  • $gt - 必须大于
  • $lte - 必须小于或者等于
  • $gte - 必须大于或者等于
  • $in - 必须存在于一组条件中
  • $nin - 必须不存在于一组条件中
  • $not - 必须不等于一个条件

如果你使用过类似mysql的数据库,你基本也了解以上关键字作用。为了更好的演示,我们这里需要修改一些数据库内容。我们将添加一个age的属性。运行如下代码:

db.gbin1.update({"_id" : ObjectId("4f2f473bc9031ac66270a27b")}, {"$set" : {"age" : 18 }}); db.gbin1.update({"_id" : ObjectId("4f2f473bc9031ac66270a27c")}, {"$set" : {"age" : 45 }}); db.gbin1.update({"_id" : ObjectId("4f2f473bc9031ac66270a27d")}, {"$set" : {"age" : 65 }}); db.gbin1.update({"_id" : ObjectId("4f2f473bc9031ac66270a27e")}, {"$set" : {"age" : 43 }}); db.gbin1.update({"_id" : ObjectId("4f2f473bc9031ac66270a27f")}, {"$set" : {"age" : 22 }}); db.gbin1.update({"_id" : ObjectId("4f2f473bc9031ac66270a280")}, {"$set" : {"age" : 45 }}); db.gbin1.update({"_id" : ObjectId("4f2f473cc9031ac66270a281")}, {"$set" : {"age" : 33 }});

注意:如果你的数据库中的_id和我这里的演示不一样,请针对你自己的_id值更新数据

如果你运行以上命令后,你查询内容将得到以下输出:

db.gbin1.find(); { "_id" : ObjectId("4f2f473bc9031ac66270a27b"), "age" : 18, "dob" : "21/04/1978", "first" : "matthew", "gender" : "m", "hair_colour" : "brown", "last" : "setter", "nationality" : "australian", "occupation" : "developer" } { "_id" : ObjectId("4f2f473bc9031ac66270a27c"), "age" : 45, "dob" : "26/03/1940", "first" : "james", "gender" : "m", "hair_colour" : "brown", "last" : "caan", "nationality" : "american", "occupation" : "actor" } { "_id" : ObjectId("4f2f473bc9031ac66270a27d"), "age" : 65, "dob" : "03/06/1925", "first" : "arnold", "gender" : "m", "hair_colour" : "brown", "last" : "schwarzenegger", "nationality" : "american", "occupation" : "actor" } { "_id" : ObjectId("4f2f473bc9031ac66270a27e"), "age" : 43, "dob" : "21/04/1978", "first" : "tony", "gender" : "m", "hair_colour" : "brown", "last" : "curtis", "nationality" : "american", "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("4f2f473bc9031ac66270a280"), "age" : 45, "dob" : "14/03/1933", "first" : "michael", "gender" : "m", "hair_colour" : "brown", "last" : "caine", "nationality" : "english", "occupation" : "actor" } { "_id" : ObjectId("4f2f473cc9031ac66270a281"), "age" : 33, "dob" : "09/12/1934", "first" : "judi", "gender" : "f", "hair_colour" : "white", "last" : "dench", "nationality" : "english", "occupation" : "actress" }

$lt/$lte

db.gbin1.find( { "age" : { "$lt" : 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" }

如果需要查询年龄小于等于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", &qu
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,