【问题标题】:How to check if string length is >0 in mongodb?如何检查mongodb中的字符串长度是否> 0?
【发布时间】:2018-09-15 19:07:42
【问题描述】:

我在 MongoDb 上从 GoLang 触发 mgo,以查找修剪后的字符串长度大于零的记录,我在下面尝试过,但都不起作用,请帮忙。

findQuery["shopname"] = bson.M{"$where": "len > 0"}
findQuery["shopname"] = bson.M{"$where": "shopname.len > 0"}
findQuery["shopname"] = bson.M{"$where": "this.shopname.len > 0"}
findQuery["shopname"] = bson.M{"$regex": ".*.*"}

【问题讨论】:

标签: mongodb


【解决方案1】:

如果您只是在寻找非空值,试试这个:

bson.M{"$exists": true, "$ne": ""}

【讨论】:

  • 非常感谢,你是最棒的!
【解决方案2】:

另一种方法或另一种方法是检查数组的第 0 个元素是否存在。

"arr.0" : bson.M{"$exists": true}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-03
    • 1970-01-01
    • 1970-01-01
    • 2019-12-22
    • 1970-01-01
    • 2015-06-23
    相关资源
    最近更新 更多