MongoDB Notes
- https://www.mongodb.com/basics/get-started
- Documentation: https://mongoosejs.com/
- ‘mongo’ starts mongo shell in terminal
- ‘show dbs’ shows existing databases
- ‘use some_db_name’ opens db or creates one if not exisit
- ‘show collections’ see the collections in db
- ‘db.collection.find(some_query)’
You can create a mongodb in a js file (there are some depreciations so you will get some warnings in the console/terminal however they will not impact the functionality at this time):
const mongoose = require(‘mongoose’);
mongoose.connect(‘mongodb://localhost/someDBName’);
//When making a schema you can use various data types like String, Number, etc. Remember that the _id key assigns a unique identifier to a document. However if you do not assign it, mongo will create one for you.
let someSchema = mongoose.Schema({
_id: String,
someKey: String,
someOtherKey: Number
});
let someModel = mongoose.model(‘SomeModel’, someSchema);
let someFunction = (someData, cb) => {
let document = new someModel({
_id: someData.id,
someKey: someData.key,
someOtherKey: someData.otherKey
})
//you can insert an array of documents with insertMany or one at a time with insert
someModel.collection.save(document, (err, docs) => {
if(err) {
cb(err);
} else {
cb(null, `Successfully added ${docs.result.n} repos`);
}
});
}
module.exports.someFunction = someFunction;