I work with mongodb 3.2 I can not do a search on several fields. for example, in yii it was easier:

$command = Yii->createCommand($sql) if ($one) $command->addCondition($cond1); if ($two) $command->addCondition($cond2); $result = $command->queryAll(); 

and in mongodb it turns out only 1 time you can set the parameters, and then the search starts

 db.coll.find({}); 

I would like this:

 curs = myColl.find(); if(cond1) { curs2 = curs.find({cond2}) } 

that is, we will move hierarchically, adding filtering filters as we go.

PS: I write API, for some reason, $ where it does not plow from me .. (meteor 1.3, nimble / restivus)

UPD

 { "_id" : "u5QYDyrbXn24FYaQD", "baseFields" : { "carCatId" : "yCHznKsJaMjL3dzTM", "carClassId" : "c69tHZHGWyxLDthkL", "carClassTplId" : 4, "carMarkaId" : "76Xme6nPQyaLQST9g", "carGosNomer" : "Z 123", "carProductionYear" : "2010", "carHourlyPrice" : "5000" }, "extraFields" : { "carKubatura" : "2000", "carTonna" : "5000" } } { "_id" : "H53hpx6mBPrWPTPCB", "baseFields" : { "carCatId" : "ye3DNu482L9uGeMqk", "carClassId" : "4sf3eXAcqmLe7XysY", "carClassTplId" : 3, "carMarkaId" : "i3BKrpJf4baLYo8Ky", "carGosNomer" : "LLL", "carProductionYear" : "2010", "carHourlyPrice" : "5000" }, "extraFields" : { "carSeats" : "4", "carColor" : "1" } 

}

trying to find like this:

 var cond = {'baseFields':{}}; cond.baseFields.carCatId = 'yCHznKsJaMjL3dzTM'; db.mycoll.find(cond); 

    1 answer 1

    Yii just hid these details from you. In fact, everything is the same as in SQL - you need to assemble the search condition yourself (or the query text for SQL) and pass it into a single call to find.

    Figuratively speaking,

     conditions = {}; if (cond1) { conditions['field'] = value; // ну или любую другую допустимую для mongodb структуру запроса } myColl.find(conditions); 
    • for some reason, I'm not looking for how you suggested - Vfvtnjd
    • Well, then show me exactly how you try, which is then actually sent to find, and, before the bundle, a similar request to the mongo client, which returns data. And, and very carefully check for typos. This crap called mongodb will not tell you if you try to read a nonexistent base or collection. - Fine
    • Well, where is the proof from a regular mongo client that such a request will return something? Because nothing will return. Mongodb uses dot notation to refer to attached objects: docs.mongodb.com/manual/core/document/#dot-notation - Shallow