A case study of searching data with mgo specified string length
Case study of mgo specified string length to find data? This problem may be often seen in our daily study or work. I hope you can gain a lot from this question. The following is the reference content that the editor brings to you, let's take a look at it!
The content of this article is about the mgo specified string length to find data method introduction (code), there is a certain reference value, friends in need can refer to, hope to help you.
Work occasionally according to the length of the string field to filter some data, this time may use regular expressions, you can also use mongodb $where, regular expressions in different languages, the correct way to write different, here to record.
If you look for data with a comment field string length greater than 10, the mongodb command line is written as follows:
Where is written as follows:
Find ({"comment": {"$exists": true}, "$where": "this.comment.length > 10"})
How to write a regular expression:
Find ({"comment": {"$regex": / ^. {10,} $/}})
It is written as follows in go language:
Where is written as follows:
Collection.Find (bson.M {"comment": bson.M {"$exists": true}, "$where": "this.comment.length > 10"})
How to write a regular expression:
Collection.Find (bson.M {"comment": bson.M {"$exists": true, "$regex": bson.RegEx {`^. {10,} $`, ""})
Other conditions are regular:
^. {n ·m} $n