MongoDB快速入门
2021-02-24 15:23:53
```shell
$ docker pull mongo:4.4
$ docker pull mongo-express
$ docker run -it --rm -v $PWD/mongo-data:/data/db \
-p 27017:27017 \
-e MONGO_INITDB_ROOT_USERNAME=root \
-e MONGO_INITDB_ROOT_PASSWORD=example \
mongo:4.4
$ docker run -it --rm \
-p 8081:8081 \
-e ME_CONFIG_OPTIONS_EDITORTHEME="ambiance" \
-e ME_CONFIG_MONGODB_SERVER="your-ip" \
-e ME_CONFIG_MONGODB_PORT=27017 \
-e ME_CONFIG_MONGODB_ADMINUSERNAME="root" \
-e ME_CONFIG_MONGODB_ADMINPASSWORD="example" \
-e ME_CONFIG_BASICAUTH_USERNAME="admin" \
-e ME_CONFIG_BASICAUTH_PASSWORD="123456" \
mongo-express
```
将`your-ip`替换成你运行Docker的机器的IP。如果是在macOS下,替换为`docker.for.mac.host.internal`
```
http://your-ip:8081/
用户名:admin
密码:123456
```
Spring Boot中操作MongoDB,先添加pom依赖
```xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
```
application配置文件
```
spring.data.mongodb.uri=mongodb://root:example@127.0.0.1:27017/demo?authSource=admin&authMechanism=SCRAM-SHA-1
```
Java类
```java
package com.example;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;
@Service
public class BookService {
@Autowired
private MongoTemplate mongoTemplate;
/**
* 保存对象
*/
public void saveObj(Book book) {
book.setCreateTime(new Date());
mongoTemplate.save(book);
}
/**
* 查询所有
*/
public List<Book> findAll() {
return mongoTemplate.findAll(Book.class);
}
/**
* 分页
*/
public List<Book> findAll(int skip, int limit) {
Query query = new Query();
query.skip(skip).limit(limit);
return mongoTemplate.find(query, Book.class);
}
/***
* 根据id查询
*/
public Book getBookById(String id) {
Query query = new Query(Criteria.where("_id").is(id));
return mongoTemplate.findOne(query, Book.class);
}
/**
* 根据名称查询
*/
public Book getBookByName(String name) {
Query query = new Query(Criteria.where("name").is(name));
return mongoTemplate.findOne(query, Book.class);
}
/**
* 更新对象
*/
public String updateBook(Book book) {
Query query = new Query(Criteria.where("_id").is(book.getId()));
Update update = new Update().set("name", book.getName())
.set("updateTime", new Date());
//updateFirst 更新查询返回结果集的第一条
mongoTemplate.updateFirst(query, update, Book.class);
//updateMulti 更新查询返回结果集的全部
// mongoTemplate.updateMulti(query,update,Book.class);
//upsert 更新对象不存在则去添加
// mongoTemplate.upsert(query,update,Book.class);
return "success";
}
/***
* 删除对象
*/
public void deleteBook(Book book) {
mongoTemplate.remove(book);
}
/**
* 根据id删除
*/
public void deleteBookById(String id) {
Book book = getBookById(id);
deleteBook(book);
}
}
```
```java
@Autowired
BookService bookService;
@GetMapping("/create")
public String demo() {
Book book = new Book();
book.setId("1");
book.setName("book1");
bookService.saveObj(book);
Book book2 = new Book();
// book2.setId("2"); // 不指定id,自动生成
book2.setName("book2");
bookService.saveObj(book2);
return "ok";
}
```
```java
package com.example;
import org.springframework.data.annotation.Id;
import java.util.Date;
public class Book {
@Id
private String id; // 将id关联到 mongo的 _id
private Integer price;
private String name;
private Date createTime;
private Date updateTime;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public Integer getPrice() {
return price;
}
public void setPrice(Integer price) {
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public Date getUpdateTime() {
return updateTime;
}
public void setUpdateTime(Date updateTime) {
this.updateTime = updateTime;
}
}
```