help命令

通过此命令可以看到一些最基本的命令

  1. MongoDB shell version v3.4.1
  2. usage: mongo [options] [db address] [file names (ending in .js)]
  3. db address can be:
  4. foo foo database on local machine
  5. 192.168.0.5/foo foo database on 192.168.0.5 machine
  6. 192.168.0.5:9999/foo foo database on 192.168.0.5 machine on port 9999
  7. Options:
  8. --shell run the shell after executing files
  9. --nodb don't connect to mongod on startup - no
  10. 'db address' arg expected
  11. --norc will not run the ".mongorc.js" file on
  12. start up
  13. --quiet be less chatty
  14. --port arg port to connect to
  15. --host arg server to connect to
  16. --eval arg evaluate javascript
  17. -h [ --help ] show this usage information
  18. --version show version information
  19. --verbose increase verbosity
  20. --ipv6 enable IPv6 support (disabled by default)
  21. --disableJavaScriptJIT disable the Javascript Just In Time
  22. compiler
  23. --disableJavaScriptProtection allow automatic JavaScript function
  24. marshalling
  25. --ssl use SSL for all connections
  26. --sslCAFile arg Certificate Authority file for SSL
  27. --sslPEMKeyFile arg PEM certificate/key file for SSL
  28. --sslPEMKeyPassword arg password for key in PEM file for SSL
  29. --sslCRLFile arg Certificate Revocation List file for SSL
  30. --sslAllowInvalidHostnames allow connections to servers with
  31. non-matching hostnames
  32. --sslAllowInvalidCertificates allow connections to servers with invalid
  33. certificates
  34. --sslFIPSMode activate FIPS 140-2 mode at startup
  35. --networkMessageCompressors arg Comma-separated list of compressors to
  36. use for network messages
  37. --jsHeapLimitMB arg set the js scope's heap size limit
  38. Authentication Options:
  39. -u [ --username ] arg username for authentication
  40. -p [ --password ] arg password for authentication
  41. --authenticationDatabase arg user source (defaults to dbname)
  42. --authenticationMechanism arg authentication mechanism
  43. --gssapiServiceName arg (=mongodb) Service name to use when authenticating
  44. using GSSAPI/Kerberos
  45. --gssapiHostName arg Remote host name to use for purpose of
  46. GSSAPI/Kerberos authentication
  47. file names: a list of files to run. files have to end in .js and will exit after unless --shell is specified

use命令

例如命令【use demodb】,创建demodb,不用担心demodb不会创建,当使用use demodb 命令创建第一个collection时会自动创建数据库demodb
插入数据
使用命令【db.collectionName.insert({name:”jack”,age:33})】collectionName中插入一个document,如果collectionName不存在则创建。 使用命令【db.getCollectionNames()】会得到collectionName和system.indexex。system.indexex对于每个database都有,用于记录index。 使用命令【db.collectionName.find()】会查看到collectionName中的所有document。 命令如下:
[root@VM_53_120_centos ~]# mongo

  1. MongoDB shell version v3.4.1
  2. connecting to: mongodb://127.0.0.1:27017
  3. MongoDB server version: 3.4.1
  4. > use demodb
  5. switched to db demodb
  6. > db.FirstCollection.insert({name:"jack",age:22})
  7. WriteResult({ "nInserted" : 1 })
  8. > show collections
  9. FirstCollection
  10. system.indexes
  11. > db.getCollectionNames()
  12. [ "FirstCollection", "system.indexes" ]
  13. > db.demodb.find()
  14. > db.FirstCollection.find()
  15. { "_id" : ObjectId("543731431dc491f307663a0d"), "name" : "jack", "age" : 22 }
  16. > db.system.indexes.find()
  17. { "v" : 1, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "demodb.FirstCollection" }

查询数据

参考网址:http://www.cnblogs.com/stephen-liu74/archive/2012/08/03/2553803.html

