Win10 安装 MongoDB4

MongoDB社区版与企业版没多大区别,企业版主要是多了一些权限控制,一般装社区版够用了。

一、下载

https://www.mongodb.com/download-center/community

页面有三个选项卡,Cloud,Server,Tools,

选Server下载:mongodb-win32-x86_64-2012plus-4.2.5-signed.msi

https://fastdl.mongodb.org/win32/mongodb-win32-x86_64-2012plus-4.2.5-signed.msi

Win10 安装 MongoDB4_第1张图片

linux版,有tgz的,也有rpm的

https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel80-4.2.5.tgz

Win10 安装 MongoDB4_第2张图片

选Tools下载:mongodb-compass-1.20.5-win32-x64.msi,MongoDB Compass 是一个图形界面管理工具

https://downloads.mongodb.com/compass/mongodb-compass-1.20.5-win32-x64.msi

Win10 安装 MongoDB4_第3张图片

 

二、安装

1.Next

Win10 安装 MongoDB4_第4张图片

2.同意

Win10 安装 MongoDB4_第5张图片

3.Custom

Win10 安装 MongoDB4_第6张图片

4.选择安装路径

Win10 安装 MongoDB4_第7张图片

5.MongoDB4.2.5会自动创建服务,并且创建data、log目录,并配置好

Win10 安装 MongoDB4_第8张图片

6.不要勾选Install MongoDB Compass。勾选了,他下载特别慢,会超时。

我们可以自己下,自己安。

他的下载链接是这个,不用这个

https://s3.amazonaws.com/downloads.10gen.com/compass/mongodb-compass-community-1.20.5-win32-x64.exe

Win10 安装 MongoDB4_第9张图片

7.Install

Win10 安装 MongoDB4_第10张图片

8.如果勾选了Install MongoDB Compass,安装特别特别慢,也安不上这个

Win10 安装 MongoDB4_第11张图片

9.安装好的服务

10.浏览器

Win10 安装 MongoDB4_第12张图片

11.文件夹

Win10 安装 MongoDB4_第13张图片

12.bin/mongod.cfg

# mongod.conf

# for documentation of all options, see:
#   http://docs.mongodb.org/manual/reference/configuration-options/

# Where and how to store data.
storage:
  dbPath: D:\Program Files\MongoDB\Server\4.2\data
  journal:
    enabled: true
#  engine:
#  mmapv1:
#  wiredTiger:

# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path:  D:\Program Files\MongoDB\Server\4.2\log\mongod.log

# network interfaces
net:
  port: 27017
  bindIp: 127.0.0.1


#processManagement:

#security:

#operationProfiling:

#replication:

#sharding:

## Enterprise-Only Options:

13.log/mongod.log

