show dbs
Show databasesuse dbName
Connect/Select databaseuse dbName
Create database (MongoDB doesn’t actually create a db until we start storing documents, so although you now have the db bookmarks, it’s not actually saved anywhere until we put some data in it. Same goes for collectons )db
show the db you are usingdoc
show the document contentMongoDB | MySQL |
---|---|
Collections | Tables |
Documents | Rows |
Fields (key:value pairs) | Columns |
There are two methods. One uses .insert()
1db.links.insert({ title: "Courses", url: "http://tutsplus.com", comment: "advanced programming video courses", tag: ["tutorials", "dev "], saved_on: new Date() });
and the other us saving an empty document and then adding data to it. e.g:
1var doc = {};
2doc.title = '';
3doc.url = '';
4doc.comment = '';
5doc.tags = '';
6doc.saved_on = new Date;
adding sub-objects inside of objects
1doc.meta = {}
2doc.meta.browser = 'Google Chrome 31'
3doc.meta.OS = 'Mac OS 10.9.2'
db.links.save(doc)
save all data in the doc collection to the links databasedb.links.update()
.insert()
add data.count()
show the number of collections/documents.date()
shows the date the data was entered.find()
find with no parameters given will just output all data, e.g: - db.links.find()
.save()
.update()
.forEach(printjson);
print documents nicely formatted e.g: db.links.find()- .forEach(printjson);
.getTimestap()
use this method on an _id to get the time the object was created.This _id
or ObjectId object field is immutable and unique. You can’t have two documents with the same _id