安装完成后在/bin文件夹下打开命令窗口

输入.\mongo启动数据库,若正常启动说明安装成功:

为了启动mongodb方便,将mongod.exe路径加入环境变量,电脑->属性->高级系统设置->环境变量,在path里加入路径,即可在命令行任何地方都能用到mongod命令。

开始

在自定义文件夹下新建data和log文件夹,若指定直接启动则会在安装目录的data和log文件夹中生成相关文件。

在log文件夹下新建mongodb.log文件,指定log文件

开启服务并指定端口和日志文件目录

mongod --dbpath F:\test\node\mongo\data --port 27011 --logpath F:\test\node\mongo\log\mongodb.log

shell操作

开启指定客户端 :mongo 127.0.0.1:27011

显示当前所有数据库及大小:show dbs

使用某个数据库,若没有则直接创建:use tezt01

显示当前数据库的名字:db

当插入一条数据后一个集合才算真正的创建

向集合中插入数据:

> use school
switched to db school
> db.student.insert({"name":"adoctors"});
WriteResult({ "nInserted" : 1 })

列出当前使用的集合

> show collections
student

查找数据

//查找一个集合中的所有数据
> db.student.find()
{ "_id" : ObjectId("5b66cb0a8276da765b977346"), "name" : "adoctors" }
{ "_id" : ObjectId("5b66cc598276da765b977347"), "name" : "adoctor2s", "age" : 18 }
{ "_id" : ObjectId("5b66cc668276da765b977348"), "name" : "adoctor3s", "age" : 28 }
{ "_id" : ObjectId("5b66cc7d8276da765b977349"), "name" : "adoctor4s", "age" : 68 }
{ "_id" : ObjectId("5b66cc8b8276da765b97734a"), "name" : "adoctor5s", "age" : 26 } //按条件查找
> db.student.find({"name":"adoctor3s"})
{ "_id" : ObjectId("5b66cc668276da765b977348"), "name" : "adoctor3s", "age" : 28 } //查找年龄大于20的数据
> db.student.find({"age":{$gt:20}})
{ "_id" : ObjectId("5b66cc668276da765b977348"), "name" : "adoctor3s", "age" : 28 }
{ "_id" : ObjectId("5b66cc7d8276da765b977349"), "name" : "adoctor4s", "age" : 68 }
{ "_id" : ObjectId("5b66cc8b8276da765b97734a"), "name" : "adoctor5s", "age" : 26 } //或
> db.student.find({$or:[{"name":"adoctors"},{"age":26}]})
{ "_id" : ObjectId("5b66cb0a8276da765b977346"), "name" : "adoctors" }
{ "_id" : ObjectId("5b66cc8b8276da765b97734a"), "name" : "adoctor5s", "age" : 26 } //排序,-1降序;1升序
> db.student.find().sort({"age":-1})
{ "_id" : ObjectId("5b66cc7d8276da765b977349"), "name" : "adoctor4s", "age" : 68 }
{ "_id" : ObjectId("5b66cc668276da765b977348"), "name" : "adoctor3s", "age" : 28 }
{ "_id" : ObjectId("5b66cc8b8276da765b97734a"), "name" : "adoctor5s", "age" : 26 }
{ "_id" : ObjectId("5b66cc598276da765b977347"), "name" : "adoctor2s", "age" : 18 }
{ "_id" : ObjectId("5b66cb0a8276da765b977346"), "name" : "adoctors" }

修改

//update
> db.student.update({"name":"adoctors"},{$set:{"age":16}});
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.student.find()
{ "_id" : ObjectId("5b66cb0a8276da765b977346"), "name" : "adoctors", "age" : 16 }
{ "_id" : ObjectId("5b66cc598276da765b977347"), "name" : "adoctor2s", "age" : 18 }
{ "_id" : ObjectId("5b66cc668276da765b977348"), "name" : "adoctor3s", "age" : 28 }
{ "_id" : ObjectId("5b66cc7d8276da765b977349"), "name" : "adoctor4s", "age" : 68 }
{ "_id" : ObjectId("5b66cc8b8276da765b97734a"), "name" : "adoctor5s", "age" : 26 }