2020-04-03T02:55:48.707+0800 I  CONTROL  [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'
2020-04-03T02:55:49.600+0800 W  ASIO     [main] No TransportLayer configured during NetworkInterface startup
2020-04-03T02:55:49.601+0800 I  CONTROL  [main] Trying to start Windows service 'MongoDB'
2020-04-03T02:55:49.602+0800 I  CONTROL  [initandlisten] MongoDB starting : pid=28048 port=27017 dbpath=D:\Program Files\MongoDB\Server\4.2\data 64-bit host=PC-20
2020-04-03T02:55:49.603+0800 I  CONTROL  [initandlisten] targetMinOS: Windows 7/Windows Server 2008 R2
2020-04-03T02:55:49.603+0800 I  CONTROL  [initandlisten] db version v4.2.5
2020-04-03T02:55:49.603+0800 I  CONTROL  [initandlisten] git version: 2261279b51ea13df08ae708ff278f0679c59dc32
2020-04-03T02:55:49.603+0800 I  CONTROL  [initandlisten] allocator: tcmalloc
2020-04-03T02:55:49.604+0800 I  CONTROL  [initandlisten] modules: none
2020-04-03T02:55:49.604+0800 I  CONTROL  [initandlisten] build environment:
2020-04-03T02:55:49.604+0800 I  CONTROL  [initandlisten]     distmod: 2012plus
2020-04-03T02:55:49.604+0800 I  CONTROL  [initandlisten]     distarch: x86_64
2020-04-03T02:55:49.604+0800 I  CONTROL  [initandlisten]     target_arch: x86_64
2020-04-03T02:55:49.604+0800 I  CONTROL  [initandlisten] options: { config: "D:\Program Files\MongoDB\Server\4.2\bin\mongod.cfg", net: { bindIp: "127.0.0.1", port: 27017 }, service: true, storage: { dbPath: "D:\Program Files\MongoDB\Server\4.2\data", journal: { enabled: true } }, systemLog: { destination: "file", logAppend: true, path: "D:\Program Files\MongoDB\Server\4.2\log\mongod.log" } }
2020-04-03T02:55:49.627+0800 I  STORAGE  [initandlisten] wiredtiger_open config: create,cache_size=7630M,cache_overflow=(file_max=0M),session_max=33000,eviction=(threads_min=4,threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000,close_scan_interval=10,close_handle_minimum=250),statistics_log=(wait=0),verbose=[recovery_progress,checkpoint_progress],
2020-04-03T02:55:49.882+0800 I  STORAGE  [initandlisten] WiredTiger message [1585853749:882828][28048:140719030820432], txn-recover: Set global recovery timestamp: (0, 0)
2020-04-03T02:55:50.030+0800 I  RECOVERY [initandlisten] WiredTiger recoveryTimestamp. Ts: Timestamp(0, 0)
2020-04-03T02:55:50.152+0800 I  STORAGE  [initandlisten] Timestamp monitor starting
2020-04-03T02:55:50.251+0800 I  CONTROL  [initandlisten] 
2020-04-03T02:55:50.251+0800 I  CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2020-04-03T02:55:50.251+0800 I  CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2020-04-03T02:55:50.251+0800 I  CONTROL  [initandlisten] 
2020-04-03T02:55:50.252+0800 I  STORAGE  [initandlisten] createCollection: admin.system.version with provided UUID: 1a397c45-7cb2-447e-944b-974c000d5f00 and options: { uuid: UUID("1a397c45-7cb2-447e-944b-974c000d5f00") }
2020-04-03T02:55:50.360+0800 I  INDEX    [initandlisten] index build: done building index _id_ on ns admin.system.version
2020-04-03T02:55:50.361+0800 I  SHARDING [initandlisten] Marking collection admin.system.version as collection version: 
2020-04-03T02:55:50.361+0800 I  COMMAND  [initandlisten] setting featureCompatibilityVersion to 4.2
2020-04-03T02:55:50.362+0800 I  SHARDING [initandlisten] Marking collection local.system.replset as collection version: 
2020-04-03T02:55:50.362+0800 I  STORAGE  [initandlisten] Flow Control is enabled on this deployment.
2020-04-03T02:55:50.363+0800 I  SHARDING [initandlisten] Marking collection admin.system.roles as collection version: 
2020-04-03T02:55:50.364+0800 I  STORAGE  [initandlisten] createCollection: local.startup_log with generated UUID: 0131eaaa-ae42-451d-8bb0-bf1903679c1b and options: { capped: true, size: 10485760 }
2020-04-03T02:55:50.470+0800 I  INDEX    [initandlisten] index build: done building index _id_ on ns local.startup_log
2020-04-03T02:55:50.470+0800 I  SHARDING [initandlisten] Marking collection local.startup_log as collection version: 
2020-04-03T02:55:51.216+0800 I  FTDC     [initandlisten] Initializing full-time diagnostic data capture with directory 'D:/Program Files/MongoDB/Server/4.2/data/diagnostic.data'
2020-04-03T02:55:51.218+0800 I  SHARDING [LogicalSessionCacheRefresh] Marking collection config.system.sessions as collection version: 
2020-04-03T02:55:51.219+0800 I  CONTROL  [LogicalSessionCacheReap] Sessions collection is not set up; waiting until next sessions reap interval: config.system.sessions does not exist
2020-04-03T02:55:51.219+0800 I  STORAGE  [LogicalSessionCacheRefresh] createCollection: config.system.sessions with provided UUID: ae698652-e9a4-44c5-9b3f-1e5932cba162 and options: { uuid: UUID("ae698652-e9a4-44c5-9b3f-1e5932cba162") }
2020-04-03T02:55:51.219+0800 I  NETWORK  [listener] Listening on 127.0.0.1
2020-04-03T02:55:51.219+0800 I  NETWORK  [listener] waiting for connections on port 27017
2020-04-03T02:55:51.219+0800 I  STORAGE  [initandlisten] Service running
2020-04-03T02:55:51.374+0800 I  INDEX    [LogicalSessionCacheRefresh] index build: done building index _id_ on ns config.system.sessions
2020-04-03T02:55:51.472+0800 I  INDEX    [LogicalSessionCacheRefresh] index build: starting on config.system.sessions properties: { v: 2, key: { lastUse: 1 }, name: "lsidTTLIndex", ns: "config.system.sessions", expireAfterSeconds: 1800 } using method: Hybrid
2020-04-03T02:55:51.472+0800 I  INDEX    [LogicalSessionCacheRefresh] build may temporarily use up to 200 megabytes of RAM
2020-04-03T02:55:51.473+0800 I  INDEX    [LogicalSessionCacheRefresh] index build: collection scan done. scanned 0 total records in 0 seconds
2020-04-03T02:55:51.474+0800 I  INDEX    [LogicalSessionCacheRefresh] index build: inserted 0 keys from external sorter into index in 0 seconds
2020-04-03T02:55:51.497+0800 I  INDEX    [LogicalSessionCacheRefresh] index build: done building index lsidTTLIndex on ns config.system.sessions
2020-04-03T02:55:51.522+0800 I  COMMAND  [LogicalSessionCacheRefresh] command config.system.sessions command: createIndexes { createIndexes: "system.sessions", indexes: [ { key: { lastUse: 1 }, name: "lsidTTLIndex", expireAfterSeconds: 1800 } ], $db: "config" } numYields:0 reslen:114 locks:{ ParallelBatchWriterMode: { acquireCount: { r: 2 } }, ReplicationStateTransition: { acquireCount: { w: 3 } }, Global: { acquireCount: { r: 1, w: 2 } }, Database: { acquireCount: { r: 1, w: 2, W: 1 } }, Collection: { acquireCount: { r: 4, w: 1, R: 1, W: 2 } }, Mutex: { acquireCount: { r: 3 } } } flowControl:{ acquireCount: 1, timeAcquiringMicros: 1 } storage:{} protocol:op_msg 303ms
2020-04-03T02:55:52.002+0800 I  SHARDING [ftdc] Marking collection local.oplog.rs as collection version: 

三、使用

1.配置环境变量(可选)

设置用户的环境变量
Path添加
D:\Program Files\MongoDB\Server\4.2\bin

2.打开cmd

MongoDB Shell是MongoDB自带的交互式Javascript shell,用来对MongoDB进行操作和管理的交互式环境。
你进入mongoDB后台后,它默认会链接到 test 文档(数据库):

D:\Program Files\MongoDB\Server\4.2\bin>mongo.exe
MongoDB shell version v4.2.5
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("6f38ac9e-1163-4ac1-83f4-086948b9d511") }
MongoDB server version: 4.2.5
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
        http://docs.mongodb.org/
