Home > Database > Mysql Tutorial > Introduction to how mgo specifies the string length to find data (code)

Introduction to how mgo specifies the string length to find data (code)

不言
Release: 2019-03-23 17:52:58
forward
2760 people have browsed it

The content of this article is about the method of mgo specifying the string length to find data (code). It has certain reference value. Friends in need can refer to it. I hope it will help You helped.

At work, we occasionally filter some data based on the length of the string field. At this time, we may use regular expressions, or we can use mongodb's $where. Regular expressions are written correctly in different languages. There are some differences, so I’ll record them here.

If you are looking for data with a comment field string length greater than 10, the mongodb command line is written as follows:

$where is written as:

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

Regular expression is written as:

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

The writing method in go language is as follows:

$where writing method:

collection.Find(bson.M{"comment": bson.M{"$exists": true}, "$where": "this.comment.length > 10"})
Copy after login

Regular expression writing method:

collection.Find(bson.M{"comment": bson.M{"$exists": true, "$regex": bson.RegEx{`^.{10,}$`, ""}}})
Copy after login

Other conditions regular:

^.{n,m}$ n <= length<= m
^.{n}$ length= n

this length It is the length of characters. For example, the length of "regular expression" is 5

As for search performance, it is said on the Internet that regular expression has better performance than $where. When the amount of data is not large, a simple test shows that regular expression has better search performance. One point, I will have time to do further research later

This article has ended here. For more other exciting content, you can pay attention to the mongodb video tutorial column on the PHP Chinese website !

The above is the detailed content of Introduction to how mgo specifies the string length to find data (code). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
mongodb start
From 1970-01-01 08:00:00
0
0
0
linux - ubuntu14 error installing mongodb
From 1970-01-01 08:00:00
0
0
0
Use of symfony2 mongodb
From 1970-01-01 08:00:00
0
0
0
mongodb _id rename
From 1970-01-01 08:00:00
0
0
0
Parameter understanding of mongodb
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template