代码之家  ›  专栏  ›  技术社区  ›  contemplator

MongoDB字符串字段值长度(Yii2)

  •  0
  • contemplator  · 技术社区  · 7 年前

    我只是在看书 How to query the string field value length in MongoDB

    db.getCollection('language').find({
      name: {$ne: ''}, 
      $where: "this.i18n.length < 3"
    })
    

    我现在尝试在Yii2中复制相同的内容:

    $languages = Language::find()
      ->where(['$ne', 'name', ''])
      ->andWhere(['<', 'this.i18n.length', '3'])
      ->all();
    

    但是数组是空的。

    我尝试了没有字符串长度的查询:

    $languages = Language::find()
      ->where(['$ne', 'name', ''])
      ->all();
    

    然后填充数组。

    我还尝试了仅使用字符串长度的查询:

    $languages1 = Language::find()
      ->where(['<', 'this.i18n.length', '3'])
      ->all();
    $languages2 = Language::find()
      ->where(['<', 'i18n.length', '3'])
      ->all();
    

    但数组仍为空。

    问题是:我如何得到 $where: "this.i18n.length < 3" 在Yii2工作?

    2 回复  |  直到 7 年前
        1
  •  1
  •   contemplator    7 年前

    我想没有办法直接查询数据库。我改为用php过滤结果:

    $languages = Language::find()->where(['$ne', 'name', ''])->all();
    foreach ($languages as $fac) {
      if(strlen($fac->i18n)<3){
        // my code here
      }
    }
    

    谢谢。

        2
  •  0
  •   matthPen    7 年前

    $languages = Language::find()
      ->where(['$ne', 'name', ''])
      ->andWhere(['=', '$where','this.i18n.length<3'])
      ->all();
    
    推荐文章