Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
304 views
in Technique[技术] by (71.8m points)

mongodb - Can Mongo DB perform queries only if a condition is matched

I have a barbershop app were every user can filter out the barbershops based on some criteria. I wanted to know if there is a way to only add filters when the user has set their preferred filters in the app. This is my how I attempted to do it which threw multiple errors:

bool avg_price = true;
bool barbers = true;
bool chairs = false;
bool dailyCustomers = false;

final db = await mongo.Db.create('serverCall');
db.open().then((value) {
barbershops = db.collection('Houses').find((avg_price)?mongo.where.lte("Price",15) 
:mongo.where.lte("Price", 0).(barbers)?gte("barbers", 3):gte("barbers", 0).(chairs)?gt("chairs", 
8):gt("chairs", 8).(dailyCustomers)?gt("minDailyCustomers", 15):gt("minDailyCustomers",15)
).forEach((element) {
    print(element);
  });

I know the code is a bit confusing, but it basically checks if the user has entered the preference, if yes then the bool relating to that preference will be set to true and the filter would need to be applied in that case.

My first week learning MongDB, so any help would be greatly appreciated.

Thanks Community!

question from:https://stackoverflow.com/questions/65936883/can-mongo-db-perform-queries-only-if-a-condition-is-matched

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...