Questions? Try the support group
        http://groups.google.com/group/mongodb-user
Server has startup warnings:
2020-04-03T02:55:50.251+0800 I  CONTROL  [initandlisten]
2020-04-03T02:55:50.251+0800 I  CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2020-04-03T02:55:50.251+0800 I  CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2020-04-03T02:55:50.251+0800 I  CONTROL  [initandlisten]
---
Enable MongoDB's free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).

The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.

To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---

> 2 + 2
4
> db
test
> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB
> use mydb
switched to db mydb
> db.users.insert({x:10})
WriteResult({ "nInserted" : 1 })
> db.users.find()
{ "_id" : ObjectId("5e8640faabb339e6969879ab"), "x" : 10 }
> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB
mydb    0.000GB
> show collections
users

以下命令是必须掌握的: 

mongo   
运行mongo

db    
显示当前的数据库名称

show dbs    
显示当前服务器下数据库(非空的数据库)列表

use test    
如果test数据库不存在,则创建test数据库
如果test已存在,则切换到test数据库

show collections    
显示当前数据库下所包含的集合(表)列表

db.users.insert({name:'zhangsha'})    
向users集合中插入数据
如果users集合存在,则直接插入数据,如果不存在,则创建users集合再插入数据

db.createCollection('products')
创建一个空集合products

