文章目录

一、第1关:了解并创建一个简单索引二、第2关:常见索引的创建三、第3关:有趣的地理位置索引

提示:以下是本篇文章正文内容,下面案例可供参考

一、第1关:了解并创建一个简单索引

编程要求 根据提示,在右侧命令行进行操作,在 test 数据库创建集合 student ,内容如下:

然后在集合中创建成绩的降序索引。

测试说明 平台会对你的操作进行测试:

展示当前数据库 test 中 student 集合的所有索引,符合编程要求便可通过。

代码如下(示例):

mongo

use test

db.student.insert([{_id:1,name:"王小明",age:15,score:90},{_id:2,name:"周晓晓",age:18,score:86},{_id:3,name:"王敏",age:20,score:96},{_id:4,name:"李晓亮",age:15,score:74},{_id:5,name:"张青青",age:21,score:88}])

db.student.createIndex({score:-1})

二、第2关:常见索引的创建

编程要求 根据提示,在右侧命令行进行操作,在 test2 数据库中创建集合 article,内容如下:

集合创建完成后,按要求创建以下索引: 用字段 follwers 和 title 创建复合升序索引; 用字段 tags 创建多 key 降序索引; 用_id创建哈希索引; 用字段 title 和 tags 创建文本索引。 测试说明 平台会对你的操作进行测试: 展示当前数据库 test2 中 article 集合的所有索引,符合编程要求便可通过。 代码如下(示例):

mongo

use test2

db.article.insert([

... {_id:1,title:"提升程序员工作效率的6个工具利器",tags:["Alfred","幕布"],follwers:543},

... {_id:2,title:"我是如何从零开始学习前端的",tags:["HTML","Html5","CSS"],follwers:1570},

... {_id:3,title:"20个非常有用的JAVA程序片段",tags:["Java","编程"],follwers:1920}])

db.article.createIndex({follwers:1,title:1})

db.article.createIndex({tags:-1})

db.article.createIndex({_id:'hashed'})

db.article.createIndex({title:'text',tags:'text'})

三、第3关:有趣的地理位置索引

编程要求 如图4所示,有6个人 A、B、C、D、E、F 的位置,请将这些位置信息插入到数据库 test3 的集合 people 中,并建立地理位置索引 personloc。

查询 A 周围100~3000米有哪些人;

查询 B 周围100~5000米有哪些人;

查询 C 周围3000~8000米有哪些人;

查询 D 周围3000~8000米有哪些人。

请逐条插入以下信息和位置 GeoJson 数据:

{_id:1,name:‘A’,personloc:{type:‘Point’,coordinates:[116.403981,39.914935]}} {_id:2,name:‘B’,personloc:{type:‘Point’,coordinates:[116.433733,39.909511]}} {_id:3,name:‘C’,personloc:{type:‘Point’,coordinates:[116.488781,39.949901]}} {_id:4,name:‘D’,personloc:{type:‘Point’,coordinates:[116.342609,39.948021]}} {_id:5,name:‘E’,personloc:{type:‘Point’,coordinates:[116.328236,39.901098]}} {_id:6,name:‘F’,personloc:{type:‘Point’,coordinates:[116.385728,39.871645]}} 测试说明 上述操作共有11条命令,请按要求填入右侧代码栏 Begin-End 中,每条命令以;号隔开。

填写完成之后点击评测,平台会对你的命令进行测试,如果成功,平台会输出如测试集所示的结果,否则会显示报错信息。

本关只需在代码文件中打即可。

代码如下(示例):

echo "

db.people.insert({_id:1,name:'A',personloc:{type:'Point',coordinates:[116.403981,39.914935]}});

db.people.insert({_id:2,name:'B',personloc:{type:'Point',coordinates:[116.433733,39.909511]}});

db.people.insert({_id:3,name:'C',personloc:{type:'Point',coordinates:[116.488781,39.949901]}});

db.people.insert({_id:4,name:'D',personloc:{type:'Point',coordinates:[116.342609,39.948021]}});

db.people.insert({_id:5,name:'E',personloc:{type:'Point',coordinates:[116.328236,39.901098]}});

db.people.insert({_id:6,name:'F',personloc:{type:'Point',coordinates:[116.385728,39.871645]}});

db.people.createIndex({personloc:'2dsphere'});

db.runCommand({geoNear:'people',near:{type:'Point',coordinates:[116.403981,39.914935]},spherical:true,minDistance:100,maxDistance:3000});

db.runCommand({geoNear:'people',near:{type:'Point',coordinates:[116.433733,39.909511]},spherical:true,minDistance:100,maxDistance:5000});

db.runCommand({geoNear:'people',near:{type:'Point',coordinates:[116.488781,39.949901]},spherical:true,minDistance:3000,maxDistance:8000});

db.runCommand({geoNear:'people',near:{type:'Point',coordinates:[116.342609,39.948021]},spherical:true,minDistance:3000,maxDistance:8000});

"

文章来源

评论可见,请评论后查看内容,谢谢!!!
 您阅读本篇文章共花了: