MongoDB简单使用
介绍
MongoDB 是一个免费的开源跨平台面向文档的 NoSQL 数据库。
安装
docker pull mongo
下载最新版本的镜像
docker run -d --name mongo -e MONGO_INITDB_ROOT_USERNAME=root -e MONGO_INITDB_ROOT_PASSWORD=xxx123 -p 27017:27017 mongo
设置初始账号和密码,注意开启防火墙对27017端口的限制
使用
windows客户端下载
MongoDB Shell Download
mongosh --host=ip --port=27017 -u=root -p=xxx123
增删改查
show dbs //显示所有数据库,如果db中没有数据,不显示,
use test_info //切换到test_info,db不存在就创建
db.movies.insertOne({title:"The First",year:2020}) //插入一条记录,会自动添加 _id 字段
db.movies.insertMany([{title:"The First",year:2020}]) //插入多条记录
db.movies.find({title:{$regex:"The"})//数据查询,正则表达式匹配,类似mysql的like
db.movies.find({year:{$gt:2010})//查询年份大于2010的电影
db.movies.find({year:2010})//查询年份等于2010的电影 相当于 db.movies.find({year:{$eq:2010}})
db.movies.updateOne({year:2020},{$set:{title:"The2"}}) //查询year为2020的记录(匹配到的第一条),title更新为The2
db.movies.updateMany({year:2020},{$set:{title:"The2"}}) //查询year为2020的记录(匹配到的多条),title更新为The2
db.movies.deleteOne({year:2020}) //删除year为2020的匹配到的第一条
db.movies.deleteMany({year:2020}) //删除year为2020的匹配到的多条
show collections //查询表
db类似mysql的数据库,不存在就创建
movies类似mysql的表,不存在就创建
用户及权限
use test_info
db.createUser({user:"root2",pwd:"xxx123",roles:[{role:"dbOwner",db:"test_info"},{role:"dbOwner",db:"admin"}]}) //在当前库(test_info)下创建用户及授权
show users //显示当前库下的用户
哪个库创建的用户必须在哪个库做认证
mongosh --host=ip --port=27017 -u=root2 -p=xxx123 admin //Authentication failed. 因为root2是在test_info库下创建的
mongosh mongodb://root2:xxx123@ip:27017/admin //Authentication failed. 因为root2是在test_info库下创建的
mongosh mongodb://root2:xxx123@ip:27017/test_info //Authentication Successfully.
Java客户端
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver-sync</artifactId>
<version>4.8.2</version>
</dependency>
import com.mongodb.BasicDBObject;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Filters;
import org.bson.Document;
import org.bson.conversions.Bson;
import java.util.Collections;
public class TestMongoDBClient {
public static void main(String[] args) {
// testDelete();
testFind();
}
private static void testDelete() {
MongoCollection<Document> mongoCollection = mongoCollection();
BasicDBObject queryObj = new BasicDBObject();
queryObj.put("year", new BasicDBObject("$gt", 2020));
mongoCollection.deleteOne(queryObj);
}
private static void testUpdate() {
MongoCollection<Document> mongoCollection = mongoCollection();
BasicDBObject queryObj = new BasicDBObject();
queryObj.put("year", new BasicDBObject("$gt", 2020));
BasicDBObject setObj = new BasicDBObject();
setObj.put("$set", new BasicDBObject("title", "The First22"));
mongoCollection.updateMany(queryObj, setObj);
}
private static void testInsert() {
MongoCollection<Document> mongoCollection = mongoCollection();
Document document = new Document();
document.put("title", "THe fisrt");
document.put("year", 2023);
mongoCollection.insertMany(Collections.singletonList(document));
}
private static void testFind() {
MongoCollection<Document> mongoCollection = mongoCollection();
//两种拼接查询条件的方式,第一种
BasicDBObject queryObj = new BasicDBObject();
queryObj.put("year", new BasicDBObject("$gt", 2020));
//两种拼接查询条件的方式,第二种
Bson filter = Filters.gte("year", 2020);
FindIterable<Document> findIterable = mongoCollection.find(filter);
for (Document document : findIterable) {
System.out.println(document);
}
}
private static MongoCollection<Document> mongoCollection() {
MongoClient mongoClient = mongoClient();
MongoDatabase mongoDatabase = mongoClient.getDatabase("test_info");
return mongoDatabase.getCollection("movies");
}
private static MongoClient mongoClient() {
//mongodb://[username:password@]host[/[database][?options]]
return MongoClients.create("mongodb://root2:xxx123@ip/test_info");
}
}