此文基于Spring的MongoTemplate,介绍MongoDB比较基础常用的增删改查操作。涵盖了从集合创建、索引创建和CRUD操作到更高级的功能(如Map-Reduce和聚合)等等。不多说,直接上代码。
模糊查询:https://blog.csdn.net/weixin_42227975/article/details/80533678
1、精确查询用“is” Criteria criteria=new Criteria("namespaceName"); criteria.is(namespaceName); Query query = new Query(criteria); return mongoTemplate.find(query,StorageBO.class,StorageConstant.STORAGE_COLLECTION_NAME); 2、模糊查询用“regex” Criteria criteria=new Criteria("storageName"); criteria.regex(storageName); Query query = new Query(criteria); return mongoTemplate.find(query,StorageBO.class,StorageConstant.STORAGE_COLLECTION_NAME); 3、模糊查询示例2 //完全匹配 Pattern pattern = Pattern.compile("^hzb$", Pattern.CASE_INSENSITIVE); //右匹配 Pattern pattern = Pattern.compile("^.*hzb$", Pattern.CASE_INSENSITIVE); //左匹配 Pattern pattern = Pattern.compile("^hzb.*$", Pattern.CASE_INSENSITIVE); //模糊匹配 Pattern pattern = Pattern.compile("^.*hzb.*$", Pattern.CASE_INSENSITIVE); Query query = Query.query(Criteria.where(fieldName).regex(pattern)); List<StorageBO> storages = mongoTemplate.find(query, StorageBO.class, collectionName); return storages; 4、按createTime降序排序 以下红色部分是排序设置 Criteria criteria=new Criteria("namespaceName"); criteria.is(namespaceName); criteria.and("tenantName").is(tenantName); Query query = new Query(criteria); query.with(new Sort(Sort.Direction.DESC, "createTime")); return mongoTemplate.find(query,StorageBO.class,StorageConstant.STORAGE_COLLECTION_NAME);
其它查询:https://blog.csdn.net/lxb15959168136/article/details/52253415
模拟基础数据
Person dave, oliver, carter; @Autowired private MongoOperations mongoOps; Collation en; @Before public void setUp() { // 排序规则需要一个用于创建的区域设置。中文排序使用:zh en = Collation.of("en") // 排序规则强度定义表示字符之间差异的比较级别。 // 根据所选的强度,可以配置各种选项(区分大小写、大小写排序等)。 .strength(Collation.ComparisonLevel.secondary().includeCase()) // 指定是将数字字符串比较为数字还是字符串。 .numericOrderingEnabled() // 为了便于比较,指定排序规则是否应将空格和标点符号视为基本字符。 .alternate(Collation.Alternate.shifted().punct()) // 指定带有变音符号的字符串是否从字符串的后面排序,例如使用英语字典排序。 .forwardDiacriticSort() // 指定是否检查文本是否需要规范化以及是否执行规范化。 .normalizationEnabled(); // 排序可用于创建集合和索引。如果您创建了一个指定排序规则的集合,则排序规则将应用于索引创建和查询 mongoOps.createCollection(Person.class, CollectionOptions.just(en)); mongoOps.indexOps(Person.class).ensureIndex(new Index("firstname", Sort.Direction.ASC).collation(en)); dave = mongoOps.insert(new Person("Dave", "Matthews")); oliver = mongoOps.insert(new Person("Oliver August", "Matthews")); carter = mongoOps.insert(new Person("Carter", "Beauford")); } @After public void destory(){ mongoOps.dropCollection(Person.class); }
新增操作
/** * 存在同ID更新;不存在ID新增。 */ @Test(expected = DuplicateKeyException.class) public void testSave(){ Person person = new Person("Dave", "Dreamson"); person.setId(dave.getId()); // 使用save,如果是相同ID,则更新相应的记录 final Person dave = mongoOps.save(person); Assert.assertEquals("Dreamson",dave.getLastname()); // 使用insert,如果是相同ID,抛出DuplicateKeyException mongoOps.insert(person); } /** * 批量新增 */ @Test public void testInsertAll(){ mongoOps.insertAll(Lists.newArrayList( new Person("Dave", "Matthews"), new Person("Oliver August", "Matthews"), new Person("Carter", "Beauford") )); final List<Person> all = mongoOps.findAll(Person.class); Assert.assertEquals(6,all.size()); }
更新操作
/** * 更新第一条记录 */ @Test public void testUpdateFirst(){ final UpdateResult updateResult = mongoOps.updateFirst(Query.query(Criteria.where("firstname").is("Carter")), Update.update("lastname", "Dreamson"), Person.class); System.out.println(updateResult); Person carter = mongoOps.findOne(Query.query(Criteria.where("firstname").is("Carter")), Person.class); Assert.assertEquals("Dreamson",carter.getLastname()); } /** * 批量更新 */ @Test public void testBatchUpdate(){ mongoOps.updateMulti(new Query(Criteria.where("age").exists(false)), new Update().inc("age", 50), Person.class); mongoOps.findAll(Person.class).forEach(person-> Assert.assertEquals(50,person.getAge().longValue()) ); } /** * 存在更新,不存在新增 */ @Test public void testUpsert(){ final UpdateResult updateResult = mongoOps.upsert(Query.query(Criteria.where("firstname").is("xiaoming").and("lastname").is("Joe")) , Update.update("age", 3), Person.class); System.out.println(updateResult); final Person xiaoming = mongoOps.findOne(Query.query(Criteria.where("firstname").is("xiaoming")), Person.class); Assert.assertEquals(xiaoming.getLastname(),"Joe"); Assert.assertEquals(xiaoming.getAge().longValue(),3); } /** * 找到记录并更新,没找到返回NULL */ @Test public void testFindAndModify(){ Query query = new Query(Criteria.where("firstname").is("Dave")); Update update = new Update().inc("age", 1); Person p = mongoOps.findAndModify(query, update, Person.class); // 返回旧对象 if(Optional.ofNullable(p).isPresent()){ Assert.assertEquals(p.getFirstname(), "Dave"); Assert.assertEquals(p.getAge(), null); p = mongoOps.findOne(query, Person.class); Assert.assertEquals(p.getAge().longValue(), 1); p = mongoOps.findAndModify(query, update, new FindAndModifyOptions().returnNew(true), Person.class); // 返回新对象 Assert.assertEquals(p.getAge().longValue(), 2); }else{ System.out.println("没找到相应的记录!"); } } /** * 查找并替换,返回旧记录 */ @Test public void testFindAndReplace(){ Optional<Person> result = mongoOps.update(Person.class) .matching(Query.query(Criteria.where("firstname").is("Dave"))) .replaceWith(new Person("Dick","what's up!")) .withOptions(FindAndReplaceOptions.options().upsert()) .as(Person.class) .findAndReplace(); Assert.assertEquals("Dave", result.get().getFirstname()); final Person dick = mongoOps.findById(result.get().getId(), Person.class); Assert.assertEquals("Dick", dick.getFirstname()); }
删除操作
/** * 删除对象 */ @Test public void testRemove(){ // 批量删除 final DeleteResult deleteResult = mongoOps.remove( Query.query(Criteria.where("firstname").is("Oliver August")), Person.class); System.out.println(deleteResult); Assert.assertEquals(2,mongoOps.findAll(Person.class).size()); mongoOps.remove(dave); Assert.assertEquals(1,mongoOps.findAll(Person.class).size()); // 批量逐个删除 mongoOps.findAllAndRemove( Query.query(Criteria.where("firstname").is("Carter")), Person.class); Assert.assertEquals(0,mongoOps.findAll(Person.class).size()); }
乐观锁处理
/** * 乐观锁,version版本自增。 * 如果更新时version<=原记录version,则抛出异常 */ @Test(expected = OptimisticLockingFailureException.class) public void testOptimisticLocking(){ Person daenerys = mongoOps.insert(new Person("Daenerys","ABC")); Person tmp = mongoOps.findOne(Query.query(Criteria.where("id").is(daenerys.getId())), Person.class); daenerys.setAge(100); mongoOps.save(daenerys); mongoOps.save(tmp); }
查询操作
/** * 通过id查询记录 */ @Test public void testFindById(){ Person dave = mongoOps.findOne(Query.query(Criteria.where("firstname").is("Dave")), Person.class); dave = mongoOps.findById(dave.getId(), Person.class); Assert.assertEquals(this.dave,dave); } /** * 查询所有 */ @Test public void testFindAll(){ final List<Person> all = mongoOps.findAll(Person.class); Assert.assertEquals(all.size(),3); Assert.assertTrue(all.containsAll(Lists.newArrayList(dave,oliver,carter))); } /** * 查询一条记录 */ @Test public void testFindOne(){ Person dave = mongoOps.findOne(Query.query(Criteria.where("firstname").is("Dave")), Person.class); Assert.assertEquals("Matthews",dave.getLastname()); } /** * 通过JSON的方式查询 */ @Test public void testBasicQuery(){ BasicQuery query = new BasicQuery("{ firstname : { $in : ['Dave','Carter'] }, lastname : { $eq : 'Beauford' }}"); List<Person> result = mongoOps.find(query, Person.class); Assert.assertEquals(1,result.size()); Assert.assertEquals("Carter",result.get(0).getFirstname()); } /** * 为单个字段获取不同的值 */ @Test public void testDistinct(){ List<String> lastname = mongoOps.query(Person.class) .distinct("lastname") .as(String.class) .all(); lastname.forEach(System.out::println); Assert.assertTrue(lastname.containsAll(Lists.newArrayList("Matthews","Beauford"))); } /** * 集合排序操作 */ @Test public void testCollations(){ // 使用collation的find查询 Query query = new Query(Criteria.where("lastname").is("Matthews")).collation(en); List<Person> results = mongoOps.find(query, Person.class); System.out.println(results); Assert.assertEquals(results.get(0).getFirstname(),"Dave"); // 使用collation的aggregation查询 // newAggregation().withOptions(AggregationOptions.builder().collation(collation).build()); }
GEO查询操作
1、新建初始化脚本starbucks-in-nyc.json
[ { "_class":"example.springdata.mongodb.repo.Store", "id":"16628", "name":"26th & Broadway", "street":"1140 Broadway", "city":"New York", "location" : { "latitude":40.743827, "longitude":-73.989015 } }, { "_class":"example.springdata.mongodb.repo.Store", "id":"7427", "name":"29th & Park-Park Ave. South", "street":"424 Park Avenue South", "city":"New York", "location" : { "latitude":4.074426, "longitude":-73.983749} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"7263", "name":"43rd & 8th", "street":"684 Eighth Avenue", "city":"New York", "location" : { "latitude":40.758001, "longitude":-73.988994} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"7215", "name":"1585 Broadway (47th)", "street":"1585 Broadway", "city":"New York", "location" : { "latitude":40.759686, "longitude":-73.985235} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"7393", "name":"49th & 8th-World Wide Plaza", "street":"325 W 49th St", "city":"New York", "location" : { "latitude":40.762176, "longitude":-73.987472} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"7316", "name":"31st & 7th", "street":"370 7th Avenue", "city":"New York", "location" : { "latitude":40.74899, "longitude":-73.992372} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"7540", "name":"17th and Broadway (41 Union Square", "street":"41 Union Square West", "city":"New York", "location" : { "latitude":40.737035, "longitude":-73.990558} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"7611", "name":"40th & Lexington (360 Lex)", "street":"360 Lexington Avenue", "city":"New York", "location" : { "latitude":40.750332, "longitude":-73.977043} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"15461", "name":"Jackson Square at Greenwich Ave", "street":"122 Greenwich Avenue, (space A)", "city":"New York", "location" : { "latitude":40.738441, "longitude":-74.002217} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"852", "name":"1656 Broadway", "street":"1656 Broadway", "city":"New York", "location" : { "latitude":40.762092, "longitude":-73.983345} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"11738", "name":"63rd & Broadway", "street":"1889 Broadway", "city":"New York", "location" : { "latitude":40.771365, "longitude":-73.982591} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"817", "name":"1st Avenue & 75th St.", "street":"1445 First Avenue", "city":"New York", "location" : { "latitude":40.770031, "longitude":-73.954631} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"839", "name":"Broadway @ 81st", "street":"2252 Broadway", "city":"New York", "location" : { "latitude":40.784905, "longitude":-73.978696} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"7378", "name":"Union Square", "street":"10 UNION SQUARE EAST", "city":"New York", "location" : { "latitude":40.735022, "longitude":-73.989848} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"7547", "name":"Sheridan Square (72 Grove Street)", "street":"72 Grove Street", "city":"New York", "location" : { "latitude":40.73311, "longitude":-74.002707} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"7216", "name":"87th & Lexington", "street":"120 EAST 87TH ST", "city":"New York", "location" : { "latitude":40.780139, "longitude":-73.955346} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"7352", "name":"86th & Columbus", "street":"540 Columbus Avenue", "city":"New York", "location" : { "latitude":40.786704, "longitude":-73.972244} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"7346", "name":"SONY", "street":"550 Madison Avenue, #A32", "city":"New York", "location" : { "latitude":40.761478, "longitude":-73.973501} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"7317", "name":"73rd & Columbus", "street":"267-275 Columbus Ave", "city":"New York", "location" : { "latitude":40.777949, "longitude":-73.978175} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"14091", "name":"Macy's 5th Floor - Herald Square", "street":"151 W. 34th Street", "city":"New York", "location" : { "latitude":40.751148, "longitude":-73.990061} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"7525", "name":"135 E 57 Street (New World)", "street":"135 East 57th Street", "city":"New York", "location" : { "latitude":40.760965, "longitude":-73.96921} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"7608", "name":"48th & Lexington", "street":"511 Lexington Avenue", "city":"New York", "location" : { "latitude":40.755021, "longitude":-73.973223} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"75589", "name":"Marriott Marquis-Lobby", "street":"1535 Broadway", "city":"New York", "location" : { "latitude":40.757993, "longitude":-73.985633} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"7373", "name":"Grand Central Station", "street":"107 E 43rd St, Space MC-72", "city":"New York", "location" : { "latitude":40.753291, "longitude":-73.977635} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"75847", "name":"Hilton New York Marketplace", "street":"1335 Avenue of the Americas", "city":"New York", "location" : { "latitude":40.762304, "longitude":-73.979184} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"7342", "name":"41st & 3rd", "street":"639 3rd Avenue", "city":"New York", "location" : { "latitude":40.750195, "longitude":-73.974565} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"9241", "name":"Park Avenue Plaza", "street":"55 E 53rd St", "city":"New York", "location" : { "latitude":40.759285, "longitude":-73.973516} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"7860", "name":"Trump Tower", "street":"725 5th Avenue", "city":"New York", "location" : { "latitude":40.762555, "longitude":-73.974236} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"16576", "name":"36th St & Sixth Avenue", "street":"977 Avenue of the Americas", "city":"New York", "location" : { "latitude":40.751005, "longitude":-73.986913} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"14441", "name":"118th & Frederick Douglas Blvd.", "street":"2195 Frederick Douglas Boulevard", "city":"New York", "location" : { "latitude":40.805915, "longitude":-73.954495} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"5114", "name":"96th & Lexington Ave", "street":"1491 Lexington Avenue", "city":"New York", "location" : { "latitude":40.785886, "longitude":-73.950919} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"15685", "name":"69th & First", "street":"1281 First Avenue", "city":"New York", "location" : { "latitude":40.765864, "longitude":-73.957431} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"10751", "name":"Roosevelt Island", "street":"455 Main Street", "city":"New York", "location" : { "latitude":40.759326, "longitude":-73.95289} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"10608", "name":"Third & 60th", "street":"1021 Third Avenue", "city":"New York", "location" : { "latitude":40.762394, "longitude":-73.965745} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"10693", "name":"Bloomberg Building", "street":"731 Lexington Avenue", "city":"New York", "location" : { "latitude":40.761401, "longitude":-73.967862} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"7404", "name":"57th & Lexington", "street":"116 E. 57th Street", "city":"New York", "location" : { "latitude":40.761088, "longitude":-73.969984} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"10841", "name":"55th & Lexington", "street":"655 Lexington Avenue", "city":"New York", "location" : { "latitude":40.759446, "longitude":-73.969979} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"7655", "name":"53rd & Lexington", "street":"630 Lexington Ave", "city":"New York", "location" : { "latitude":40.758743, "longitude":-73.97094} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"7761", "name":"52nd & Lexington", "street":"599 Lexington", "city":"New York", "location" : { "latitude":40.757616, "longitude":-73.970857} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"7802", "name":"50th & Lexington", "street":"560 Lexington Avenue", "city":"New York", "location" : { "latitude":40.756861, "longitude":-73.972635} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"7370", "name":"48th & 3rd", "street":"757 Third Avenue", "city":"New York", "location" : { "latitude":40.754122, "longitude":-73.971714} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"7670", "name":"Worldwide Plaza II", "street":"825 Eighth Avenue, W-9", "city":"New York", "location" : { "latitude":40.762228, "longitude":-73.986567} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"9242", "name":"450 Lexington Ave.", "street":"450 Lexington Avenue", "city":"New York", "location" : { "latitude":40.753176, "longitude":-73.974975} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"7426", "name":"575 Fifth Avenue", "street":"575 Fifth Avenue", "city":"New York", "location" : { "latitude":40.756131, "longitude":-73.978922} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"14241", "name":"NHL store @ 47th & 6th", "street":"1185 Avenue of the Americas", "city":"New York", "location" : { "latitude":40.758048, "longitude":-73.982192} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"7462", "name":"545 Fifth Ave.", "street":"545 Fifth Ave.", "city":"New York", "location" : { "latitude":40.755168, "longitude":-73.979409} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"7297", "name":"45th St. & Sixth Ave.", "street":"1166 Avenue of the Americas", "city":"New York", "location" : { "latitude":40.756649, "longitude":-73.982141} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"11737", "name":"Madison & 44th", "street":"340 Madison Avenue", "city":"New York", "location" : { "latitude":40.753944, "longitude":-73.978759} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"9244", "name":"335 Madison Ave.", "street":"335 Madison Avenue", "city":"New York", "location" : { "latitude":40.753887, "longitude":-73.978719} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"7506", "name":"150 E. 42nd Street", "street":"150 E. 42nd Street", "city":"New York", "location" : { "latitude":40.751238, "longitude":-73.975608} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"7222", "name":"330 Madison", "street":"330 Madison Ave", "city":"New York", "location" : { "latitude":40.753335, "longitude":-73.979405} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"7898", "name":"42nd & Park", "street":"125 Park Ave", "city":"New York", "location" : { "latitude":40.751779, "longitude":-73.977735} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"8993", "name":"45th & Broadway", "street":"1530 Broadway", "city":"New York", "location" : { "latitude":40.757662, "longitude":-73.985721} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"15440", "name":"43rd & Sixth", "street":"1101 - 1109 Avenue of the Americas", "city":"New York", "location" : { "latitude":40.755352, "longitude":-73.983972} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"7801", "name":"41st & Madison", "street":"295 Madison Avenue", "city":"New York", "location" : { "latitude":40.751957, "longitude":-73.979722} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"11764", "name":"Empire State Building", "street":"350 Fifth Avenue", "city":"New York", "location" : { "latitude":40.74866, "longitude":-73.985614} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"8769", "name":"West 43rd and Broadway", "street":"1500 Broadway", "city":"New York", "location" : { "latitude":40.756643, "longitude":-73.985904} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"7273", "name":"42nd & 6th", "street":"1100 Avenue of the Americas", "city":"New York", "location" : { "latitude":40.754883, "longitude":-73.984074} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"8812", "name":"39th & Park", "street":"90 Park Ave", "city":"New York", "location" : { "latitude":40.750628, "longitude":-73.979265} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"7890", "name":"42nd & Eighth", "street":"251 West 42nd Street", "city":"New York", "location" : { "latitude":40.757171, "longitude":-73.98905} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"7463", "name":"41st and Broadway", "street":"1460 Broadway", "city":"New York", "location" : { "latitude":40.755043, "longitude":-73.986415} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"7511", "name":"525 Seventh Avenue (New World)", "street":"525 7th Avenue", "city":"New York", "location" : { "latitude":40.753647, "longitude":-73.988434} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"7468", "name":"39th and 8th", "street":"600 Eighth Ave.", "city":"New York", "location" : { "latitude":40.755325, "longitude":-73.990906} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"7536", "name":"36th and Madison", "street":"200 Madison Avenue", "city":"New York", "location" : { "latitude":40.748918, "longitude":-73.982682} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"7238", "name":"1372 Broadway", "street":"1372 Broadway", "city":"New York", "location" : { "latitude":40.751985, "longitude":-73.986909} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"10784", "name":"35th & 5th", "street":"373 5th Avenue", "city":"New York", "location" : { "latitude":40.749101, "longitude":-73.983799} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"8130", "name":"East 34th and Park", "street":"3 Park Avenue", "city":"New York", "location" : { "latitude":40.747051, "longitude":-73.981236} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"7403", "name":"35th & 7th", "street":"462 7th Avenue", "city":"New York", "location" : { "latitude":40.751842, "longitude":-73.990214} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"14092", "name":"Macy's 6th Floor - Herald Square", "street":"151 W. 34th Street", "city":"New York", "location" : { "latitude":40.751103, "longitude":-73.989518} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"7446", "name":"450 7th Avenue", "street":"450 7th Avenue", "city":"New York", "location" : { "latitude":40.751357, "longitude":-73.990435} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"7769", "name":"Herald Square- Macy's", "street":"151 W. 34th Street, Room 900", "city":"New York", "location" : { "latitude":40.751014, "longitude":-73.990248} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"7470", "name":"35th and 8th", "street":"494 Eighth Ave.", "city":"New York", "location" : { "latitude":40.752633, "longitude":-73.992905} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"11650", "name":"Hudson & 10th", "street":"518 Hudson Street", "city":"New York", "location" : { "latitude":40.733677, "longitude":-74.006126} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"7711", "name":"15th & Ninth", "street":"76 9th Ave", "city":"New York", "location" : { "latitude":40.741684, "longitude":-74.004629} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"7255", "name":"8th & 16th", "street":"124 Eighth Avenue", "city":"New York", "location" : { "latitude":40.741152, "longitude":-74.001294} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"7586", "name":"19th & 8th", "street":"177 Eighth Avenue", "city":"New York", "location" : { "latitude":40.742999, "longitude":-74.000343} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"7622", "name":"15th & Third", "street":"145 Third Avenue", "city":"New York", "location" : { "latitude":40.733749, "longitude":-73.986598} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"7285", "name":"22nd St. & Sixth Ave.", "street":"684 Avenue of the Americas", "city":"New York", "location" : { "latitude":40.741844, "longitude":-73.993237} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"14618", "name":"23rd - 24th & 7th", "street":"229 Seventh Avenue", "city":"New York", "location" : { "latitude":40.744315, "longitude":-73.99525} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"10396", "name":"21st & 5th", "street":"4 W. 21st Street", "city":"New York", "location" : { "latitude":40.740382, "longitude":-73.991165} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"14442", "name":"19th & Park", "street":"240 Park Avenue South", "city":"New York", "location" : { "latitude":40.73798, "longitude":-73.988303} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"13539", "name":"23rd btwn 5th & 6th", "street":"14 W. 23rd St", "city":"New York", "location" : { "latitude":40.741661, "longitude":-73.990313} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"7587", "name":"24th & 6th", "street":"750 6th Avenue", "city":"New York", "location" : { "latitude":40.743574, "longitude":-73.992028} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"5072", "name":"Fashion Inst of Technology", "street":"227 W 27th St", "city":"New York", "location" : { "latitude":40.747097, "longitude":-73.994576} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"7277", "name":"23rd & Park", "street":"304 Park Avenue South", "city":"New York", "location" : { "latitude":40.74016, "longitude":-73.986971} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"13538", "name":"28th & Seventh", "street":"315 Seventh Avenue", "city":"New York", "location" : { "latitude":40.746974, "longitude":-73.993323} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"7826", "name":"33rd & Tenth", "street":"450 W. 33rd Street", "city":"New York", "location" : { "latitude":40.753066, "longitude":-73.999424} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"7792", "name":"27th & Sixth", "street":"776 Avenue of the Americas", "city":"New York", "location" : { "latitude":40.745213, "longitude":-73.990874} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"7420", "name":"261 5th Avenue", "street":"261 Fifth Avenue", "city":"New York", "location" : { "latitude":40.745069, "longitude":-73.986861} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"9722", "name":"31st and Sixth Avenue", "street":"875 Sixth Ave", "city":"New York", "location" : { "latitude":40.748026, "longitude":-73.989251} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"7353", "name":"1 Penn Plaza", "street":"1 Penn Plaza", "city":"New York", "location" : { "latitude":40.752586, "longitude":-73.992834} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"7851", "name":"Penn Station LIRR", "street":"1 Penn Plaza Concourse Level", "city":"New York", "location" : { "latitude":40.752587, "longitude":-73.992835} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"13791", "name":"Penn Station LIRR #2", "street":"1 Penn Plaza, Concourse level", "city":"New York", "location" : { "latitude":40.752587, "longitude":-73.992835} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"7709", "name":"76th & Columbus", "street":"338 Columbus Avenue", "city":"New York", "location" : { "latitude":40.779982, "longitude":-73.977146} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"11765", "name":"Sixth & 58th", "street":"1411 Sixth Avenue", "city":"New York", "location" : { "latitude":40.764767, "longitude":-73.97709} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"10279", "name":"W. 56th & 6th", "street":"1380 Sixth Avenue", "city":"New York", "location" : { "latitude":40.763839, "longitude":-73.977174} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"7344", "name":"60th & Broadway-II", "street":"1841 Broadway", "city":"New York", "location" : { "latitude":40.768869, "longitude":-73.982343} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"7575", "name":"43rd St & Third Ave", "street":"685 Third Avenue", "city":"New York", "location" : { "latitude":40.751585, "longitude":-73.97359} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"13455", "name":"42nd & Second", "street":"220 East 42nd St", "city":"New York", "location" : { "latitude":40.749768, "longitude":-73.973188} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"7698", "name":"57th & Seventh", "street":"142 W. 57th Street", "city":"New York", "location" : { "latitude":40.764875, "longitude":-73.97897} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"7245", "name":"57th btwn 8th & 9th", "street":"322 West 57th Street", "city":"New York", "location" : { "latitude":40.767558, "longitude":-73.983086} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"7696", "name":"120 W. 56th", "street":"120 W. 56th Street", "city":"New York", "location" : { "latitude":40.76402, "longitude":-73.978898} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"8615", "name":"West 55th & 7th", "street":"870 7th Avenue", "city":"New York", "location" : { "latitude":40.764304, "longitude":-73.980913} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"9240", "name":"1345 Ave. of the Americas Acquisiti", "street":"1345 6th Ave", "city":"New York", "location" : { "latitude":40.76201, "longitude":-73.978533} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"2784", "name":"53rd & Sixth", "street":"1330 6th Ave.", "city":"New York", "location" : { "latitude":40.761933, "longitude":-73.978633} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"8965", "name":"51st between Park & Madison", "street":"45 E. 51st Street", "city":"New York", "location" : { "latitude":40.758144, "longitude":-73.974267} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"8992", "name":"54th & Broadway", "street":"1710 Broadway", "city":"New York", "location" : { "latitude":40.764046, "longitude":-73.98223} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"9239", "name":"1290 Ave. of the Americas", "street":"1290 6th Ave", "city":"New York", "location" : { "latitude":40.760757, "longitude":-73.979621} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"7673", "name":"52nd & Seventh", "street":"156 W 52nd St", "city":"New York", "location" : { "latitude":40.76183, "longitude":-73.981144} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"7424", "name":"48th & Park", "street":"280 Park Avenue", "city":"New York", "location" : { "latitude":40.756423, "longitude":-73.975073} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"11736", "name":"49th & Madison", "street":"425 Madison Avenue", "city":"New York", "location" : { "latitude":40.756963, "longitude":-73.975901} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"2785", "name":"47th & Eighth", "street":"770 Eighth Ave", "city":"New York", "location" : { "latitude":40.760626, "longitude":-73.987171} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"7443", "name":"Rockefeller Center Concourse", "street":"30 Rockefeller Plaza, Space A", "city":"New York", "location" : { "latitude":40.758759, "longitude":-73.978691} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"7501", "name":"52nd Street and 8th Ave", "street":"871 8th Avenue", "city":"New York", "location" : { "latitude":40.763701, "longitude":-73.985292} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"7444", "name":"Rockefeller Center Subway", "street":"30 Rockefeller Plaza", "city":"New York", "location" : { "latitude":40.758852, "longitude":-73.979169} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"15388", "name":"ARAMARK @ JP Morgan Chase New York", "street":"270 Park Ave", "city":"New York", "location" : { "latitude":40.75582, "longitude":-73.975683} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"7282", "name":"49th & 7th", "street":"750 Seventh Avenue", "city":"New York", "location" : { "latitude":40.760755, "longitude":-73.983693} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"7426", "name":"575 Fifth Avenue (Relocation)", "street":"575 Fifth Avenue", "city":"New York", "location" : { "latitude":40.756428, "longitude":-73.97853} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"7577", "name":"45th & Park Avenue", "street":"230 Park Avenue", "city":"New York", "location" : { "latitude":40.754532, "longitude":-73.976097} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"7663", "name":"90th & First", "street":"400 East 90th Street", "city":"New York", "location" : { "latitude":40.779119, "longitude":-73.947474} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"14218", "name":"103rd & Broadway", "street":"2690 Broadway", "city":"New York", "location" : { "latitude":40.798882, "longitude":-73.968372} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"7699", "name":"95th & Broadway", "street":"2521 Broadway", "city":"New York", "location" : { "latitude":40.793864, "longitude":-73.972744} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"7474", "name":"93rd and Broadway", "street":"2498 Broadway", "city":"New York", "location" : { "latitude":40.792522, "longitude":-73.973027} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"7534", "name":"B'way & 114 Street (New World)", "street":"2929 Broadway", "city":"New York", "location" : { "latitude":40.807108, "longitude":-73.96491} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"14498", "name":"Broadway btwn 87th & 88th", "street":"2394 Broadway", "city":"New York", "location" : { "latitude":40.789286, "longitude":-73.975324} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"7394", "name":"81st & Columbus", "street":"444 Columbus Avenue", "city":"New York", "location" : { "latitude":40.783633, "longitude":-73.974456} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"7557", "name":"50th & Second", "street":"943 Second Avenue", "city":"New York", "location" : { "latitude":40.755042, "longitude":-73.968613} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"7675", "name":"14th & Sixth", "street":"510 Sixth Avenue", "city":"New York", "location" : { "latitude":40.737022, "longitude":-73.996494} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"7407", "name":"92nd & 3rd", "street":"1642 Third Avenue", "city":"New York", "location" : { "latitude":40.782627, "longitude":-73.951431} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"9439", "name":"84th & Third Ave", "street":"1488 Third Avenue #A", "city":"New York", "location" : { "latitude":40.777492, "longitude":-73.955133} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"7465", "name":"2 Columbus Ave.", "street":"2 Columbus Avenue", "city":"New York", "location" : { "latitude":40.769313, "longitude":-73.984905} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"831", "name":"32nd @ 2nd Ave.", "street":"585 2nd Avenue", "city":"New York", "location" : { "latitude":40.743679, "longitude":-73.976866} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"7414", "name":"47th & 9th", "street":"682 9th Avenue", "city":"New York", "location" : { "latitude":40.761614, "longitude":-73.990046} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"11277", "name":"Lexington & 85th", "street":"1261 Lexington Avenue", "city":"New York", "location" : { "latitude":40.778649, "longitude":-73.956036} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"3421", "name":"West 23rd and 8th", "street":"300 W 23rd St.", "city":"New York", "location" : { "latitude":40.745103, "longitude":-73.998743} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"7558", "name":"96th & Madison", "street":"1378 Madison Avenue", "city":"New York", "location" : { "latitude":40.787139, "longitude":-73.954486} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"7858", "name":"17th & First (Stuyvesant Town)", "street":"286 First Avenue, A", "city":"New York", "location" : { "latitude":40.732401, "longitude":-73.981499} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"8770", "name":"80th & York", "street":"1515 York Avenue", "city":"New York", "location" : { "latitude":40.772414, "longitude":-73.949904} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"7261", "name":"Greenwich Avenue", "street":"93 Greenwich Avenue", "city":"New York", "location" : { "latitude":40.73744, "longitude":-74.001641} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"9467", "name":"43rd & Ninth", "street":"593 Ninth Ave", "city":"New York", "location" : { "latitude":40.758934, "longitude":-73.992514} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"7612", "name":"60th & First", "street":"1102 1st Ave", "city":"New York", "location" : { "latitude":40.760348, "longitude":-73.961138} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"7874", "name":"2138 Broadway", "street":"2140 Broadway", "city":"New York", "location" : { "latitude":40.781019, "longitude":-73.981072} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"7354", "name":"70th & Broadway", "street":"2045 Broadway", "city":"New York", "location" : { "latitude":40.777773, "longitude":-73.982557} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"819", "name":"Columbus @ 67th", "street":"152 - 154 Columbus Avenue", "city":"New York", "location" : { "latitude":40.774211, "longitude":-73.981356} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"821", "name":"Third @ 66th", "street":"1128 Third Avenue", "city":"New York", "location" : { "latitude":40.765935, "longitude":-73.963591} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"823", "name":"Lexington & 78th", "street":"1117 Lexington Ave., #4", "city":"New York", "location" : { "latitude":40.774115, "longitude":-73.959318} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"7672", "name":"85th & First", "street":"1631 1st Ave", "city":"New York", "location" : { "latitude":40.776103, "longitude":-73.949968} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"11423", "name":"Macy's 35th Street Balcony", "street":"151 W 34th Street", "city":"New York", "location" : { "latitude":40.750942, "longitude":-73.989757} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"8614", "name":"58th & 8th", "street":"4 Columbus Circle", "city":"New York", "location" : { "latitude":40.767539, "longitude":-73.983111} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"847", "name":"6th & Waverly (Waverly Place)", "street":"378 6th Avenue", "city":"New York", "location" : { "latitude":40.733037, "longitude":-73.99976} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"7507", "name":"33rd Street and 5th Avenue (Old Ban", "street":"334 Fifth Ave.", "city":"New York", "location" : { "latitude":40.747763, "longitude":-73.985306} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"818", "name":"Second @ 81st", "street":"1559 2nd Avenue", "city":"New York", "location" : { "latitude":40.774639, "longitude":-73.95433} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"7682", "name":"76th & Second", "street":"1449 Second Avenue", "city":"New York", "location" : { "latitude":40.771213, "longitude":-73.956828} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"7341", "name":"23rd & 3rd", "street":"296-300 Third Avenue", "city":"New York", "location" : { "latitude":40.738792, "longitude":-73.983401} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"7800", "name":"Grand Central Station 2", "street":"7800 Grand Central Station, Track 35", "city":"New York", "location" : { "latitude":40.752347, "longitude":-73.977456} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"7386", "name":"111th & Broadway", "street":"2853 Broadway", "city":"New York", "location" : { "latitude":40.804695, "longitude":-73.96667} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"1379", "name":"Target East River Plaza T-2380", "street":"517 E 117th St", "city":"New York", "location" : { "latitude":40.795688, "longitude":-73.932552} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"75491", "name":"Limited Brands-NYC", "street":"1740 Broadway", "city":"New York", "location" : { "latitude":40.765221, "longitude":-73.982023} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"75271", "name":"Javits Convention Ctr Taxi North", "street":"655 W 34th St", "city":"New York", "location" : { "latitude":40.756782, "longitude":-74.003423} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"9239", "name":"1290 Sixth Ave (Ave of Americas)", "street":"1290 Sixth Avenue", "city":"New York", "location" : { "latitude":40.760875, "longitude":-73.979729} }, { "_class":"example.springdata.mongodb.repo.Store", "id":"75846", "name":"Waldorf-Astoria", "street":"301 Park Ave", "city":"New York", "location" : { "latitude":40.75656, "longitude":-73.97405} } ]
2、初始化mongo数据
/** * 初始化mongo数据 * * @return */ public @Bean AbstractRepositoryPopulatorFactoryBean repositoryPopulator() { ObjectMapper mapper = new ObjectMapper(); // 封装JSON,让第一个类拥有第二个类的注释 mapper.addMixIn(GeoJsonPoint.class, GeoJsonPointMixin.class); mapper.configure(FAIL_ON_UNKNOWN_PROPERTIES, false); Jackson2RepositoryPopulatorFactoryBean factoryBean = new Jackson2RepositoryPopulatorFactoryBean(); factoryBean.setResources(new Resource[] { new ClassPathResource("starbucks-in-nyc.json") }); factoryBean.setMapper(mapper); return factoryBean; } static abstract class GeoJsonPointMixin { GeoJsonPointMixin(@JsonProperty("longitude") double x, @JsonProperty("latitude") double y) {} }
3、查询操作
@Autowired private MongoOperations mongoOps; @Autowired private StoreRepository repository; @Before public void setUp() { initGeo(); } @After public void destory(){ mongoOps.dropCollection(Venue.class); } /** * 地理坐标操作 */ @Test public void testGeoSpatial(){ // 圆形查找 Circle circle = new Circle(30, 20, 20); List<Venue> venues = mongoOps.find(new Query(Criteria.where("location").within(circle)), Venue.class); System.out.println(venues); System.out.println(venues.size()); // 球型查询 venues = mongoOps.find(new Query(Criteria.where("location").withinSphere(circle)), Venue.class); System.out.println(venues); System.out.println(venues.size()); // 矩形查找 Box box = new Box(new Point(10, 11), new Point(10, 20)); venues = mongoOps.find(new Query(Criteria.where("location").within(box)), Venue.class); System.out.println(venues); System.out.println(venues.size()); // 按距离由近到远查询 Point point = new Point(12, 12); venues = mongoOps.find(new Query(Criteria.where("location").near(point).maxDistance(20)), Venue.class); System.out.println(venues.size()); System.out.println(venues); // 空间距离查询 venues = mongoOps.find(new Query(Criteria.where("location").nearSphere(point).maxDistance(20)), Venue.class); System.out.println(venues.size()); System.out.println(venues); // 最近点查询 NearQuery query = NearQuery.near(point).maxDistance(new Distance(100000, Metrics.KILOMETERS)); GeoResults<Venue> result = mongoOps.geoNear(query, Venue.class); System.out.println(result); } /** * 使用GeoJSON和遗留格式调用它 */ @Test public void testGeoJSON(){ /* * { * "location": { * "$geoWithin": { * "$geometry": { * "type": "Polygon", * "coordinates": [ * [ * [-73.992514,40.758934], * [-73.961138,40.760348], * [-73.991658,40.730006], * [-73.992514,40.758934] * ] * ] * } * } * } * } */ List<Store> storeList = repository.findByLocationWithin( new GeoJsonPolygon( new Point(-73.992514, 40.758934), new Point(-73.961138, 40.760348), new Point(-73.991658, 40.730006), new Point(-73.992514, 40.758934))); System.out.println(storeList); System.out.println(storeList.size()); /* * { * "location" : { * "$geoWithin" : { * "$polygon" : [ [-73.992514,40.758934] , [-73.961138,40.760348] , [-73.991658,40.730006] ] * } * } * } */ storeList = repository.findByLocationWithin( new Polygon( new Point(-73.992514, 40.758934), new Point(-73.961138, 40.760348), new Point(-73.991658, 40.730006))); System.out.println(storeList); System.out.println(storeList.size()); } private void initGeo(){ for (int x = 100; x < 131; x++) { for (int y = 30; y < 61; y++) { Double loca[] = new Double[]{Double.valueOf(x), Double.valueOf(y)}; Venue venue = new Venue("venue" + Arrays.toString(loca),Double.valueOf(x), Double.valueOf(y)); mongoOps.insert(venue); } } }
这个网站也不错:https://blog.csdn.net/mazhen1991/article/details/78161784
原文链接:https://www.jianshu.com/p/1222c2a043ab