【问题标题】:Node.js find() in Mongoose model definitionMongoose 模型定义中的 Node.js find()
【发布时间】:2017-01-15 18:14:39
【问题描述】:

我想在我的模型上定义一个涉及搜索同一模型的文档的方法,这是我尝试过的:

var mongoose = require('mongoose');
var Author = require('./author.js'); 

var bookSchema = mongoose.Schema({
    author : { type: mongoose.Schema.Types.ObjectId, ref: 'author' },
    genre: String,
});

bookSchema.methods.findSimilar = function(callback) {
    bookSchema.find({'genre': this.genre}).exec(function doThings(err, doc){ 
        /* ... */ 
    }); 
}; 
module.exports = mongoose.model('book', bookSchema, 'book');

但是,我得到了TypeError: bookSchema.find is not a function

我也试过bookSchema.methods.find(),结果一样。我怎样才能解决这个问题?

谢谢,

编辑: 受此answer 的启发,我也尝试了this.model('Book').find(),但我得到了类似的错误:TypeError: this.model is not a function

【问题讨论】:

标签: javascript node.js mongodb


【解决方案1】:

将您的方法更改为: (我假设您已经从架构模块中导出了模型 Book

bookSchema.methods.findSimilar = function(callback) {

    this.model('Book').find({'genre': this.genre}).exec(function doThings(err, doc){ 
        /* ... */ 

    }); 

    // Or if Book model is exported in the same module
    // this will work too:
    // Book.find({'genre': this.genre}).exec(function doThings(err, doc){ 
    //     /* ... */ 
    //    
    // }); 
};

该方法将在您的模型实例上可用:

var book = new Book({ author: author_id, genre: 'some_genre' });
// Or you could use a book document retrieved from database

book.findSimilarTypes(function(err, books) {
    console.log(books);
});

documentation here

编辑 (完整架构/模型代码)

完整的架构/模型代码如下:

var mongoose = require('mongoose');
var Author = require('./author.js'); 

var BookSchema = mongoose.Schema({
    author : { type: Schema.Types.ObjectId, ref: 'Author' },
    genre: String,
});

BookSchema.methods.findSimilar = function(callback) {
    Book.find({genre: this.genre}).exec(function doThings(err, doc){ 
        /* ... */ 
    }); 
}; 

const Book = module.exports = mongoose.model('Book', BookSchema);

使用示例:

var book = new Book({ author: author_id, genre: 'some_genre' });
// Or you could use a book document retrieved from database

book.findSimilarTypes(function(err, books) {
    console.log(books);
});

【讨论】:

  • 您是否为此架构创建了模型?如果您从同一个模块(名为 Book)导出了此架构的模型,则可以使用 Book.find({'genre': this.genre}).exec(function doThings(err, doc) { // ... // })
  • 我认为这个文件是模型 :) 在其他文件中,我将其包含在 var Book = require('./models/thisfile'); 中,然后执行 Book.find(..)。我可以在外部文件(如控制器)上定义我的方法,但我只是想知道是否可以将其作为模型的一部分。 编辑: 我忘记了我在module.exports = ... 的最后一行。但这令人困惑。我应该在导出模型之前还是之后定义函数?现在看起来都不合乎逻辑:D
  • 我对您的代码中的 PascalCasing 和 CamelCasing 感到困惑。通常我们对模式和模型使用 PascalCasing,对实例使用 camelCasing。请参阅我上面的编辑。我已经包含了您的架构/模型模块应该如何的完整代码。希望这会有所帮助。
  • 非常感谢!这仍然让我感到困惑,findSimilar 中的Book 是在函数定义之后定义的。它是如何工作的? const 是做什么的?再次,非常感谢。
  • 啊,好吧...您正在执行 Book.find() ,这将返回文档数组。所以你必须调用doc[0].save()。您还有另一种使用 Book.findOne() 的选项,它返回单个文档,在这种情况下,您可以调用 doc.save()
【解决方案2】:

你忘了写新的关键字,所以这样做:

var bookSchema = new mongoose.Schema({
    author : { type: mongoose.Schema.Types.ObjectId, ref: 'author' },
    genre: String,
});

干杯:)

【讨论】:

  • 奇怪,我定义的所有模型都没有new,一切正常。我想知道是否有区别。 P.S. 添加它并没有解决问题。
猜你喜欢
  • 2020-12-21
  • 2019-08-16
  • 2019-03-18
  • 1970-01-01
  • 2011-11-17
  • 1970-01-01
  • 1970-01-01
  • 2018-01-18
  • 1970-01-01
相关资源
最近更新 更多