查看: 1984|回复: 0

[JavaScript/JQuery] mongodb Mac系统安装与基础学习

发表于 2018-2-28 08:04:00
Mac系统下安装MongoDB 使用Homebrew安装mongodb 更新Homebrew数据包

brew update

安装mongodb

brew install mongodb

启动mongodb

通过命令mongod,将会使用系统默认的数据目录(/data/db)

mongod

也可以自己指定目录通过--dbpath来指向创建的目录

mongod --dbpath <自定义的目录路径>

出现以下信息链接成功

  1. 2018-01-28T21:20:50.190+0800 I CONTROL [initandlisten] MongoDB starting : pid=10488 port=27017 dbpath=/Users/qufei/documents/mongodb/data 64-bit host=BLM-XXXX-MBP.local
  2. 2018-01-28T21:20:50.191+0800 I CONTROL [initandlisten] db version v3.6.2
  3. 2018-01-28T21:20:50.191+0800 I CONTROL [initandlisten] git version: 489d177dbd0f0420a8ca04d39fd78d0a2c539420
  4. 2018-01-28T21:20:50.191+0800 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.0.2n 7 Dec 2017
  5. 2018-01-28T21:20:50.191+0800 I CONTROL [initandlisten] allocator: system
  6. 2018-01-28T21:20:50.191+0800 I CONTROL [initandlisten] modules: none
  7. 2018-01-28T21:20:50.191+0800 I CONTROL [initandlisten] build environment:
  8. 2018-01-28T21:20:50.191+0800 I CONTROL [initandlisten] distarch: x86_64
  9. 2018-01-28T21:20:50.191+0800 I CONTROL [initandlisten] target_arch: x86_64
  10. 2018-01-28T21:20:50.191+0800 I CONTROL [initandlisten] options: { storage: { dbPath: "/Users/test/documents/mongodb/data" } }
  11. 2018-01-28T21:20:50.192+0800 I - [initandlisten] Detected data files in /Users/qufei/documents/mongodb/data created by the 'wiredTiger' storage engine, so setting the active storage engine to 'wiredTiger'.
  12. 2018-01-28T21:20:50.193+0800 I STORAGE [initandlisten] wiredtiger_open config: create,cache_size=3584M,session_max=20000,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),statistics_log=(wait=0),verbose=(recovery_progress),
  13. 2018-01-28T21:20:50.540+0800 I STORAGE [initandlisten] WiredTiger message [1517145650:540642][10488:0x7fffd379a3c0], txn-recover: Main recovery loop: starting at 2/13056
  14. 2018-01-28T21:20:50.671+0800 I STORAGE [initandlisten] WiredTiger message [1517145650:671750][10488:0x7fffd379a3c0], txn-recover: Recovering log 2 through 3
  15. 2018-01-28T21:20:50.766+0800 I STORAGE [initandlisten] WiredTiger message [1517145650:766052][10488:0x7fffd379a3c0], txn-recover: Recovering log 3 through 3
  16. 2018-01-28T21:20:50.956+0800 I CONTROL [initandlisten]
  17. 2018-01-28T21:20:50.956+0800 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
  18. 2018-01-28T21:20:50.956+0800 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
  19. 2018-01-28T21:20:50.956+0800 I CONTROL [initandlisten]
  20. 2018-01-28T21:20:50.956+0800 I CONTROL [initandlisten] ** WARNING: This server is bound to localhost.
  21. 2018-01-28T21:20:50.956+0800 I CONTROL [initandlisten] ** Remote systems will be unable to connect to this server.
  22. 2018-01-28T21:20:50.956+0800 I CONTROL [initandlisten] ** Start the server with --bind_ip <address> to specify which IP
  23. 2018-01-28T21:20:50.956+0800 I CONTROL [initandlisten] ** addresses it should serve responses from, or with --bind_ip_all to
  24. 2018-01-28T21:20:50.956+0800 I CONTROL [initandlisten] ** bind to all interfaces. If this behavior is desired, start the
  25. 2018-01-28T21:20:50.956+0800 I CONTROL [initandlisten] ** server with --bind_ip 127.0.0.1 to disable this warning.
  26. 2018-01-28T21:20:50.956+0800 I CONTROL [initandlisten]
  27. 2018-01-28T21:20:50.956+0800 I CONTROL [initandlisten]
  28. 2018-01-28T21:20:50.956+0800 I CONTROL [initandlisten] ** WARNING: soft rlimits too low. Number of files is 256, should be at least 1000
  29. 2018-01-28T21:20:50.979+0800 I FTDC [initandlisten] Initializing full-time diagnostic data capture with directory '/Users/qufei/documents/mongodb/data/diagnostic.data'
  30. 2018-01-28T21:20:50.980+0800 I NETWORK [initandlisten] waiting for connections on port 27017
复制代码
打开mongodb客户端

打开本地控制台 输入命令mongo 连接到mongodb服务器

mongo

