MongoDB学习

一 有用的网站资源

Manual

https://docs.mongodb.com/manual/

Document

https://docs.mongodb.com/manual/reference/

IT笔录 学习,记录,整理

https://itbilu.com/database/mongo

1 MongoDB官网
http://www.mongodb.org
安装包下载 使用文档 只能下载稳定版
2 MongoDB国内网站
http://www.mongoing.com
3 中文文档
http://docs.mongoing.com/manual-zh
并未全部完成
4 GitHub : https://github.com/mongodb
源码集工具,可以下载所有版本
5 BUG发现
https://jira.mongodb.org/browse/TOOLS-352?jql=

二 MongoDB的安装
http://www.cnblogs.com/dannyheart/p/4616375.html

终端访问工具:
Robomongo 0.9.0 https://robomongo.org/download
下载版本:0.9.0 Oct 6, 2016 Full Size Editor with Dockable Output Window

1 根据配置文件启动MongoDB服务器
在cmd环境中执行命令: "C:\Program Files\MongoDB\Server\3.4\bin\mongod.exe" -f "E:\MongoDB\mongo.conf"
或添加path到系统环境变量之后,可以使用Git在mongo.conf文件所在的路径下执行命令:mongod -f mongo.conf
如果MongoDB服务器正常启动,通过浏览器访问localhost:27017 ,可以看到以下页面:

2 启动Mongo Shell环境
在本地 cmd 环境下执行mongo命令即可调用Mongodb的Shell环境。启动之后会出现以下的界面:

接下来,可以在Mongo Shell环境下输入命令对Mongo数据库进行操作:

Microsoft Windows [版本 6.1.7601]
版权所有 (c) 2009 Microsoft Corporation。保留所有权利。

C:\Users\david.zhong>mongo
MongoDB shell version v3.4.9
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.9
Server has startup warnings:
2017-10-20T16:12:27.639+0800 I CONTROL  [initandlisten]
2017-10-20T16:12:27.639+0800 I CONTROL  [initandlisten] ** WARNING: Access contr
ol is not enabled for the database.
2017-10-20T16:12:27.639+0800 I CONTROL  [initandlisten] **          Read and wri
te access to data and configuration is unrestricted.
2017-10-20T16:12:27.639+0800 I CONTROL  [initandlisten]
2017-10-20T16:12:27.639+0800 I CONTROL  [initandlisten] Hotfix KB2731284 or late
r update is not installed, will zero-out data files.
2017-10-20T16:12:27.639+0800 I CONTROL  [initandlisten]
> db
test
> show dbs
admin  0.000GB
local  0.000GB
> db
test
> ^C
bye

C:\Users\david.zhong>mongo
MongoDB shell version v3.4.9
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.9
Server has startup warnings:
2017-10-20T16:12:27.639+0800 I CONTROL  [initandlisten]
2017-10-20T16:12:27.639+0800 I CONTROL  [initandlisten] ** WARNING: Access contr
ol is not enabled for the database.
2017-10-20T16:12:27.639+0800 I CONTROL  [initandlisten] **          Read and wri
te access to data and configuration is unrestricted.
2017-10-20T16:12:27.639+0800 I CONTROL  [initandlisten]
2017-10-20T16:12:27.639+0800 I CONTROL  [initandlisten] Hotfix KB2731284 or late
r update is not installed, will zero-out data files.
2017-10-20T16:12:27.639+0800 I CONTROL  [initandlisten]
> db
test
> x = 200
200
> x/5
40
> Math.sin(Math.PI / 2)
1
> new Date("2010/1/1")
ISODate("2009-12-31T16:00:00Z")
> new Date("2010/1/2")
ISODate("2010-01-01T16:00:00Z")
> "Hello World!".replace("World","MongoDB")
Hello MongoDB!
> function factorial (n) {
... if (n <= 1) return 1;
... return n * factorial(n - 1);
... }
> factorial(5);
120
> db
test
> use foobar
switched to db foobar
> show dbs
admin  0.000GB
local  0.000GB
> db
foobar
> post = {"title" : "My Blog Post",
... "Content": "Here is my blog post.",
... "date": new Date()}
{
        "title" : "My Blog Post",
        "Content" : "Here is my blog post.",
        "date" : ISODate("2017-10-23T01:44:57.403Z")
}
> db.blog.insert(post)
WriteResult({ "nInserted" : 1 })
> db.blog.find()
{ "_id" : ObjectId("59ed49a79e5815c75385a452"), "title" : "My Blog Post", "Conte
nt" : "Here is my blog post.", "date" : ISODate("2017-10-23T01:44:57.403Z") }
> db.blog.findOne()
{
        "_id" : ObjectId("59ed49a79e5815c75385a452"),
        "title" : "My Blog Post",
        "Content" : "Here is my blog post.",
        "date" : ISODate("2017-10-23T01:44:57.403Z")
}
> post.comments = []
[ ]
> db.blog.update({title : "My Blog Post"},post)
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.blog.find()
{ "_id" : ObjectId("59ed49a79e5815c75385a452"), "title" : "My Blog Post", "Conte
nt" : "Here is my blog post.", "date" : ISODate("2017-10-23T01:44:57.403Z"), "co
mments" : [ ] }
> db.blog.findOne()
{
        "_id" : ObjectId("59ed49a79e5815c75385a452"),
        "title" : "My Blog Post",
        "Content" : "Here is my blog post.",
        "date" : ISODate("2017-10-23T01:44:57.403Z"),
        "comments" : [ ]
}
> db.blog.remove("title : "My Blog Post")
2017-10-23T09:51:27.334+0800 E QUERY    [thread1] SyntaxError: missing ) after a
rgument list @(shell):1:25
> db.blog.remove(title : "My Blog Post")
2017-10-23T09:52:04.432+0800 E QUERY    [thread1] SyntaxError: missing ) after a
rgument list @(shell):1:21
> db.blog.remove({title : "My Blog Post"})
WriteResult({ "nRemoved" : 1 })
> db.blog.find()
>

以上执行mongo默认访问本地Mongo中的test数据库,且由于缺省安装没有设置密码,因此可以执行以上命令。
但是实际应用中如果想访问Mongo数据库,需要执行以下格式的命令
mongo 121.201.8.102:27014/dbname -utest -p123456

如果想同时关注MongoDB的数据库可视化以及Shell命令的执行,可以使用工具Robomongo 0.9.0

值得注意的是,在Shell环境中,以下两条命令是的执行结果是相同的:

db.getCollection('LoanApplication').find({})
db.LoanApplication.find()

第一种方式更像是严格的使用db对象的API方法进行访问
第二种方式更像是一种js语言的object对象的快捷方式

posted @ 2017-10-19 23:49  当归远志  阅读(222)  评论(0编辑  收藏  举报