Mongo length of the string to find data in two ways

Work occasionally to filter some of the data according to the length of the string field, this time might use regular expressions, you can also use mongodb of $ where, regular expressions in different languages, the correct wording there are differences, it is hereby record it.  

If the comment field lookup data string length is larger than 10, mongodb command written as follows:

$ Where writing:

find({"comment":{"$exists":true},"$where":"this.comment.length>10"})

Regular expressions wording:

find({"comment":{"$regex":/^.{10,}$/}})

go languages ​​worded as follows:

$where写法:
collection.Find(bson.M{"comment": bson.M{"$exists": true}, "$where": "this.comment.length > 10"})
正则表达式写法:
collection.Find(bson.M{"comment": bson.M{"$exists": true, "$regex": bson.RegEx{`^.{10,}$`, ""}}})

Other regular conditions:
. N-^ {,} m $  n-<= length <= m
^ {n-} $.  Length = n

This length is the length of a character, such as "regular expression" length is 5

Transfer the contents of the above https://www.ucloud.cn/yun/19509.html

I tried 40w of data query, the regular performance much faster speed than $ where

Guess you like

Origin www.cnblogs.com/lfm601508022/p/12144240.html