我想要一个可以牢固掌握 CRUD 操作如何工作的环境。到目前为止,我一直在使用views
看看数据是什么样子,但由于明显缺乏灵活性,这种方法并不是那么有洞察力——这就像在黑暗中开车一样。
现在我希望能够通过 Mongoose 提供的功能来处理 MongoDB 中的数据,这样我就可以看到当我这样做或那样做时实际发生了什么,子文档、人口等等。最初,我将这段代码放在script.js
file:
var mongoose = require('mongoose');
// ...
// define schemas
// try some crazy things
// ...
console.log(results);
然后我尝试执行该代码:
node script.js
这不起作用。然后我尝试:
mongo load('script.js')
当然,这一次我犯了一个错误——require()
没有定义。捂脸。
希望这能让您了解我正在尝试做什么。此后我尝试了其他似乎不起作用的方法。
请指教。
UPDATE
这是我的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();
您正在尝试将节点脚本作为 mongo shell 脚本运行。要将其作为常规节点脚本运行,只需使用:
node script.js
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)