《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》,点击传送门,即可获取!
db.sang_collect.find({KaTeX parse error: Expected '}', got 'EOF' at end of input: text:{search:““Java C#””}})
用一对双引号将查询条件括起来,如果想查询包含PHP或者Python的文档,操作如下:
db.sang_collect.find({KaTeX parse error: Expected '}', got 'EOF' at end of input: text:{search:“PHP Python”}})
如果想查询既有PHP,又有Python,但是又不包括Java的文档,如下:
db.sang_collect.find({KaTeX parse error: Expected '}', got 'EOF' at end of input: text:{search:“PHP Python -Java”}})
建立了全文索引之后,我们也可以查看查询结果的相似度,使用$meta,如下:
db.sang_collect.find({KaTeX parse error: Expected '}', got 'EOF' at end of input: text:{search:“PHP Python”}},{score:{$meta:“textScore”}})
此时查询结果中会多出一个score字段,该字段的值越大,表示相似度越高,我们可以根据score利用sort来对其进行排序,如下:
db.sang_collect.find({KaTeX parse error: Expected '}', got 'EOF' at end of input: text:{search:“PHP Python”}},{score:{KaTeX parse error: Expected 'EOF', got '}' at position 17: …eta:"textScore"}̲}).sort({score:…meta:“textScore”}})
全文索引目前看起来功能还是很强大,可惜暂时不支持中文,不过网上对此也有很多解决方案,小伙伴们可以自行搜索查看。
地理空间索引
地理空间索引类型
地理空间索引可以分为两类:
1.2d索引,可以用来存储和查找平面上的点。
2.2d sphere索引,可以用来存储和查找球面上的点。
2d索引
2d索引一般我们可以用在游戏地图中。
向集合中插入一条记录点的数据:
db.sang_collect.insert({x:[90,0]})
插入数据的格式为[经度,纬度],取值范围,经度[-180,180],纬度[-90,90]。数据插入成功之后,我们先通过如下命令创建索引:
db.sang_collect.ensureIndex({x:“2d”})
然后通过$near我们可以查询某一个点附近的点,如下:
db.sang_collect.find({x:{$near:[90,0]}})
默认情况下返回该点附近100个点,我们可以通过$maxDistance来设置返回的最远距离:
db.sang_collect.find({x:{ n e a r : [ 90 , 0 ] , near:[90,0], near:[90,0],maxDistance:99}})
我们也可以通过$geoWithin查询某个形状内的点,比如查询矩形中的点:
db.sang_collect.find({x:{KaTeX parse error: Expected '}', got 'EOF' at end of input: geoWithin:{box:[[0,0],[91,1]]}}})
两个坐标点用来确定矩形的位置。
查询圆中的点:
db.sang_collect.find({x:{KaTeX parse error: Expected '}', got 'EOF' at end of input: geoWithin:{center:[[0,0],90]}}})
参数分别表示圆的圆心和半径。
查询多边形中的点:
db.sang_collect.find({x:{KaTeX parse error: Expected '}', got 'EOF' at end of input: geoWithin:{polygon:[[0,0],[100,0],[100,1],[0,1]]}}})
这里可以填入任意多个点,表示多边形中的各个点。
2d sphere索引
2dsphere适用于球面类型的地图,它的数据类型是GeoJSON格式的,我们可以在http://geojson.org/地址上查看GeoJSON格式的样式,比如我们描述一个点,GeoJSON如下:
{
“_id” : ObjectId(“59f5e0571f9e8e181ffc3196”),
“name” : “shenzhen”,
“location” : {
“type” : “Point”,
“coordinates” : [
90.0,
0.0
]
}
}
描述线,GeoJSON格式如下:
{
“_id” : ObjectId(“59f5e0d01f9e8e181ffc3199”),
“name” : “shenzhen”,
“location” : {
“type” : “LineString”,
“coordinates” : [
[
90.0,
0.0
],
[
90.0,
1.0
],
[
90.0,
2.0
]
]
}
}
描述多边形,GeoJSON格式如下:
{
“_id” : ObjectId(“59f5e3f91f9e8e181ffc31d0”),
“name” : “beijing”,
“location” : {
“type” : “Polygon”,
“coordinates” : [
[
[
0.0,
1.0
],
[
0.0,
2.0
],
[
1.0,
2.0
],
[
0.0,
1.0
]
]
]
}
}
还有其他的类型,具体小伙伴们可以参考http://geojson.org/。有了数据之后,我们可以通过如下操作来创建地理空间索引了:
db.sang_collect.ensureIndex({location:“2dsphere”})
比如我想查询和深圳这个区域有交集的文档,如下:
var shenzhen = db.sang_collect.findOne({name:“shenzhen”})
db.sang_collect.find({location:{KaTeX parse error: Expected '}', got 'EOF' at end of input: geoIntersects:{geometry:shenzhen.location}}})
这里的查询结果是和深圳这个区域有交集的都会查到(比如经过深圳的高速公路、铁路等),我们也可以只查询深圳市内的区域(比如深圳市内所有的学校),如下:
var shenzhen = db.sang_collect.findOne({name:“shenzhen”})
db.sang_collect.find({location:{KaTeX parse error: Expected '}', got 'EOF' at end of input: within:{geometry:shenzhen.location}}})
也可以查询腾讯附近的其他位置,如下:
var QQ = db.sang_collect.findOne({name:“QQ”})
db.sang_collect.find({location:{KaTeX parse error: Expected '}', got 'EOF' at end of input: near:{geometry:QQ.location}}})
复合地理空间索引
位置往往只是我们查询的一个条件,比如我要查询深圳市内所有的学校,那我得再增加一个查询条件,如下:
var shenzhen = db.sang_collect.findOne({name:“shenzhen”})
db.sang_collect.find({location:{KaTeX parse error: Expected '}', got 'EOF' at end of input: within:{geometry:shenzhen.location}},name:“QQ”})
其他的查询条件跟在后面就行了。
好了,MongoDB中的索引问题我们就说到这里,小伙伴们有问题欢迎留言讨论。
参考资料:
最后
分布式技术专题+面试解析+相关的手写和学习的笔记pdf
还有更多Java笔记分享如下:
《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》,点击传送门,即可获取!
= db.sang_collect.findOne({name:“shenzhen”})
db.sang_collect.find({location:{KaTeX parse error: Expected '}', got 'EOF' at end of input: within:{geometry:shenzhen.location}},name:“QQ”})
其他的查询条件跟在后面就行了。
好了,MongoDB中的索引问题我们就说到这里,小伙伴们有问题欢迎留言讨论。
参考资料:
最后
分布式技术专题+面试解析+相关的手写和学习的笔记pdf
还有更多Java笔记分享如下:
[外链图片转存中…(img-OddYhxSJ-1714515937785)]
《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》,点击传送门,即可获取!