美文网首页
node.js连接mongodb数据库之总结一

node.js连接mongodb数据库之总结一

作者: fangcaiwen | 来源:发表于2018-09-29 16:35 被阅读0次

话不多说,直接核心:
第一步:下载mongodb数据库,打开终端

终端输入:brew install mongodb
Updating Homebrew...
==> Auto-updated Homebrew!
Updated 1 tap (homebrew/core).
==> New Formulae
diceware                                                  llvm@6
==> Updated Formulae
nginx ✔                                git-sizer                              parallel
node ✔                                 gjs                                    pass
sqlite ✔                               glib                                   pdftoedn
alexjs                                 glide                                  pdftoipe
angular-cli                            gocr                                   pgrouting
anjuta                                 grakn                                  pixz
apache-flink                           grpc                                   pmd
app-engine-java                        grunt-completion                       poppler
arangodb                               gtk-mac-integration                    postgis
aria2                                  help2man                               pre-commit
ark                                    httpd                                  prometheus
arp-scan                               hugo                                   protobuf-swift
arpack                                 hwloc                                  pyinvoke
avrdude                                iamy                                   pyside
avro-c                                 ice                                    qcachegrind
aws-shell                              imagemagick                            quicktype
awscli                                 imagemagick@6                          rtags
azure-cli                              immortal                               rust
bat                                    ipython                                saxon
bazel                                  jenkins                                scala
bind                                   jerasure                               skaffold
black                                  jmeter                                 skafos
cargo-completion                       kerl                                   smlnj
cash-cli                               kibana                                 solr
chakra                                 kibana@5.6                             sops
chapel                                 kotlin                                 sourcery
circleci                               kubernetes-cli                         sphinx-doc
clang-format                           kubernetes-helm                        spotbugs
coffeescript                           kubernetes-service-catalog-client      sqldiff
cromwell                               kumo                                   sqlite-analyzer
crowdin                                kustomize                              stellar-core
crystal                                languagetool                           stunnel
dbhash                                 libcouchbase                           swiftgen
diff-pdf                               libdazzle                              tcpflow
diffoscope                             libetpan                               telegraf
digdag                                 libgda                                 terragrunt
docker-machine-nfs                     libopkele                              tgui
dub                                    libsoup                                tomcat@7
duck                                   libuv                                  tomcat@8
elasticsearch                          libwbxml                               twarc
elasticsearch@5.6                      llnode                                 typescript
erlang                                 llvm                                   unp64
erlang@19                              logstash                               urh
eslint                                 luaradio                               vala
exiftool                               lynis                                  vegeta
faas-cli                               mame                                   vnu
file-roller                            mariadb@10.2                           vorbis-tools
fmsx                                   mbedtls                                vte3
fn                                     mdds                                   wdiff
folly                                  megatools                              weaver
fossil                                 mosquitto                              webpack
freetds                                mujs                                   wireguard-tools
frugal                                 neko                                   wtf
futhark                                nifi-registry                          xonsh
fzy                                    nim                                    yarn
gdb                                    opam                                   yelp-tools
geckodriver                            ortp                                   youtube-dl
gedit                                  paket                                  zig
git-archive-all                        pandoc-crossref
==> Deleted Formulae
redis@2.8                                                 submarine

==> Installing dependencies for mongodb: sqlite
==> Installing mongodb dependency: sqlite
==> Downloading https://homebrew.bintray.com/bottles/sqlite-3.25.2.high_sierra.bottle.tar.gz
######################################################################## 100.0%
==> Pouring sqlite-3.25.2.high_sierra.bottle.tar.gz
==> Caveats
sqlite is keg-only, which means it was not symlinked into /usr/local,
because macOS provides an older sqlite3.

If you need to have sqlite first in your PATH run:
  echo 'export PATH="/usr/local/opt/sqlite/bin:$PATH"' >> ~/.bash_profile

For compilers to find sqlite you may need to set:
  export LDFLAGS="-L/usr/local/opt/sqlite/lib"
  export CPPFLAGS="-I/usr/local/opt/sqlite/include"

==> Summary
🍺  /usr/local/Cellar/sqlite/3.25.2: 11 files, 3.7MB
==> Installing mongodb
==> Downloading https://homebrew.bintray.com/bottles/mongodb-4.0.2.high_sierra.bottle.tar.gz
######################################################################## 100.0%
==> Pouring mongodb-4.0.2.high_sierra.bottle.tar.gz
==> Caveats
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
==> Summary
🍺  /usr/local/Cellar/mongodb/4.0.2: 18 files, 256.6MB
==> Caveats
==> sqlite
sqlite is keg-only, which means it was not symlinked into /usr/local,
because macOS provides an older sqlite3.

If you need to have sqlite first in your PATH run:
  echo 'export PATH="/usr/local/opt/sqlite/bin:$PATH"' >> ~/.bash_profile

For compilers to find sqlite you may need to set:
  export LDFLAGS="-L/usr/local/opt/sqlite/lib"
  export CPPFLAGS="-I/usr/local/opt/sqlite/include"

==> 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

第二步:启动数据库服务:

终端输入:brew services start mongodb
==> Tapping homebrew/services
Cloning into '/usr/local/Homebrew/Library/Taps/homebrew/homebrew-services'...
remote: Enumerating objects: 14, done.
remote: Counting objects: 100% (14/14), done.
remote: Compressing objects: 100% (10/10), done.
remote: Total 14 (delta 0), reused 8 (delta 0), pack-reused 0
Unpacking objects: 100% (14/14), done.
Tapped 1 command (43 files, 55.7KB).
==> Successfully started `mongodb` (label: homebrew.mxcl.mongodb)

第三步:进入数据库:

终端输入:mongo
MongoDB shell version v4.0.2
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 4.0.2
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: 
2018-09-29T16:07:00.426+0800 I CONTROL  [initandlisten] 
2018-09-29T16:07:00.426+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-09-29T16:07:00.426+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2018-09-29T16:07:00.426+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()
---

展示所有数据库

终端输入:show dbs
admin   0.000GB
config  0.000GB
local   0.000GB

创建数据库:

终端输入:use shop
switched to db shop //没有数据库会自动创建

此时运行

> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB

发现依然没有,怎么办?创建的数据库(newdb)不在列表中。要显示数据库,需要至少插入一个文档,空的数据库是不显示出来的。

> db.shop.insert({"name":"wind"})
WriteResult({ "nInserted" : 1 })

再次

show dbs
admin   0.000GB
config  0.000GB
local   0.000GB
shop    0.000GB

到此一个数据从下载到建库全部完成了。

相关文章

网友评论

      本文标题:node.js连接mongodb数据库之总结一

      本文链接:https://www.haomeiwen.com/subject/agkboftx.html