MongoDB 在windows shell环境下的基本操作和命令的使用示例(一)

来源:转载


1:MongoDB做为非关系型数据库的代表有着非常强大功能,由C++编写,主要特点。

1.1   容易部署,跨平台。

1.2 高性能,数据存储非常方便,支持各种数据的存储,包括图片等二进制格式的文件

1.3 易使用,支持完全索引,查询,支持云计算和其它主流编程语言

2,在win7 64位平台上展示mongoDB的各中命令(配置省略)

          mongodb version:V3.0.4

3,连接数据库 mongo

        终端显示: mongoDB shell version: 3.0.4
   connecting to:test

4, show dbs:查看所有数据库

5, db.stats();显示当前状态
6,      db.version():显示db版本
7, db.getMongo();查看当前链接机器地址
8, db.getName();得到当前使用的数据库
9, db.repairDatabase();修复当前数据库
                成功返回{“OK”:1}
10, use databasename;切换或是创建一个数据库databasename
11, db.dropDatabase();删除当前数据库
12,        db.collection.drop();删除数据库中的一个结合

13, 下面通过举例来了解mongo的基本操作

14,建立一个数据库testbase
     use testbase
15,在这个数据库的集合testcoll中插入一些数据,如下中所示


db.testcoll.insert({"num":"1","name":"李一","age":"18","sex":"male","job":"worker","salares":"300","score":"12"})
db.testcoll.insert({"num":"2","name":"没大","age":"33","sex":"female","job":"manager","salares":"8000","score":"100"})
db.testcoll.insert({"num":"3","name":"王五","age":"80","sex":"male","job":"worker","salares":"300000","score":"80"})
db.testcoll.insert({"num":"4","name":"赵六","age":"12","sex":"male","job":"engineer","salares":"100","score":"90"})
db.testcoll.insert({"num":"5","name":"左奔","age":"20","sex":"female","job":"worker","salares":"2000","score":"80"})
db.testcoll.insert({"num":"6","name":"及二","age":"28","sex":"male","job":"fda","salares":"1000","score":"100"})
db.testcoll.insert({"num":"7","name":"张三","age":"38","sex":"female","job":"dddd","salares":"30000000","score":"0"})
db.testcoll.insert({"num":"8","name":"李四","age":"48","sex":"male","job":"cccc","salares":"32100","score":"0"})
db.testcoll.insert({"num":"9","name":"木程","age":"98","sex":"male","job":"worker","salares":"21000","score":"60"})
db.testcoll.insert({"num":"10","name":"之力","age":"8","sex":"female","job":"manager","salares":"3200","score":"50"})
db.testcoll.insert({"num":"11","name":"工作","age":"23","sex":"male","job":"engineer","salares":"300","score":"40"})
db.testcoll.insert({"num":"12","name":"压力","age":"34","sex":"female","job":"wfa","salares":"3020","score":"30"})
db.testcoll.insert({"num":"13","name":"脸左","age":"45","sex":"male","job":"wfdafar","salares":"20000","score":"20"})
db.testcoll.insert({"num":"14","name":"顶级","age":"32","sex":"male","job":"wofdafda","salares":"10000","score":"20"})
db.testcoll.insert({"num":"15","name":"萨达","age":"9","sex":"male","job":"workdfaer","salares":"4000","score":"70"})
db.testcoll.insert({"num":"16","name":"努力","age":"23","sex":"female","job":"workdfaer","salares":"5000","score":"85"})
db.testcoll.insert({"num":"17","name":"大大","age":"18","sex":"male","job":"workdfaer","salares":"9000","score":"29"})
db.testcoll.insert({"num":"18","name":"怎么样","age":"18","sex":"male","job":"worker","salares":"12000","score":"98"})
db.testcoll.insert({"num":"19","name":"枯萎","age":"13","sex":"female","job":"wofarker","salares":"3000","score":"84"})
db.testcoll.insert({"num":"20","name":"取得","age":"14","sex":"male","job":"workefar","salares":"32000","score":"83"})
db.testcoll.insert({"num":"21","name":"灾区","age":"29","sex":"female","job":"workder","salares":"3121000","score":"88"})


16,执行命令 db.testcoll.find().pretty(),显示如下面所示。



17,我们就用上面所示的数据库来进行相应的操作,条件查询,查询年纪为18的文档即age:18
      db.testcoll.find({"age":"18"}).pretty()



18,查询一个:db.testcoll.findOne(),返回一个文档
19,查询中的关系运算和逻辑运算:等于({"key":"value"}或$eq),小于($lt),小于等于($lte),大于($gt),大于等于($gte),不等于($ne),与($and),或($or),非($not)
       下面举一个综合例子,关系与逻辑综合(注意一定要是JSON格式的)
        db.testcoll.find({"$or":[{"score":{"$gte":"80"}},{"sex":{"$ne":"male"}}]}).pretty().limit(3)




最后的显示结果如上面所示。











分享给朋友:
您可能感兴趣的文章:
随机阅读: