-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathMongoDB Notes.txt
More file actions
108 lines (70 loc) · 4.34 KB
/
MongoDB Notes.txt
File metadata and controls
108 lines (70 loc) · 4.34 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
MongoDB
db.collectionName.save({key:value}); // saves something to a collection
db.collectionName.find(); // finds all data in that collection
db.collectionName.find({key:value}); // query for a key:value hash in a collection. You
// can even search for multiple key:value pairs.
The value can me a key:value hash in itself using one of the query operators, i.e. {$lt:10}
it // shows more of a collection??
You can put this code in say a for-loop to save a whole bunch of records to a collection.
========================
Chyld Mongo Lesson:
========================
There is a server and a client part. Mongod is the server part of Mongo. That does all the work of MongoDB. The mongo command starts up the client part of it. That is where you issue your commands that interact with the server part of mongo (mongod).
My databases are saved in /Data/DB/ - that is where I set up Mongo to save stuff.
Can also use Robomongo instead of using mongo on the command line.
ps -ef | grep mongo <-- to check if mongo is running
in Mongo client (after typed mongo command):
To stop Mongod:
use admin - switches to admin database (admin database is hidden)
db.shutdownServer() - shuts down mongod, client will still be running, but it is dead
exit - exits the mongo client
show dbs - shows all your databases in your local mongoDB
show collections - shows all the collections in your current database
use databaseName - won't show the database in "show dbs" until you actually save a record
db.collectionName.save({}) - save the object you pass into the collection (creates
collection if it doesn't exist)
When you save MongoDB will create a unique id in the _id key and saves it as ObjectID("some id string").
db.commandHelp() - to see db commands
db.collectionName.find() - to see all the records in the collection
db.collectionName.remove({}) - removes all data from the collection
db.collectionName.drop() - deletes the entire collection itself
db.dropDatabase() - deletes the entire database that you are in
========================
MongoDB is a document-based database. It is part of the whole NoSQL movement of using something other than SQL-based relational databases to store data.
MongoDB uses JSON objects, which it calls documents, to store data. You save these documents in databases, called collections.
Save a document:
db.collectionName.save(theDocument);
To view, or query, a collection:
db.collectionName.find();
i.e.
db.blah.find({ name: “todd”, age: 31 });
db.blah.find(someJSONobject);
db.blah.find({a: {‘$gt’: 15}}) // queries all documents where a > 15
There are many special query operators, like these:
{a: {$lt: 5}} // less than
{a: {$gte: 10}} // greater than or equal to
{a: {$ne: ‘b’}} // not equal to
{a: {$in: [‘a’, ‘b’, ‘c’]}} // exists in array
Create documents:
db.collection.insert(theDoc);
Update/Change a document:
// the {key: value} is the query for finding records to update
db.collection.update({key: value}, {updated info for document});
Apparently the update method can sometimes delete an entire document. That’s no good! To avoid this you can use update operators to only modify parts of documents.
Update Operators in use:
// Push an element into an array (the key/field must be an array), and doesn't allow duplicates:
db.collection.update({key: value}, { $addToSet: {key: value}});
// Updates an existing field, or adds a new field to a document:
db.collection.update({key: value}, { $set: {key: value}});
// Delete fields from a single record (the first matched record), the values of the unset fields doesn't matter, so just did '' here:
db.collection.update({key: value}, {$unset: {key: '', key2: ''}});
// Delete fields from all records in the collection by adding the {multi: true} property:
db.collection.update({key: value}, {$unset: {key: ''}, {multi: true}});
// Push items to arrays:
db.collection.update({key: value}, { $push: {key: value}});
// Pull items from arrays:
db.collection.update({key: value}, { $pull: {key: value}});
Delete everything from a collection:
db.collection.remove({});
Example of checking if a document has a property and sorting:
db.meal.find({ $query: {isCloned: { $exists: false }, isPublic: true}, $orderby: { createdAt: 1}})