-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongodb setup
61 lines (47 loc) · 2.39 KB
/
mongodb setup
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
1. Open a CLI window and type the following
cd /blah/blah/node_api<enter>
mkdir data<enter>
mongod --dbpath /blah/blah/node_api<enter>
2. A bunch of text should appear, you will see "I NETWORK [initandlisten]" alot;
3. Open a new CLI window and type: mongo<enter>
4. Hopefully you will see "MongoDB shell version: 3.0.1 connecting to: test" If not, go to the root folder of your OS and make a directory called "data" and then repeat from step 3. above
5. In the second "mongo<enter>" CLI window type: use node_api*
6. You should see "switched to db node_api"*
7. Open server.js and change the "mongoose.connect..." line to the following:
mongoose.connect('mongodb://localhost/node_api');*
8. In a third [new?] CLI window ctrl+C (if necessary), cd to blah/blah/node_api* (if necessary) and then run "node server.js" again.
You should now have a mongo db located in node_api/data and be connecting to it via server.js
* substitute "node_api" for whatever the name of your project folder is.
/*======= command ===========*/
mongo -> start mongo cli
show dbs -> Show database
use #db ->
show collections -> Show all collections (or table)
db.#collectionName.find() -> view everything from this collection
e.g. db.#collectionName.find({"username":"two"}).pretty()
db.#collectionName.upate() -> update a key value of this collection
e.g. db.#collectionName.update({"username":"two"}, {$set:{"table":[]}} )
db.#collectionName.remove() -> delete all collections or single collection
e.g. db.#collectioName.remove({"username":"two"}, {justOne: true})
/*========= backup and restore ==========*/
// checkout https://www.youtube.com/watch?v=2RApNiXsd-g
//backup (in normal command, not mongo)
mongodump --out #directoryPath
//to specify dbs path
mongodump --db #dbName --collections #collectionName --out #directoryPath
//restore (in normal command, not mongo)
mongorestore #directoryPath
OR
mongorestore --db #database #directoryPath
use mongoimport to import json data
e.g.
mongoimport -d simpleexpress2 C:\Users\sydne_000\Documents\site\test\node-oauth2-server-implementation\mongo-dump\users.json
/*================= Delete DB ==============*/
> use mydb;
> db.dropDatabase()
/*======== mongo-express ===========*/
//https://github.com/mongo-express/mongo-express
install:npm install -g mongo-express
run: mongo-express -u admin -p pass -d #database
access: http://localhost:8081
/*======== mongo-lab ===========*/