MongoDB安装
c:\MongoDB
c:\MongoDB
下新建 data\db
目录
mongod.exe:
C:\>cd C:\MongoDB\bin
C:\MongoDB\bin>mongod.exe --dbpath=C:\MongoDB\data\db --directoryperdb --logpath=C:\MongoDB\data\logs --logappend
mongo.exe
(不要关闭 mongod.exe
这个命令行窗口)MongoDB 没有新建数据库的命令,只要进行
insert
或其它操作,MongoDB 就会自动帮你建立数据库和collection
。当查询一个不存在的collection
时也不会出错,Mongo 会认为那是一个空的collection
。一个对象被插入到数据库中时,如果它没有 ID,会自动生成一个“
_id
”字段,为12
字节(24位)16
进制数。 当然如果插入文档不带_id,则系统会帮你自动创建一个,如果自己指定了就用自己指定的。
字段名限制:不能以“$”开头;不能包含“.”
;“_id”
是系统保留的字段,但用户可以自己储存唯一性的数据在字段中。
show dbs
// 列出所有数据库
use memo
// 使用数据库 memo。即使这个数据库不存在也可以执行,但该数据库不会立刻被新建,要等到执行了insert等的操作时,才会建立这个数据库。
show collections
// 列出当前数据库的collections(当前数据库下的表)
db
// 显示当前数据库
show users
// 列出用户
db.表名.insert({name:"jack",addr:"fujian"})
;//向表插入字段
db.表名.find();
//查询-作用相当于 select * from
表名
$lt
->less then 小于
$lte
->less than and equal 不大于
-$lt
->less then 小于
$gt
->greater then 大于
$gte
->greater then and equal 不小于)
$ne
->not equal 不等于
db.foo.find() // select * from foo
db.foo.find().limit(10) // select * from foo limit 10
db.foo.find().sort({x:1}) // select * from foo order by x asc 1:升序 -1:降序
db.foo.find().sort({x:1}).skip(5).limit(10) // select * from foo order by x asc limit 5, 10
db.foo.find({x:10}) // select * from foo where x = 10
db.foo.find({x: {$lt:10}}) // select * from foo where x <= 10
db.foo.find({}, {y:true}) // select y from foo
// 一些SQL不能做的,MongoDB也可以做:
db.foo.find({"address.city":"gz"}) // 搜索嵌套文档address中city值为gz的记录
db.foo.find({likes:"math"}) // 搜索数组
db.foo.ensureIndex({"address.city":1}) // 在嵌套文档的字段上建索引
db.foo.update({},{})
//更新对象,第一个参数是查询对象,第二个是替代的,可以在第二个对象里指定更新哪些字段,要使用set"用来指定一个键的值。如果这个键不存在,则创建它,如果存在则更新
db.foo.update({name:"jack"},{$set:{name:"zky"}});
db.foo.remove({});/
/第一个参数要删除的记录,只删除匹配的对象db.foo.drop();
//删除foo这个表db.dropDatebase();
$push
:增加数组元素;db.foo.update({name:'xxx'},{$push:{age:"10"}});
$pop
:减少数组元素;
db.people.find({"name":"yhb",$or:[{"age":18},{"age":20}]})
//找出name为yhb,age为18或者20的;
db.people.find({"addr":{$exists:false}})
//找出不存在addr field
db.people.find({$or:[{"name":"yhb","age":18},{"name":"lwy","age":19}]})
// 找出 people 中 name 为 yhb,年龄为 18,或者 name 为 lwy,name 为 19 的
如果是or: ->> {
db.foo.ensureIndex({productid:1}) // 在productid上建立普通索引
db.foo.ensureIndex({district:1, plate:1}) // 多字段索引
db.foo.ensureIndex({productid:1}, {unique:true}) // 唯一索引
命令行执行:
c:\MongoDB\bin\mongodump.exe --help 查看帮助命令
示例-备份数据库:
命令行执行:
c:\MongoDB\bin\mongodump -o ../data/backup/test (备份数据到backuo/test目录下)
示例-恢复数据库:
命令行执行:
c:\MongoDB\bin\mongorestore -d test -c t002 c:\MongoDB\data\backup\test\t002.bson
每次只导入\导出一个表,json或csv格式;
示例:
mongoimport -d test -c t004 drop c:\MongoDB\data\backup\test_t001.json
示例:
mongoexport -d test -c t001 -o c:\MongoDB\data\backup\test_t001.json
MongoDB本身是没有开启安全性检查的,在开启之前,需要至少一个管理员账号。
开启安全性检查,只有数据库认证用户才能执行读或写的操作。
注:V3版本mongoDB已经不再使用addUser,而是采用了db.createUser;
示例:
use admin
db.createUser(
{
user: "accountUser",
pwd: "password",
roles: [ "readWrite", "dbAdmin" ]
}
)
1、重启客户端与服务器;
2、然后切换到客户端命令行:
use admin
db.auth("root","root")
这样,某种权限的用户只有切换到对应的数据库才能执行某些操作。
http://www.open-open.com/lib/view/open1335003001358.html
http://www.runoob.com/mongodb/mongodb-tutorial.html