node.js - 创建使用 Mongoose 更新和保存文档的方法?

标签 node.js mongodb methods express mongoose

查看 official documentation 后,我仍然不确定如何在 mongoose 中创建用于创建和更新文档的方法。

那么我该怎么做呢?

我有这样的想法:

mySchema.statics.insertSomething = function insertSomething () {
    return this.insert(() ?
}

最佳答案

在静态方法中,您还可以通过以下方式创建新文档:

schema.statics.createUser = function(callback) {
  var user = new this();
  user.phone_number = "jgkdlajgkldas";
  user.save(callback);
};

关于node.js - 创建使用 Mongoose 更新和保存文档的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8987851/

相关文章:

node.js - 从 Node API 加载 Web Audio API 中的音频

python - 在 python 中从类调用函数 - 不同的方式

c# - 在 asp.net 中验证 .cs 文件中的方法

node.js - 有哪些常见的开发问题、陷阱和建议?

javascript - Node js中带有svg图形的月相

sql - SQLite3 在 Node.js 中有准备好的语句吗?

node.js - 填充 ('model' 时出现 Mongoose 错误不是函数)

javascript - "EventEmitter memory leak detected"错误导致崩溃

php - 从使用 ISODate 保存的 mongodb 数据库获取时间戳

c# - 如何选择从泛型方法调用的正确的 C# 重载扩展方法