繁体   English   中英

查询/查找具有一系列多个条件的MongoDB文档

[英]Query/Find MongoDB documents with series of multiple conditions

我有一个用户架构,其中的基本字段包括兴趣,位置坐标,我需要使用特定的UserId来执行POST请求以获得结果

  app.post('api/users/search/:id',function(err,docs) { //find all the documents whose search is enabled. //on documents returned in above find the documents who have atleast 3 common interests(req.body.interests) with the user with ':id' // -----OR----- //find the documents who stay within 'req.body.distance' compared to location of user with':id' //Something like this return User .find({isBuddyEnabled:true}), .find({"interests":{"$all":req.body.interests}}), .find({"_id":req.params.id},geoLib.distance([[req.body.gcordinates],[])) }); 

基本上我需要执行内部查找或查询内部查询。

根据您在代码中的注释,您想在查找查询中使用多个条件,以便满足其中一个条件,并根据该条件返回结果。 您可以使用$ or$ and来实现它。 下面给出了与您的条件类似的示例代码。

find({
  $or:[
       { isBuddyEnabled:true },
       { "interests": { "$all":req.body.interests }},
       { $and:[ 
               { "_id":req.params.id }, 
               { geoLib.distance...rest_of_the_condition }
              ] 
       }
      ]
});

暂无
暂无

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

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