4
這是/models/joke.js
:如何在Node中(使用Mongoose)訪問Mongo中的`findAndModify`方法?
var mongoose = require ('mongoose')
, database = mongoose.connect('localhost', 'joke', { server: { poolSize: 3 } });
var jokeSchema = mongoose.Schema({
content: String,
upvotes: {
type: Number,
default: 0
},
downvotes: {
type: Number,
default: 0
},
views: {
type: Number,
default: 0
},
published: {
type: Boolean,
default: true
},
author_id: Number,
created: {
type: Date,
default: Date.now
}
});
var Joke = mongoose.model('Joke', jokeSchema);
module.exports = Joke;
而且我這樣做是爲了檢查是否存在的東西 - 如果沒有,那麼創建:
var Joke = require ('./models/joke');
// ...
Joke.findAndModify({
query: {
content: content
},
update: {
$setOnInsert: {
content: "test",
}
},
new: true,
upsert: true
});
但我的控制檯喊我下面的:
TypeError: Object function model(doc, fields, skipId) {
if (!(this instanceof model))
return new model(doc, fields, skipId);
Model.call(this, doc, fields, skipId);
} has no method 'findAndModify'
我可以理解錯誤的原因 - I'm calling it through a model instead of a collection,但我如何訪問我的笑話的收集方法?
我的意思是,所有的例子都使用db.collection.findAndModify
,但這是什麼db.collection
?我如何稱呼它?