MongoDB Notes

Amalia Bryant
1 min readJul 19, 2021

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;

--

--

Amalia Bryant
0 Followers

Freedom lover and life long student.