mongo db: search for multiple criteria



  • work with mongodb 3.2 I can't search several fields, like yii was easier:

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

    a mongodb only produces 1 times the parameters and the search continues

    db.coll.find({});
    

    I'd like that:

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

    I mean, hierarchically, we're going to move along with the sewing filters.

    P.S: I'll write I.P., why $ doesn't fall to 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"
    }

    I'm trying to find this:

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



  • Yii just kept these details from you. In fact, as in SQL, you need to collect the search condition (or the SQL request) on your own and put it on the same page.

    In a way,

    conditions = {};
    if (cond1) {
        conditions['field'] = value; 
        // ну или любую другую допустимую для mongodb структуру запроса
    }
    myColl.find(conditions);
    



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2