很简单:
brew update
brew install mongodb
耐心等待安装完毕,出现提示:
To have launchd start mongodb now and restart at login: brew services start mongodb --后台启动 Or, if you don't want/need a background service you can just run: mongod --config /usr/local/etc/mongod.conf --临时启动
安装就完事儿了。
使用:shell控制台 >mongo 即可。
常用命令:
> show dbs
admin 0.000GB
local 0.000GB
test 0.000GB
> db
test
> db.test.insert({title:'firstOne',age:10})
WriteResult({ "nInserted" : 1 })
> db.test.insert({title:'second',name:20,sex:'woman'})
WriteResult({ "nInserted" : 1 })
> db.test.find()
{ "_id" : ObjectId("58b281b13dbc5b19bfa74e7e"), "title" : "firstTest", "name" : "liuyang", "age" : 10 }
{ "_id" : ObjectId("58b282273dbc5b19bfa74e7f"), "title" : "firstOne", "age" : 10 }
{ "_id" : ObjectId("58b282593dbc5b19bfa74e80"), "title" : "second", "name" : 20, "sex" : "woman" }
>
> db.test.find()
{ "_id" : ObjectId("58b281b13dbc5b19bfa74e7e"), "title" : "firstTest", "name" : "liuyang", "age" : 10 }
{ "_id" : ObjectId("58b282273dbc5b19bfa74e7f"), "title" : "firstOne", "age" : 10 }
{ "_id" : ObjectId("58b282593dbc5b19bfa74e80"), "title" : "second", "name" : 20, "sex" : "woman" }
> db.test.find().pretty()
{
"_id" : ObjectId("58b281b13dbc5b19bfa74e7e"),
"title" : "firstTest",
"name" : "liuyang",
"age" : 10
}
{
"_id" : ObjectId("58b282273dbc5b19bfa74e7f"),
"title" : "firstOne",
"age" : 10
}
{
"_id" : ObjectId("58b282593dbc5b19bfa74e80"),
"title" : "second",
"name" : 20,
"sex" : "woman"
}
>
> db.test.find({title:'firstOne'}).pretty()
{
"_id" : ObjectId("58b282273dbc5b19bfa74e7f"),
"title" : "firstOne",
"age" : 10
}
>
> db.test.find({$or:[{title:'firstOne'},{sex:'woman'}]}).pretty()
{
"_id" : ObjectId("58b282273dbc5b19bfa74e7f"),
"title" : "firstOne",
"age" : 10
}
{
"_id" : ObjectId("58b282593dbc5b19bfa74e80"),
"title" : "second",
"name" : 20,
"sex" : "woman"
}
>
> db.test.find({age:{$gt:5}}).pretty()
{
"_id" : ObjectId("58b281b13dbc5b19bfa74e7e"),
"title" : "firstTest",
"name" : "liuyang",
"age" : 10
}
{
"_id" : ObjectId("58b282273dbc5b19bfa74e7f"),
"title" : "firstOne",
"age" : 10
}
>
> db.test.find({title:'firstTest'},{name:1,_id:0}) --只返回指定字段 1 -show 0-hide
>db.test.find().sort({age:0})
> db.test.updateOne({title:'firstTest'},{$set:{age:30}})