Node.js 如何在mongoose中检索文档

Node.js 如何在mongoose中检索文档,node.js,mongodb,express,mongoose,mongoose-schema,Node.js,Mongodb,Express,Mongoose,Mongoose Schema,我在集合中有多个文档,每个文档中都有一个长字符串,我想一次检索一个文档,除了长字符串之外,我在文档中没有任何内容,如何检索 我使用insertMany()插入了集合中的所有文档,这是我检索所有文档时的代码和输出 var schema = new mongoose.Schema({ question : String, id: Number }) var quizz = mongoose.model('Quiz', schema ); var firstDoc = new quizz({ qu

我在集合中有多个文档,每个文档中都有一个长字符串,我想一次检索一个文档,除了长字符串之外,我在文档中没有任何内容,如何检索

我使用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);     
}
})

insertMany
将返回为您插入的文档创建的
\u id
列表。然后,您可以根据
\u id
分别取出每个文档

quizz.insertMany(question_data, function(err, res){
  if(err){
   console.log("error occured while saving document object " + err )
 }else{
   console.dir(res); // res has the _ids.
   console.log("saved data");
 }
})


或者,如果您始终希望确保排序,您可以在问题中添加一个序列列,和/或将所有问题放在一个测验中。

insertMany
将返回为您插入的文档创建的
\u id
列表。然后,您可以根据
\u id
分别取出每个文档

quizz.insertMany(question_data, function(err, res){
  if(err){
   console.log("error occured while saving document object " + err )
 }else{
   console.dir(res); // res has the _ids.
   console.log("saved data");
 }
})


或者,如果您总是想确保排序,您可以在问题中添加一个序列列,和/或将所有问题放在一个测验中。

如果您想使用插入到集合中的文档的_id做一些事情,请使用Kevin的答案,但如果您想稍后使用它们做一些事情,您可以使用
.find()
返回集合中的所有文档

quizz.find(function(err, docs) {
  //docs = array of all the docs in the collections
})
如果您想按id指定:

quizz.findOne({_id: id},function(err, doc) {
  //doc = the specific doc
})
如果你想通过强大的

quizz.findOne({question: "question 3"},function(err, doc) {
  //doc = the first (!!!) doc that have question in his `question` attribute 
})
或者,如果您想要所有包含问题3的文档:

quizz.find({question: "question 3"},function(err, docs) {
  //docs = array with all the docs that have "question 3" there, (return array even if only 1 found) 
})

如果您想对插入到集合中的文档的_id执行某些操作,请使用Kevin的答案,但如果您想稍后对它们执行某些操作,则可以使用
.find()
返回集合中的所有文档

quizz.find(function(err, docs) {
  //docs = array of all the docs in the collections
})
如果您想按id指定:

quizz.findOne({_id: id},function(err, doc) {
  //doc = the specific doc
})
如果你想通过强大的

quizz.findOne({question: "question 3"},function(err, doc) {
  //doc = the first (!!!) doc that have question in his `question` attribute 
})
或者,如果您想要所有包含问题3的文档:

quizz.find({question: "question 3"},function(err, docs) {
  //docs = array with all the docs that have "question 3" there, (return array even if only 1 found) 
})


quizz.findOne(function(err,data){})
我编辑了问题
quizz.findOne(function(err,data){})
我编辑了问题如果我们要检索集合中的单个文档怎么办?我问这个问题是因为除了保存的字符串之外,我在文档中没有任何不同的东西,字符串很长,我不能写下整个问题,并说如果这与ant文档匹配,就把它给我,还有mongoose自己分配的ID,这也是很长的一段时间,我想你只需要在每个文档上有一个序列,然后使用下面的
quizz.findOne({sequence:1},function(err,doc){})查询它
如果我这样做,我会得到空值。您需要先为每个文档设置
序列
字段。而不是序列,我添加了id:)输出正确的值如果我们要检索集合中的单个文档怎么办?我问这个问题是因为除了保存的字符串之外,我在文档中没有任何不同的东西,字符串很长,我不能写下整个问题,并说如果这与ant文档匹配,就把它给我,还有mongoose自己分配的ID,这也是很长的一段时间,我想你只需要在每个文档上有一个序列,然后使用下面的
quizz.findOne({sequence:1},function(err,doc){})查询它
如果我这样做,我会得到空值,您需要首先为每个文档设置
序列
字段。我添加了id:)而不是序列输出正确的值是的,我将id作为新属性添加到架构中,并最终添加到文档中,因此,我可以识别每个文档。您应该编辑问题,准确地告诉我们您想要实现的目标,因为您不需要在文档中添加id,因为mongoose在保存文档(没有id)时会立即处理它。如果我添加新的属性id并使用findOne(id:number)检索,我会得到它的本地id,本地id不是Mongoose分配的您在
err
中得到了什么?是的,我将id作为新属性添加到模式中,并最终添加到文档中,以便我可以识别每个文档。您应该编辑问题,以准确地告诉我们您想要实现什么,因为您不需要在文档中添加id,因为mongoose会在保存文档(没有id)时立即处理它。如果我添加新属性id并使用findOne(id:number)检索,我会获取null其本地id,而不是mongoose分配的本地id。您在
err
中获得了什么?