I'd like an environment where I can get a solid grasp of how CRUD operations work. Up to this point, I've been using views
to see how the data looks like but with the obvious inflexibility, this approach isn't that insightful -- it's something like driving in the dark.
Now I want to be able to work with the data in MongoDB via the functionality offered by Mongoose so that I see what actually happens when I do this or that, subdocuments, population, that sort of stuff. Initially, I had this code in a script.js
file:
var mongoose = require('mongoose');
// ...
// define schemas
// try some crazy things
// ...
console.log(results);
And then I attempted to execute that code:
node script.js
Which didn't work. I then tried:
mongo load('script.js')
This time, I got an error, of course -- require()
is not defined. Facepalm.
Hopefully that gives you an idea of what I'm trying to do. I've since tried other methods that didn't seem to work.
Please advise.
UPDATE
And here's my script.js
file:
var Hero = require('./hero-model');
// This hero-model.js file defines the schema
// and exports its functionality. I've used
// this model to successfully CRUD data
// via Express. You can ignore the code above
// where I stated require('mongoose') because I was
// merely simplifying. Running this script causes
// the command to terminate silently.
var getHeroes = function() {
Hero.find(function(err, heroes) {
if (err) {
console.log(err);
}
console.log(heroes);
});
};
getHeroes();