Mongodb有条件查找
我发布3个数据到我的routes.js,我想要如果这3个数据不为空或空的查询应该是这样的:
db.collection.find({posteddata1:req.body....,posteddate2:req.body....,posteddata3:req.body...})
如果一个数据是空的或emty比我的查询:
db.collection.find({posteddate2:req.body....,posteddata3:req.body...})
如果所有的数据都是null或emtpty比我的查询shold be:
db.collection.find({});
我不想用if语句。 有没有简单的方法来做到这一点。
不能让你的情况是这样的:
if(db.collection.find({a:1,b:2,c:3}).count() > 0) { //do db.collection.find({a:1,b:2,c:3}); } else if(db.collection.find({b:2,c:3}).count() > 0) { //do db.collection.find({b:2,c:3}); } else { //do db.collection.find({}); }