批量修改,加入{multi:true}

> db.student.update({}, {$set:{"like":"basketball"}},{multi:true})
WriteResult({ "nMatched" : 6, "nUpserted" : 0, "nModified" : 6 })
> db.student.find()
{ "_id" : ObjectId("5b66cb0a8276da765b977346"), "name" : "adoctors", "age" : 16, "like" : "basketball" }
{ "_id" : ObjectId("5b66cc598276da765b977347"), "name" : "adoctor2s", "age" : 18, "like" : "basketball" }
{ "_id" : ObjectId("5b66cc668276da765b977348"), "name" : "adoctor3s", "age" : 28, "like" : "basketball" }
{ "_id" : ObjectId("5b66cc7d8276da765b977349"), "name" : "adoctor4s", "age" : 68, "like" : "basketball" }
{ "_id" : ObjectId("5b66cc8b8276da765b97734a"), "name" : "adoctor5s", "age" : 26, "like" : "basketball" }
{ "_id" : ObjectId("5b66d0188276da765b97734b"), "name" : "adoctors", "like" : "basketball" }

替换

//只替换第一个符合条件的数据,且是整条数据都会被替换
> db.student.update({"name":"adoctors"},{"name":"abc"})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.student.find()
{ "_id" : ObjectId("5b66cb0a8276da765b977346"), "name" : "abc" }
{ "_id" : ObjectId("5b66cc598276da765b977347"), "name" : "adoctor2s", "age" : 18, "like" : "basketball" }
{ "_id" : ObjectId("5b66cc668276da765b977348"), "name" : "adoctor3s", "age" : 28, "like" : "basketball" }
{ "_id" : ObjectId("5b66cc7d8276da765b977349"), "name" : "adoctor4s", "age" : 68, "like" : "basketball" }
{ "_id" : ObjectId("5b66cc8b8276da765b97734a"), "name" : "adoctor5s", "age" : 26, "like" : "basketball" }
{ "_id" : ObjectId("5b66d0188276da765b97734b"), "name" : "adoctors", "like" : "basketball" }

删除

//默认删除所有符合条件的整条数据
db.student.remove({"name":"adoctors"}) //删除一个符合条件的整条数据
db.student.remove({"name":"adoctors"},{justOne:true})

limit&&skip

//只查找两条数据
db.student.find().limit(2) //跳过2条后查找2条数据
db.student.find().limit(2).skip(2)

整个文档集合的信息:db.student.stats()

总条数:db.student.count()

从数据库中删除集合:db.student.drop()

官方文档:http://www.mongoing.com/docs/mongo.html

最新文章

  1. bzoj 3517: 翻硬币
  2. Linux常用命令_(系统设置)
  3. 强大的日志分析工具 -- NSLogger
  4. 基于Golang的游戏服务器框架cellnet开发日记(二)
  5. xls和xlsx
  6. Android 关于网址,电话号码,邮箱的正则表达式-最权威
  7. PHP Socket编程 之使用fsockopen()函数
  8. Linux SSL 双向认证 浅解
  9. input _文本框回车或者失去光标触发事件
  10. CUDA零内存拷贝 疑问考证
  11. SQL-Teradata基础
  12. java 生成微信的二维码 工具类
  13. [Linux]Linux read/write
  14. java个人博客源码
  15. Facebook内部报告:争取青少年用户的鸡贼小技巧
  16. JS对表格排序(支持对序号,数字,字母,日期)
  17. Space Replacement
  18. 【Java】操作mysql数据库
  19. Spring Data Jpa 初探
  20. 51NOD 1013(3的幂的和)

热门文章

  1. Python学习笔记 - PostgreSQL的使用
  2. ASP.NET MVC5中View显示Html
  3. 如何安装搜索引擎Elasticsearch?
  4. Git学习笔记(一)Git初识及基本操作
  5. vue-cli脚手架build目录中的karma.conf.js配置文件
  6. java selenium webdriver第三讲 helloWord
  7. openGL一些概念02
  8. spring读取classpath目录下的配置文件通过表达式去注入属性值.txt
  9. Python之条件语句以及循环
  10. 0016_练习题d2