db.products.insert([{name:'lishi'},{name:'wangwu'}])        
一次插入多个数据

db.products.find()
查询products集合中所有的数据

db.products.find({name:'苹果手机'})
查询products集合中name='苹果手机'的数据

db.products.find({name:{$eq:'苹果手机'}})
同上,$eq等号,建议使用上面的方式,易记,易输入
eq = equal

db.products.find({price:{$gt:18}})
查询products集合中price>18的数据  
 
把$gt换成如下的符号试试:
$gt大于   great
$gte大于等于 great equal
$lt小于   less than
$lte小于等于 less than equal
$ne不等于  not equal
$in在范围内
$nin不在范围内
以上几个符号格式总结为:{ field: {符号: value}}

db.products.find({name:/^华为/})
查找products集合中name域中以“华为”字符的开头的数据

db.products.find({name:{$in:['手机1','手机2']}})
查询products集合中name='手机1'和name='手机2'的数据
$in在范围内
$nin不在范围内
以上两个符号格式为:{ field:{符号:[value1,value2,....]}}

db.products.find({name:"华为手机",price:800})
查找name="华为手机"并且price:800的数据

db.products.find({$or:[{name:'华为手机'},{price:{$lt:1000}}]})
查询products集合中name='华为手机' 或者 price<1000的数据
$or或者  注意$or:[{},{},....]
$and并且  格式同$or, 例:{$and:[{},{},....]}
$nor:not or 与$or相反, 格式同$or

db.products.find({price:{$not:{$gt:100}}})
查询products集合中price<=100的数据,不存在price属性的数据也会查询出来
$not取反 

db.products.find({price:{$exists: true}})
查询products集合中包含域名称为price的数据

db.products.find({name:{$type:2}})
查询products集合中name属性为字符串类型的数据

db.products.find({
    $where: function(){
        return this.name == '华为手机'
    }
})
查询products集合中name='华为手机’的数据

db.products.find({
    $where: function(){
         return  this.name.indexOf('华为手机') > -1;
    }
})
查询products集合中name域中包含“华为手机”字符的数据

db.products.update({name:'华为手机'},{$set:{price:2000}},{
    upsert: true,
    multi:false
})
把products集合中name='华为手机'的那条数据,把price属性设置成2000,其它属性保留
$set是指更改的属性列表,不在列表中其他属性会被保留,如果不加此符号,其它属性会被丢弃(_id属性比较特殊,不会丢失)
upsert:true如果没有符号条件的更新时,则插入一条,为false时,则不会插入, 默认是false
multi:false一次只能更新一条数据,为true时,可更新多条,默认是false

db.students.remove({})
清空集合students

db.products.remove({name:'abc'})
删除products集合中name='abc'的数据,注意,即使把集合products中的所有数据都删除了
products集合仍然存在, remove()是用来删除数据的,而drop()不仅会删除数据,还会把
集合的结构给删除

db.products.drop()
把products集合彻底从当前数据中删除,集合products不再存在,注意与remove()的区别

db.dropDatabase()
删除当前数据库

db.users.distinct('name')
查询users集合中不重复的name属性,返回的是数组

db.stu.count({name:'zhangshan'})
查询stu集合中name='zhangshan'的数据数量

db.stu.find().limit(5)
查询stu集合中前5条数据

db.stu.find().skip(5)
查询stu集合中跳过前5条后的数据

db.stu.find().sort({name:1})
查询stu集合中的全部数据,并按name属性正序排列  注:1:正序 -1: 倒序

由于mongodb的api接口方法很多,除以上命令外,其他的命令请多看官方文档

参考:

Windows 平台安装 MongoDB
https://www.runoob.com/mongodb/mongodb-window-install.html
mongodb安装教程(一)
https://www.cnblogs.com/xiaozhaoboke/p/11479144.html

 

MongoDB 教程

https://www.runoob.com/mongodb/mongodb-tutorial.html

MongoDB中文网

https://www.mongodb.org.cn/

MongoDB教程

https://www.mongodb.org.cn/tutorial/

springboot2 连接mongodb4

https://blog.csdn.net/haveqing/article/details/105446205

你可能感兴趣的:(MongoDB)