-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGetting_Started_MongoDb
128 lines (64 loc) · 2.88 KB
/
Getting_Started_MongoDb
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
----
--Details can be found on this website
http://docs.mongodb.org/manual/tutorial/getting-started/
----
--1. To start mongod shell in order to interact with the Db, issue the following word from the terminal
--It's like psql for postgresql
mongo
--2. The above command is assuming that the Db server is listening on port 27017 and it's a localhost
--To connect to a different port or host/interface use the following command (although havn't tested yet this command)
mongo --host 160.75.133.175 --port 1111
--3. To see the current Db name
db
--4. To see the list of Dbs (this command will not show the temporary Dbs. But once some data is inside that newly created
temporary db (created using the command 'use newDb'), you can see its name under the 'show dbs' command)
show dbs
OR
show databases
--5. To create a new Db and enter into it
use newDb
--6. Help
help
----
Insert Operations
----
--7. Inside the Db we create a COLLECTION (equivalent to a TABLE) and insert our DOCUMENTS (equivalent to the ROWS) into
--those COLLECTIONS. MongoDB will create a COLLECTION implicitly upon its first use. You do not need to create a COLLECTION
--before inserting data. Furthermore, because MongoDB uses dynamic schemas, you also need not specify the structure of your
--DOCUMENTS before inserting them into the COLLECTION.
--8. Create the documents using JS approach
j = { name : "mongo" }
k = { x : 3 }
--9. Insert the documents into the collections (When you insert the first document, the mongod will create both the newDb
--database and the testData collection.)
db.testData.insert( j )
db.testData.insert( k )
--10. To see the list of collections in the current Db
show collections
--11. To check the all the existing DOCUMENTS in a COLLECTION
db.testData.find()
--12. To insert the DOCUMENTS into the COLLECTION using the JS FOR LOOP
for (var i = 1; i <= 25; i++) {
db.testData.insert( { x : i } )
}
--13. Create a JS function to feed the DOCUMENTS (if the Db and COLLECTION don't exist, MongoDb will create one for you
--before feeding the DOCUMENTS). You can save these kinda JS functions into a file named 'mongorc.js' and place it in
--the /etc/ dir (function will be globally available this way). This way those functions will be available everytime
--the MongoDb gets start-up. The 'mongorc.js' must have the read permissions for the user running the Db.
function insertData(dbName, colName, num) {
var col = db.getSiblingDB(dbName).getCollection(colName);
for (i = 0; i < num; i++) {
col.insert({x:i});
}
print(col.count());
}
--To run the above function
insertData('newDb', 'testData', 10)
--15. To see the count of the collection
db.testData.count()
----
--SQL to MongoDB Mapping Chart ***Very Important Link***
http://docs.mongodb.org/manual/reference/sql-comparison/
----
--Getting Started Link
http://docs.mongodb.org/manual/tutorial/getting-started/