简体   繁体   English

mongodb如何查找多个文档

[英]mongodb how to find Multiple Documents

in mongodb how can i find multiple documents at ones?? 在mongodb中,我怎么能找到多个文档?

in my post method i have appoinments collection.when getting post data i want check they already in database using hour_responce and date_responce but how can i do with 2 documents.its working fine with only one docement. 在我的发布方法中,我有appoinments集合。当获取发布数据时,我想使用hour_responcedate_responce检查它们是否已经在数据库中,但是我该如何处理2个document.its仅使用一个文档就可以了。

app.post('/collections/:collectionName', function(req, res, next) {
   console.log(req.body[0])
   if(req.params.collectionName == 'appoinments'){
      req.collection.findOne({date_responce:req.body[0].date_responce,hour_responce:req.body[0].hour_responce}, function(e, result){
          if(result){
            console.log(result); console.log(e)
            res.send(500,{error:"You Already have a Task on this Time Period"})
          }
          else{
            req.collection.insert(req.body, {}, function(e, results){
            if (e) return next(e)
            res.send(results)

             })
          }
      })
  }
  else{
     req.collection.insert(req.body, {}, function(e, results){
            if (e) return next(e)
            res.send(results)
        })
  }

})

---UPDATE---- -更新----

{ toArray: [Function],
  each: [Function],
  next: [Function],
  nextObject: [Function],
  setReadPreference: [Function],
  batchSize: [Function],
  count: [Function],
  stream: [Function],
  close: [Function],
  explain: [Function],
  isClosed: [Function],
  rewind: [Function],
  limit: [Function],
  skip: [Function],
  hint: [Function],
  maxTimeMS: [Function],
  sort: [Function],
  fields: [Function] }

You are using findOne method of your collection. 您正在使用集合的findOne方法。

You can try with : find() method, and then iterate on your resultset to make your comparisons 您可以尝试使用:find()方法,然后对结果集进行迭代以进行比较

EDIT : 编辑:

req.collection.find({}).toArray(function(e,result){

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM