# Elab-mongodb 使用介绍
## pom.xml
```xml
<dependency>
	<groupId>com.elab.core</groupId>
	<artifactId>elab-mongodb</artifactId>
	<!-- 这里根据实际情况定义版本号 -->
	 <version>2.0.4.4</version>
</dependency>
```

## 配置类
```java
@Configuration
public class MongoBeanConfig {

    @Value("${mongodb.host}")
    private String mongodbHost;
  @Value("${mongodb.port}")
    private int mongdbPort;
  @Value("${mongodb.database.name}")
    private String mongodbDataBaseName;

  @Bean
  public MongoTemplate MongoTemplate() throws UnknownHostException {
        MongoTemplate mongoTemplate = new MongoTemplate(new SimpleMongoDbFactory(new MongoClient(mongodbHost, mongdbPort),
  mongodbDataBaseName));
 return mongoTemplate;
  }

}

```

## 配置文件中需要制定mongodb的数据源
```tex
mongodb.host=
mongodb.port=
mongodb.database.name=
```

## 代码层的使用:

### dao层
```java
/**
 * * @author Liukx
 * @create 2018-05-03 15:41
 * @email liukx@elab-plus.com
 **/public class UserMongoDB extends BaseMongodb<具体实体对象> {

    @Override
  public Class getEntity() {
        return User.class;
  }

  // 如果是针对名称操作的,重写collectionNamed的方法 针对对应的库名做映射,如果一开始就是对象操作的,则不用重写
  @Override
  protected String getCollectionName() {
	  return "collectionName";
  }

  @Autowired
  private MongoTemplate mongoTemplate;

  @Override
  public MongoTemplate getMongoTemplate() {
        return mongoTemplate;
  }
}
```

### service层:
```java

@Autowired
UserMongoDB mongoDB;


User user = new User("uuu", "123", "7");
User user1 = new User("jjj", "123", "8");
User user2 = new User("kkkk", "123", "9");
List list = new ArrayList<>();
list.add(user);
list.add(user1);
list.add(user2);
mongoDB.batchSave(list);

```

# 拓展
### 针对GEO操作


- 针对box操作
- ![imagepng](http://oub49vfnw.bkt.clouddn.com//file/2018/05/dc11eaa388e1427baa2d48247f45644a_image.png)

```java

  // 传入两个坐标
  Point point = new Point(121.591528, 21.591528);
  Point point1 = new Point(121.572344, 21.572344);
  Box box = new Box(point,point1);
  // 指定字段查询
  Criteria coordinate = Criteria.where("字段名").within(box);
  Query query = Query.query(coordinate);
  List list = getMongoTemplate().find(query, getEntity(), getCollectionName());

```
- 针对圆角范围查询

![imagepng](http://oub49vfnw.bkt.clouddn.com//file/2018/05/71861c2bc6e746c1ae2124704f68fc21_image.png)



```java
Point point = new Point(121.591528, 21.591528);
double radius = 6378137 / 3000;
Sphere sphere = new Sphere(point, radius);
Criteria coordinate = Criteria.where("字段名").within(sphere);
Query query = Query.query(coordinate);
List list = getMongoTemplate().find(query, getEntity(), getCollectionName());

```

- 针对中心范围查询

![imagepng](http://oub49vfnw.bkt.clouddn.com//file/2018/05/c29b3f894c8b4ef0ba112ecf403669d1_image.png)


```java

Point point = new Point(121.591528, 21.591528);
double radius = 6378137 / 3000;
Circle sphere = new Circle(point, radius);
Criteria coordinate = Criteria.where("字段名").within(sphere);
Query query = Query.query(coordinate);
List list = getMongoTemplate().find(query, getEntity(), getCollectionName());
System.out.println(list.size());

```


- 针对多个坐标点查询

![imagepng](http://oub49vfnw.bkt.clouddn.com//file/2018/05/3e479431da0f470c93b4beb5f6c2fe7f_image.png)



```java
Point point = new Point(121.591528, 21.591528);
Point point1 = new Point(121.591528, 21.591528);
Point point2 = new Point(121.591528, 21.591528);

List addressList = new ArrayList<>();
addressList.add(point);
addressList.add(point1);
addressList.add(point2);


Polygon sphere = new Polygon(addressList);
Criteria coordinate = Criteria.where("coordinate").within(sphere);
Query query = Query.query(coordinate);
List list = getMongoTemplate().find(query, getEntity(), getCollectionName());
System.out.println(list.size());
```