MongoDB使用find来进行查询.查询就是返回一个集合中文档的子集,子集合的范围从0个文档到整个集合.
find的第一个参数决定了要返回哪些文档(document的过滤条件).其形式也是一个文档,说明要查询的细节.空的查询文档{}会匹配集合的全部内容.
要是不指定查询文档,默认是{}.如:db.users.find()返回集合中的所有内容.
向查询文档中添加键值对,就意味着添加了查询条件.对绝大多数类型来说,整数匹配整 数,布尔类型匹配布尔类型,字符串匹配字符串.

先添加测试数据

  1. >db.Student.insert({name:"jack",sex:1,age:33})
  2. >db.Student.insert({name:"jack",sex:1,age:33})
  3. >db.Student.insert({name:"lily",sex:0,age:13})
  4. >db.Student.insert({name:"kaily",sex:0,age:33})
  5. >db.Student.insert({name:"tom",sex:1,age:53})

1、find()/findOne()条件过滤

只获取name等于jack的Student。findOne()则只获取第一条
复制代码

  1. >use demodb
  2. >switched to db demodb
  3. >db.Student.find({name:"jack"})
  4. { "_id" : ObjectId("5437383157abafe09d99cbfc"), "name" : "jack", "sex" : 1, "age
  5. " : 33 }
  6. { "_id" : ObjectId("543738b857abafe09d99cbfd"), "name" : "jack", "sex" : 1, "age
  7. " : 33 }
  8. >db.Student.findOne({name:"jack"})
  9. {
  10. "_id" : ObjectId("5437383157abafe09d99cbfc"),
  11. "name" : "jack",
  12. "sex" : 1,
  13. "age" : 33
  14. }

2、find()/findOne()指定返回的fileds

说明:find()的第二个参数限制返回的filed的个数,0代表不返回,1代表返回。”_id”键总是会被返回。
如果不带条件,只限制返回的filed个数的话,命令如下:db.Student.find({},{sex:0})。只需要第一个参数为{}空字典就可以。

只获取name等于jack的Student,并且filed为name,age的数据。
复制代码

  1. > db.Student.find({name:"jack"},{name:1,age:1})
  2. { "_id" : ObjectId("5437383157abafe09d99cbfc"), "name" : "jack", "age" : 33 }
  3. { "_id" : ObjectId("543738b857abafe09d99cbfd"), "name" : "jack", "age" : 33 }
  4. > db.Student.find({name:"jack"},{sex:0})
  5. { "_id" : ObjectId("5437383157abafe09d99cbfc"), "name" : "jack", "age" : 33 }
  6. { "_id" : ObjectId("543738b857abafe09d99cbfd"), "name" : "jack", "age" : 33 }

3、查询条件

“$lt”,”$lte”,”$gt”,”$gte”分别对应<,<=,>,>=
如下代码:

  1. >db.Student.find({age:{$gt:33}}) 查询age大于33
  2. >db.Student.find({age:{$gte:33}})
  3. >db.Student.find({age:{$lt:33}})
  4. >db.Student.find({age:{$lte:33}})
  5. >db.Student.find({age:{$gt:23,$lt:43}})

复制代码

  1. > db.Student.find({age:{$gt:33}})
  2. { "_id" : ObjectId("543738c357abafe09d99cc00"), "name" : "tom", "sex" : 1, "age" : 53 }
  3. > db.Student.find({age:{$gte:33}})
  4. { "_id" : ObjectId("5437383157abafe09d99cbfc"), "name" : "jack", "sex" : 1, "age" : 33 }
  5. { "_id" : ObjectId("543738b857abafe09d99cbfd"), "name" : "jack", "sex" : 1, "age" : 33 }
  6. { "_id" : ObjectId("543738b857abafe09d99cbff"), "name" : "kaily", "sex" : 0, "age" : 33 }
  7. { "_id" : ObjectId("543738c357abafe09d99cc00"), "name" : "tom", "sex" : 1, "age" : 53 }
  8. > db.Student.find({age:{$lt:33}})
  9. { "_id" : ObjectId("543738b857abafe09d99cbfe"), "name" : "lily", "sex" : 0, "age" : 13 }
  10. > db.Student.find({age:{$lte:33}})
  11. { "_id" : ObjectId("5437383157abafe09d99cbfc"), "name" : "jack", "sex" : 1, "age" : 33 }
  12. { "_id" : ObjectId("543738b857abafe09d99cbfd"), "name" : "jack", "sex" : 1, "age" : 33 }
  13. { "_id" : ObjectId("543738b857abafe09d99cbfe"), "name" : "lily", "sex" : 0, "age" : 13 }
  14. { "_id" : ObjectId("543738b857abafe09d99cbff"), "name" : "kaily", "sex" : 0, "age" : 33 }
  15. > db.Student.find({age:{$gt:23,$lt:43}})
  16. { "_id" : ObjectId("5437383157abafe09d99cbfc"), "name" : "jack", "sex" : 1, "age" : 33 }
  17. { "_id" : ObjectId("543738b857abafe09d99cbfd"), "name" : "jack", "sex" : 1, "age" : 33 }
  18. { "_id" : ObjectId("543738b857abafe09d99cbff"), "name" : "kaily", "sex" : 0, "age" : 33 }

$ne 代表不等于

  1. db.Student.find({age:{$ne:33}}) 查询age不等于33

$in,$not和$or

  1. db.Student.find({age:{$in:[13,53]}})
  2. db.Student.find(
  3. {
  4. $or:
  5. [
  6. {age:{$in:[13,53]}},
  7. {name:"kaily"}
  8. ]
  9. }
  10. )

4、特殊查询—null和exists

null可以匹配自身,而且可以匹配”不存在的”

—插入测试数据

  1. db.Student.insert({name:null,sex:1,age:18})
  2. db.Student.insert({sex:1,age:24})
  3. db.Student.find({name:null}) --上面两条都能查到
  4. db.Student.find({name:{$in:[null],$exists:true}}) ---只能查到第一条

5、数组数据查询

  1. db.Student.insert({name:"wjh",sex:1,age:18,color:["red","blue","black"]})
  2. db.Student.insert({name:"lpj",sex:1,age:22,color:["white","blue","black"]})
  3. db.Student.find()

—color数组中所有包含white的文档都会被检索出来
db.Student.find({color:”white”})
—color数组中所有包含red和blue的文档都会被检索出来,数组中必须同时包含red和blue,但是他们的顺序无关紧要。
db.Student.find({color:{$all:[“red”,”blue”]}})
—精确匹配,即被检索出来的文档,color值中的数组数据必须和查询条件完全匹配,即不能多,也不能少,顺序也必须保持一致。
db.Student.find({color:[“red”,”blue”,”black”]})
—匹配数组中指定下标元素的值。数组的起始下标是0。注意color要加引号。
db.Student.find({“color.0”:”white”})

6、内嵌文档查询

——待完成———-

7、排序

  1. db.Student.find().sort({age:1})
  2. db.Student.find().sort({age:1,sex:1})

1代表升序,-1代表降序

8、分页

  1. db.Student.find().sort({age:1}).limit(3).skip(3)

limit代表取多少个document,skip代表跳过前多少个document。

9、获取数量

  1. db.Student.count({name:null})

—或者

  1. db.Student.find({name:null}).count()

删除数据

说明:删除数据比较简单。

  1. db.Student.remove({name:null})

更新数据

1.更新数据

命令【db.Student.update({name:”jack”},{age:55})】执行后
先查询name=jack的所有document,然后将name=jack的所有document都替换为{age:55},其它filed都没有了。
正确的更新方式应该为:

  1. db.Student.update({name:"jack"},{$set:{age:55}})

2.增加field

—将name=lily的student增加一个filed height

  1. db.Student.update({name:"lily"},{$inc:{height:175}})

3.upset-将数字field增加多少增量

—若存在则添加,否则更新,只能用于数字field,例如age更新前是50,更新了185,则变为235.

  1. db.Student.update({name:"lily"},{$inc:{age:185}},true)

4.批量更新数据

—————————待完成————-

分类: web

标签:   mongoDB