1代开一个cmd窗口,当作mongodb客户端,执行以下命令:
mongo localhost:27017
2查看所有的数据库:
> show dbs; admin (empty) local 0.078GB
|
3 db命令,查看当前窗口在那个数据库下面(test数据库没有任何数据,在内存里面):
4创建集合
A 显示创建
db.createCollection(“collectionName”); |
> db.createCollection("c1") { "ok" : 1 } >
|
B 隐式创建
> db.c2.insert({name:"zhangsan",age:31}) WriteResult({ "nInserted" : 1 }); 显示集合 show collections;
|
5 CRUD
A添加 |
db.collectionName.insert({}) |
> db.c2.insert({name:"zhangsan",age:31}) WriteResult({ "nInserted" : 1 }) > show collections; c1 c2 system.indexes > var i = 1+2; > i 3 > for(var i = 1 ; i <= 10000;i++){ ... db.c1.insert({name:'toto',age:24}) ... } WriteResult({ "nInserted" : 1 }) > 清屏命令:cls |
B删除 db.collectionName.remove(); |
> db.c4.insert({name:"zhangsan",age:20}); WriteResult({ "nInserted" : 1 }) > db.c4.insert({name:"lisi",age:24}); WriteResult({ "nInserted" : 1 })
db.c4.find();
> db.c4.remove({name:"zhangsan"}); WriteResult({ "nRemoved" : 1 }) > db.c4.find(); { "_id" : ObjectId("543e0b6ae2c90313035e7ce3"), "name" : "lisi", "age" : 24 } >
|
C查询 A db.collectionName.find({条件}); eg:db.c3.find();
|
再如: > db.c4.find({name:"lisi"}); { "_id" : ObjectId("543e0b6ae2c90313035e7ce3"), "name" : "lisi", "age" : 24 } >
|
B查找集合中的第一条数据 db.collectionName.findOne(); eg: > db.c4.find(); { "_id" : ObjectId("543e0b6ae2c90313035e7ce3"), "name" : "lisi", "age" : 24 } { "_id" : ObjectId("543e0d96e2c90313035e7ce4"), "name" : "wangwu", "age" : 20 } { "_id" : ObjectId("543e0da0e2c90313035e7ce5"), "name" : "zhaoliu", "age" : 25 } { "_id" : ObjectId("543e0da8e2c90313035e7ce6"), "name" : "tianqi", "age" : 25 } > db.c4.findOne(); { "_id" : ObjectId("543e0b6ae2c90313035e7ce3"), "name" : "lisi", "age" : 24 } >
注意:_id每个表都有,是mongodb自动维护的。 |
C查询集合中的文档,返回某些特定的键值 除了查询表达式以外,Mongodb还支持一些额外的参数选项,例如,我们可能仅仅只想返回某些特定的字段值: > db.c4.find({name:"wangwu"},{name:1}); { "_id" : ObjectId("543e0d96e2c90313035e7ce4"), "name" : "wangwu" } >
|
D查询显示问题 |
> for(var i = 1; i<=30;i++){ ... db.c4.insert({name:"tuzuoquan",age:i}); ... } WriteResult({ "nInserted" : 1 }) > 查询: > db.c4.find(); { "_id" : ObjectId("543e0b6ae2c90313035e7ce3"), "name" : "lisi", "age" : 24 } { "_id" : ObjectId("543e0d96e2c90313035e7ce4"), "name" : "wangwu", "age" : 20 } { "_id" : ObjectId("543e0da0e2c90313035e7ce5"), "name" : "zhaoliu", "age" : 25 } { "_id" : ObjectId("543e0da8e2c90313035e7ce6"), "name" : "tianqi", "age" : 25 } { "_id" : ObjectId("543e104fe2c90313035e7ce7"), "name" : "tuzuoquan", "age" : 1 } { "_id" : ObjectId("543e104fe2c90313035e7ce8"), "name" : "tuzuoquan", "age" : 2 } { "_id" : ObjectId("543e104fe2c90313035e7ce9"), "name" : "tuzuoquan", "age" : 3 } { "_id" : ObjectId("543e104fe2c90313035e7cea"), "name" : "tuzuoquan", "age" : 4 } { "_id" : ObjectId("543e104fe2c90313035e7ceb"), "name" : "tuzuoquan", "age" : 5 } { "_id" : ObjectId("543e104fe2c90313035e7cec"), "name" : "tuzuoquan", "age" : 6 } { "_id" : ObjectId("543e104fe2c90313035e7ced"), "name" : "tuzuoquan", "age" : 7 } { "_id" : ObjectId("543e104fe2c90313035e7cee"), "name" : "tuzuoquan", "age" : 8 } { "_id" : ObjectId("543e104fe2c90313035e7cef"), "name" : "tuzuoquan", "age" : 9 } { "_id" : ObjectId("543e104fe2c90313035e7cf0"), "name" : "tuzuoquan", "age" : 10 } { "_id" : ObjectId("543e104fe2c90313035e7cf1"), "name" : "tuzuoquan", "age" : 11 } { "_id" : ObjectId("543e104fe2c90313035e7cf2"), "name" : "tuzuoquan", "age" : 12 } { "_id" : ObjectId("543e104fe2c90313035e7cf3"), "name" : "tuzuoquan", "age" : 13 } { "_id" : ObjectId("543e104fe2c90313035e7cf4"), "name" : "tuzuoquan", "age" : 14 } { "_id" : ObjectId("543e104fe2c90313035e7cf5"), "name" : "tuzuoquan", "age" : 15 } { "_id" : ObjectId("543e104fe2c90313035e7cf6"), "name" : "tuzuoquan", "age" : 16 } Type "it" for more > it { "_id" : ObjectId("543e104fe2c90313035e7cf7"), "name" : "tuzuoquan", "age" : 17 } { "_id" : ObjectId("543e104fe2c90313035e7cf8"), "name" : "tuzuoquan", "age" : 18 } { "_id" : ObjectId("543e104fe2c90313035e7cf9"), "name" : "tuzuoquan", "age" : 19 } { "_id" : ObjectId("543e104fe2c90313035e7cfa"), "name" : "tuzuoquan", "age" : 20 } { "_id" : ObjectId("543e104fe2c90313035e7cfb"), "name" : "tuzuoquan", "age" : 21 } { "_id" : ObjectId("543e104fe2c90313035e7cfc"), "name" : "tuzuoquan", "age" : 22 } { "_id" : ObjectId("543e104fe2c90313035e7cfd"), "name" : "tuzuoquan", "age" : 23 } { "_id" : ObjectId("543e104fe2c90313035e7cfe"), "name" : "tuzuoquan", "age" : 24 } { "_id" : ObjectId("543e104fe2c90313035e7cff"), "name" : "tuzuoquan", "age" : 25 } { "_id" : ObjectId("543e104fe2c90313035e7d00"), "name" : "tuzuoquan", "age" : 26 } { "_id" : ObjectId("543e104fe2c90313035e7d01"), "name" : "tuzuoquan", "age" : 27 } { "_id" : ObjectId("543e104fe2c90313035e7d02"), "name" : "tuzuoquan", "age" : 28 } { "_id" : ObjectId("543e104fe2c90313035e7d03"), "name" : "tuzuoquan", "age" : 29 } { "_id" : ObjectId("543e104fe2c90313035e7d04"), "name" : "tuzuoquan", "age" : 30 } > db.c4.count(); 34 >总结: Mongodb客户端默认一次显示20条记录
Eg:显示age大于20个数据: > db.c4.find({age:{$gt:20}}); { "_id" : ObjectId("543e0b6ae2c90313035e7ce3"), "name" : "lisi", "age" : 24 } { "_id" : ObjectId("543e0da0e2c90313035e7ce5"), "name" : "zhaoliu", "age" : 25 } { "_id" : ObjectId("543e0da8e2c90313035e7ce6"), "name" : "tianqi", "age" : 25 } { "_id" : ObjectId("543e104fe2c90313035e7cfb"), "name" : "tuzuoquan", "age" : 21 } { "_id" : ObjectId("543e104fe2c90313035e7cfc"), "name" : "tuzuoquan", "age" : 22 } { "_id" : ObjectId("543e104fe2c90313035e7cfd"), "name" : "tuzuoquan", "age" : 23 } { "_id" : ObjectId("543e104fe2c90313035e7cfe"), "name" : "tuzuoquan", "age" : 24 } { "_id" : ObjectId("543e104fe2c90313035e7cff"), "name" : "tuzuoquan", "age" : 25 } { "_id" : ObjectId("543e104fe2c90313035e7d00"), "name" : "tuzuoquan", "age" : 26 } { "_id" : ObjectId("543e104fe2c90313035e7d01"), "name" : "tuzuoquan", "age" : 27 } { "_id" : ObjectId("543e104fe2c90313035e7d02"), "name" : "tuzuoquan", "age" : 28 } { "_id" : ObjectId("543e104fe2c90313035e7d03"), "name" : "tuzuoquan", "age" : 29 } { "_id" : ObjectId("543e104fe2c90313035e7d04"), "name" : "tuzuoquan", "age" : 30 }
Eg:显示age >= 25的数据: db.c4.find({age:{$gte:25}}); > db.c4.find({age:{$gte:25}}); { "_id" : ObjectId("543e0da0e2c90313035e7ce5"), "name" : "zhaoliu", "age" : 25 } { "_id" : ObjectId("543e0da8e2c90313035e7ce6"), "name" : "tianqi", "age" : 25 } { "_id" : ObjectId("543e104fe2c90313035e7cff"), "name" : "tuzuoquan", "age" : 25 } { "_id" : ObjectId("543e104fe2c90313035e7d00"), "name" : "tuzuoquan", "age" : 26 } { "_id" : ObjectId("543e104fe2c90313035e7d01"), "name" : "tuzuoquan", "age" : 27 } { "_id" : ObjectId("543e104fe2c90313035e7d02"), "name" : "tuzuoquan", "age" : 28 } { "_id" : ObjectId("543e104fe2c90313035e7d03"), "name" : "tuzuoquan", "age" : 29 } { "_id" : ObjectId("543e104fe2c90313035e7d04"), "name" : "tuzuoquan", "age" : 30 } >
Eg:显示age <= 15的数据: > db.c4.find({age:{$lte:15}}); { "_id" : ObjectId("543e104fe2c90313035e7ce7"), "name" : "tuzuoquan", "age" : 1 } { "_id" : ObjectId("543e104fe2c90313035e7ce8"), "name" : "tuzuoquan", "age" : 2 } { "_id" : ObjectId("543e104fe2c90313035e7ce9"), "name" : "tuzuoquan", "age" : 3 } { "_id" : ObjectId("543e104fe2c90313035e7cea"), "name" : "tuzuoquan", "age" : 4 } { "_id" : ObjectId("543e104fe2c90313035e7ceb"), "name" : "tuzuoquan", "age" : 5 } { "_id" : ObjectId("543e104fe2c90313035e7cec"), "name" : "tuzuoquan", "age" : 6 } { "_id" : ObjectId("543e104fe2c90313035e7ced"), "name" : "tuzuoquan", "age" : 7 } { "_id" : ObjectId("543e104fe2c90313035e7cee"), "name" : "tuzuoquan", "age" : 8 } { "_id" : ObjectId("543e104fe2c90313035e7cef"), "name" : "tuzuoquan", "age" : 9 } { "_id" : ObjectId("543e104fe2c90313035e7cf0"), "name" : "tuzuoquan", "age" : 10 } { "_id" : ObjectId("543e104fe2c90313035e7cf1"), "name" : "tuzuoquan", "age" : 11 } { "_id" : ObjectId("543e104fe2c90313035e7cf2"), "name" : "tuzuoquan", "age" : 12 } { "_id" : ObjectId("543e104fe2c90313035e7cf3"), "name" : "tuzuoquan", "age" : 13 } { "_id" : ObjectId("543e104fe2c90313035e7cf4"), "name" : "tuzuoquan", "age" : 14 } { "_id" : ObjectId("543e104fe2c90313035e7cf5"), "name" : "tuzuoquan", "age" : 15 } >
Eg:显示age不等于 2的数据: > db.c4.find({age:{$ne:2}}); { "_id" : ObjectId("543e0b6ae2c90313035e7ce3"), "name" : "lisi", "age" : 24 } { "_id" : ObjectId("543e0d96e2c90313035e7ce4"), "name" : "wangwu", "age" : 20 } { "_id" : ObjectId("543e0da0e2c90313035e7ce5"), "name" : "zhaoliu", "age" : 25 } { "_id" : ObjectId("543e0da8e2c90313035e7ce6"), "name" : "tianqi", "age" : 25 } { "_id" : ObjectId("543e104fe2c90313035e7ce7"), "name" : "tuzuoquan", "age" : 1 } { "_id" : ObjectId("543e104fe2c90313035e7ce9"), "name" : "tuzuoquan", "age" : 3 } { "_id" : ObjectId("543e104fe2c90313035e7cea"), "name" : "tuzuoquan", "age" : 4 } { "_id" : ObjectId("543e104fe2c90313035e7ceb"), "name" : "tuzuoquan", "age" : 5 } { "_id" : ObjectId("543e104fe2c90313035e7cec"), "name" : "tuzuoquan", "age" : 6 } { "_id" : ObjectId("543e104fe2c90313035e7ced"), "name" : "tuzuoquan", "age" : 7 } { "_id" : ObjectId("543e104fe2c90313035e7cee"), "name" : "tuzuoquan", "age" : 8 } { "_id" : ObjectId("543e104fe2c90313035e7cef"), "name" : "tuzuoquan", "age" : 9 } { "_id" : ObjectId("543e104fe2c90313035e7cf0"), "name" : "tuzuoquan", "age" : 10 } { "_id" : ObjectId("543e104fe2c90313035e7cf1"), "name" : "tuzuoquan", "age" : 11 } { "_id" : ObjectId("543e104fe2c90313035e7cf2"), "name" : "tuzuoquan", "age" : 12 } { "_id" : ObjectId("543e104fe2c90313035e7cf3"), "name" : "tuzuoquan", "age" : 13 } { "_id" : ObjectId("543e104fe2c90313035e7cf4"), "name" : "tuzuoquan", "age" : 14 } { "_id" : ObjectId("543e104fe2c90313035e7cf5"), "name" : "tuzuoquan", "age" : 15 } { "_id" : ObjectId("543e104fe2c90313035e7cf6"), "name" : "tuzuoquan", "age" : 16 } { "_id" : ObjectId("543e104fe2c90313035e7cf7"), "name" : "tuzuoquan", "age" : 17 } Type "it" for more >
> db.c4.find().count(); 34 > db.c4.find({age:{$ne:20}}).count(); 32 > db.c4.find({age:{$ne:2}}).count(); 33 >
|
E :分页 db.c4.find().skip(0).limit(10); > db.c4.find().skip(0).limit(10); { "_id" : ObjectId("543e0b6ae2c90313035e7ce3"), "name" : "lisi", "age" : 24 } { "_id" : ObjectId("543e0d96e2c90313035e7ce4"), "name" : "wangwu", "age" : 20 } { "_id" : ObjectId("543e0da0e2c90313035e7ce5"), "name" : "zhaoliu", "age" : 25 } { "_id" : ObjectId("543e0da8e2c90313035e7ce6"), "name" : "tianqi", "age" : 25 } { "_id" : ObjectId("543e104fe2c90313035e7ce7"), "name" : "tuzuoquan", "age" : 1 } { "_id" : ObjectId("543e104fe2c90313035e7ce8"), "name" : "tuzuoquan", "age" : 2 } { "_id" : ObjectId("543e104fe2c90313035e7ce9"), "name" : "tuzuoquan", "age" : 3 } { "_id" : ObjectId("543e104fe2c90313035e7cea"), "name" : "tuzuoquan", "age" : 4 } { "_id" : ObjectId("543e104fe2c90313035e7ceb"), "name" : "tuzuoquan", "age" : 5 } { "_id" : ObjectId("543e104fe2c90313035e7cec"), "name" : "tuzuoquan", "age" : 6 } >
Eg:查询第
|