【发布时间】:2016-12-11 21:39:25
【问题描述】:
我有多个文档在每个文档中都有一个长字符串,我想一次检索一个文档,我在文档中除了长字符串之外没有任何内容,我该如何检索?
我使用 insertMany() 将所有文档插入到集合中,这是我检索所有文档时的代码和输出
var schema = new mongoose.Schema({
question : String,
id: Number
})
var quizz = mongoose.model('Quiz', schema );
var firstDoc = new quizz({
question: 'question 1',
id: 1
})
var secondDoc = new quizz({
question: 'question 2',
id: 2
var question_data = [firstDoc, secondDoc];
quizz.insertMany(question_data, function(err, res){
if(err){
console.log("error occured while saving document object " + err )
}else{
console.log("saved data");
}
})
quizz.findOne({id : '1'}, function(err, res){
if(err){
console.log(err)
}else{
console.log(res);
}
})
【问题讨论】:
-
quizz.findOne(function(err, data) { }) -
我已经编辑了问题
标签: node.js mongodb express mongoose mongoose-schema