1.Windows上下载安装
默认安装后将bin目录加载到环境变量中
C:Program FilesMongoDBServer3.4bin
MongoDB中服务端和客户端都在同一个目录中,mongod.exe为服务程序,mongo为客户段程序
创建MongoDB数据目录 d:mongodbdata
启动mongodb服务程序:
mongod --dbpath d:\mongodata
客户端访问
mongo 127.0.0.1 //如果不指定ip,则会访问本地
####2.简介
mongodb是c++语言编写的非关系型数据库,但是在非关系型数据库中最像关系型数据库的。其是面向存储存储的数据库,主要概念有文档、集合和数据库等,是基于分布式文件存储的数据库,具有性能高、可扩展、易部署、易使用,存储数据方便等特点,其文件存储格式是bson格式,bson格式是json格式的扩展.
3.基本操作命令
use demo; //选择一个db,如果没有就创建然后进入
db;//显示当前db名称
show dbs; //查看所有db
db.demo.insert({"_id":"520","name":"xiaoxiao"});
//向demo库中插入一个文档,_id表示文档的唯一标识
db.demo.find(); //查询demo集合所有数据
db.demo.find();
db.demo.update({"_id":"520"},{$set:{"name":"xiaosun"}});
4.java操作mongodb
引入依赖
<dependencies>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver</artifactId>
<version>3.6.3</version>
</dependency>
</dependencies>
demo
package com.xiayu;
import com.mongodb.BasicDBObject;
import com.mongodb.MongoClient;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;
import java.util.HashMap;
import java.util.Map;
public class MongoTest {
public static void main(String[] args) {
MongoClient mongoClient = new MongoClient("127.0.0.1");
MongoDatabase demoDB = mongoClient.getDatabase("demo");
MongoCollection<Document> collection = demoDB.getCollection("demo");
// BasicDBObject basicDBObject = new BasicDBObject("name","xiaoxiao");
BasicDBObject basicDBObject = new BasicDBObject("_id","520");
FindIterable<Document> documents = collection.find(basicDBObject);
for (Document document:documents){
System.out.println(document.get("name"));
System.out.println(document.get("_id"));
}
//use demo;
//db.demoCollection.find("name":"xiaoxiao");
Map map = new HashMap<String, String>();
map.put("_id","100");
map.put("name","zhangsan");
map.put("age","100");
map.put("birthday","2020-01-01");
Document document = new Document(map);
collection.insertOne(document);
mongoClient.close();
}
}
5.使用spring-data-mongodb使用demo
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.xiayu</groupId>
<artifactId>mongodbdemo</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE </version>
</parent>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.12</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
</dependencies>
</project>
application.yml
server:
port: 9996
spring:
application: name: mongodbdemo
data:
mongodb: host: 127.0.0.1
database: demo
repository
package com.xiayu.dao;
import com.xiayu.entity.Remark;
import org.springframework.data.mongodb.repository.MongoRepository;
public interface RemarkDao extends MongoRepository<Remark,String> {
}
service
package com.xiayu.service.impl;
import com.xiayu.dao.RemarkDao;
import com.xiayu.entity.Remark;
import com.xiayu.service.IRemarkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Service("remarkServiceImpl")
@Transactional
public class RemarkServiceImpl implements IRemarkService {
@Autowired
private RemarkDao remarkDao;
public List<Remark> findAll(){
return remarkDao.findAll();
}
public Remark findRemarkById(String remarkId){
return remarkDao.findById(remarkId).get();
}
public boolean insertRemark(Remark remark){
remarkDao.insert(remark);
return true;
}
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。