看到以下信息链接成功,会显示mongodb的版本号、链接地址,默认端口号为27017

  1. MongoDB shell version v3.6.2
  2. connecting to: mongodb://127.0.0.1:27017
  3. MongoDB server version: 3.6.2
  4. Server has startup warnings:
  5. 2018-01-28T20:26:16.102+0800 I CONTROL [initandlisten]
  6. 2018-01-28T20:26:16.102+0800 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
  7. 2018-01-28T20:26:16.102+0800 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
  8. 2018-01-28T20:26:16.102+0800 I CONTROL [initandlisten]
  9. 2018-01-28T20:26:16.102+0800 I CONTROL [initandlisten] ** WARNING: This server is bound to localhost.
  10. 2018-01-28T20:26:16.102+0800 I CONTROL [initandlisten] ** Remote systems will be unable to connect to this server.
  11. 2018-01-28T20:26:16.102+0800 I CONTROL [initandlisten] ** Start the server with --bind_ip <address> to specify which IP
  12. 2018-01-28T20:26:16.102+0800 I CONTROL [initandlisten] ** addresses it should serve responses from, or with --bind_ip_all to
  13. 2018-01-28T20:26:16.102+0800 I CONTROL [initandlisten] ** bind to all interfaces. If this behavior is desired, start the
  14. 2018-01-28T20:26:16.102+0800 I CONTROL [initandlisten] ** server with --bind_ip 127.0.0.1 to disable this warning.
  15. 2018-01-28T20:26:16.102+0800 I CONTROL [initandlisten]
  16. 2018-01-28T20:26:16.102+0800 I CONTROL [initandlisten]
  17. 2018-01-28T20:26:16.102+0800 I CONTROL [initandlisten] ** WARNING: soft rlimits too low. Number of files is 256, should be at least 1000
复制代码
显示所有数据库
  1. > show dbs
  2. admin 0.000GB
  3. config 0.000GB
  4. local 0.000GB
复制代码
查看当前所连接的数据库
  1. > db
  2. test
复制代码
查看当前数据库下的所有集合(数据表)
  1. > show collections
  2. demo_admin
复制代码
切换/创建数据库

切换数据库,使用use之前我们不需要对数据库进行额外的创建,在mongo中会在需要的时候自己创建。

  1. > use demo
  2. switched to db demo
复制代码
插入数据

向demo_admin表中插入数据,mongo中使用json格式以键值对的方式插入数据,会自动创建一个_id 字段,在全局范围内不会重复

  1. db.demo_admin.insert({name: 'Jack'})
复制代码
循环插入数据
  1. > for(i=0;i<10;i++)db.demo_admin.insert({x:i})
复制代码
查找数据 查找所有
  1. > db.demo_admin.find()
  2. { "_id" : ObjectId("5a6e82586757472b44d72122"), "name" : "Jack" }
  3. { "_id" : ObjectId("5a6e83206757472b44d72123"), "x" : 0 }
  4. { "_id" : ObjectId("5a6e83206757472b44d72124"), "x" : 1 }
  5. { "_id" : ObjectId("5a6e83206757472b44d72125"), "x" : 2 }
  6. { "_id" : ObjectId("5a6e83206757472b44d72126"), "x" : 3 }
  7. { "_id" : ObjectId("5a6e83206757472b44d72127"), "x" : 4 }
  8. { "_id" : ObjectId("5a6e83206757472b44d72128"), "x" : 5 }
  9. { "_id" : ObjectId("5a6e83206757472b44d72129"), "x" : 6 }
  10. { "_id" : ObjectId("5a6e83206757472b44d7212a"), "x" : 7 }
  11. { "_id" : ObjectId("5a6e83206757472b44d7212b"), "x" : 8 }
  12. { "_id" : ObjectId("5a6e83206757472b44d7212c"), "x" : 9 }
复制代码
指定条件查找
  1. > db.demo_admin.find({x: 0})
  2. { "_id" : ObjectId("5a6e83206757472b44d72123"), "x" : 0 }
复制代码
按条件或查找
  1. > db.demo_admin.find({'$or': [{x: 1}, {name: 'jack'}]})
  2. { "_id" : ObjectId("5a6e83206757472b44d72124"), "x" : 1 }
复制代码
使用count数据统计
  1. > db.demo_admin.find().count()
  2. 11
复制代码
其他查找方法

skip(3) 表示过滤掉前3条
limit(2) 显示2条结果
sort({x:1}) 使用x:1递增排序ASC,-1时递减排序DESC

  1. > db.demo_admin.find().skip(3).limit(2).sort({x:1});
  2. { "_id" : ObjectId("5a6e83206757472b44d72125"), "x" : 2 }
  3. { "_id" : ObjectId("5a6e83206757472b44d72126"), "x" : 3 }
复制代码
更新update

db.conllections.update(a,b,c,d);

a代表修改条件

b代表的是新的数据内容

将x=1的更新为x=111

  1. > db.demo_admin.update({"x":1},{"x":111})
  2. WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
复制代码
c 值,bool,作用是如果这条数据中没有我们修改的这么一条原始数据,是否插入数据库 默认false,表示不插入
  1. > db.demo_admin.update({"y":999},{"y":9999},true)
  2. WriteResult({
  3. "nMatched" : 0,
  4. "nUpserted" : 1,
  5. "nModified" : 0,
  6. "_id" : ObjectId("5a6e85a36c2de84d2e5a69d6")
  7. })
复制代码

如果设置为c第三个参数设置为false,则不会插入数据,看WriteResult返回结果遇上面不同

  1. > db.demo_admin.update({"y":999},{"y":9999888}, false)
  2. WriteResult({ "nMatched" : 0, "nUpserted" : 0, "nModified" : 0 })
复制代码
d作用是同样条件的数据,只修改一条还是修改所有 默认false
  1. db.demo_admin.update({"a":2},{$set:{"a":222}},false,true);
  2. db.demo_admin.update({"x":100},{$set:{"y":99}})
复制代码
删除 remove删除一条数据
  1. > db.demo_admin.remove({a:222})
  2. WriteResult({ "nRemoved" : 1 })
复制代码
drop删除一张表

返回true删除成功, false删除失败

  1. db.demo_admin.drop()
  2. true
复制代码

Github地址 https://github.com/Q-Angelo/Summ ... se/mongo_install.md



回复

使用道具 举报