Mongodb 笔记采坑
1 比较 数字大小用的是字符串的话,需要也转为 字符串
2 所有的类型
Type | Number | Alias | Notes |
---|---|---|---|
Double | 1 | “double” | |
String | 2 | “string” | |
Object | 3 | “object” | |
Array | 4 | “array” | |
Binary data | 5 | “binData” | |
Undefined | 6 | “undefined” | Deprecated. |
ObjectId | 7 | “objectId” | |
Boolean | 8 | “bool” | |
Date | 9 | “date” | |
Null | 10 | “null” | |
Regular Expression | 11 | “regex” | |
DBPointer | 12 | “dbPointer” | Deprecated. |
JavaScript | 13 | “javascript” | |
Symbol | 14 | “symbol” | Deprecated. |
JavaScript (with scope) | 15 | “javascriptWithScope” | |
32-bit integer | 16 | “int” | |
Timestamp | 17 | “timestamp” | |
64-bit integer | 18 | “long” | |
Decimal128 | 19 | “decimal” | New in version 3.4. |
Min key | -1 | “minKey” | |
Max key | 127 | “maxKey” |
3 启动服务命令
nohup ./mongod --port 27071 --dbpath data/db --fork --logpath log/mongo.log
不加 --fork 关闭服务台自动关闭的会--fork 为了关闭客户端是不退出
但加了--fork就需要加 logpath, 否则 失败
3 mongodb 集群分片副本集搭建
临时关闭大内存页面的方法
echo never >> /sys/kernel/mm/transparent_hugepage/enabled
echo never >> /sys/kernel/mm/transparent_hugepage/defrag
下载 redhat 4.0
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.0.10.tgz
更新系统时间
/usr/sbin/ntpdate ntp1.aliyun.com
cd /opt
为目录创建超链接
ln -s mongodb-linux-x86_64-rhel70-4.0.10 mongodb
修改文件的组名和用户名,用户名在前
chown -R root.root mongodb
chown -R root.root /data/mongodb
查看用户组 groups
查看用户组 groups root (root:root)
为添加的用户指定相应的用户组
# useradd -g root tt
ln -s mongodb-linux-x86_64-rhel70-4.0.10 mongodb
添加用户修改密码的方法
1、在root下添加用户用 adduser 命令
# 添加用户 admin
[root@flm] #sudo adduser admin
2、添加用户登录密码
# 为用户 admin 修改密码
[root@flm] #sudo passwd admin
然后输入密码就可以了
分类: Linux
覆盖文件的方法,多行的数据
## content
systemLog:
destination: file
logAppend: true
path: /data/mongodb/config/log/config.log
# Where and how to store data.
storage:
dbPath: /data/mongodb/config/data
journal:
enabled: true
# how the process runs
processManagement:
fork: true
pidFilePath: /data/mongodb/config/log/configsrv.pid
# network interfaces
net:
port: 21000
bindIp: localhost
#operationProfiling:
replication:
replSetName: config
sharding:
clusterRole: configsvr
rs.initiate({_id:"cfgReplSet",configsvr:true,members:[{_id:0,host:"192.168.150.129:21000"},{_id:1,host:"192.168.150.130:21000"},{_id:2,host:"192.168.150.131:21000"}]})
竟然 mongodb 一般放在了mongodb的目录下了
分配大内存方式启动 命令
对于一口气吃掉内存大半的MongoDB,我的配置是:
# numactl --interleave=all mongod -f /etc/mongod.conf
1
即分配所有的node供其使用,这也是官方推荐的用法。
numastat 查看内存状态
yum install numactl
配置集群前需要先启动 mongodb的服务
numactl --interleave=all /opt/mongodb/bin/mongod -f /data/mongodb/config/mongo.conf
4.0变化,下面两个都去掉
启动服务是无法识别 rest=true,需要去掉才可以
不识别 httpinterface 也要去掉 。。。。。
numactl --interleave=all /opt/mongodb/bin/mongo --host localhost --port 21000
启动后添加副本集连不上,我猜测是因为另外两个没有启动造成的,需要把另外两个也安装号才可以
rs.initiate({_id:"cfgReplSet",configsvr:true,members:[{_id:0,host:"127.0.0.1:21000"},{_id:1,host:"127.0.0.1:31000"},{_id:2,host:"127.0.0.1:41000"}]})
创建服务的顺序
先 config,然后 分片 shard,最后 mongos
因为机器数码的限制,目前3个机器也变成了一个
目前
mongos 20000 30000 40000
config 21000 31000 41000
shard1 22000 32000 42000
开始配置 shard1 EOF 输入后自动结束 记得 cat >后面没有空格
cat >/data/mongodb/shard1/mongo.conf <<"EOF"
dbpath=/data/mongodb/shard1/data
logpath=/data/mongodb/shard1/log/shard1.log
logappend=true
port=22001
fork=true
replSet=shard1ReplSet
shardsvr=true
journal=false
启动失败因为上次一场关闭了
[root@VM_0_10_centos bin]# find / -name mongod.lock
/data/mongodb/shard1/data/mongod.lock
/data/mongodb/config/data/mongod.lock
//设置时间和 基础特殊查询 if(startdate==0||enddate==0){ // enddate = new Date().getTime(); // startdate = enddate = 24*7*60*60*1000; LocalDateTime now = LocalDateTime.now(); LocalDateTime bef = now.minusHours(24*7); enddate = Date.from(now.atZone(ZoneId.systemDefault()).toInstant()).getTime(); startdate = Date.from(bef.atZone(ZoneId.systemDefault()).toInstant()).getTime(); } BasicDBObject condition = new BasicDBObject(); condition.put("aaaa",new BasicDBObject("$lte",enddate).append("$gte",startdate)); condition.put("bbb",statistic.getFloor()); condition.put("loadClass",new BasicDBObject("$in",getLoads(statistic.getLoadclasses()))); BasicDBObject resultFields=new BasicDBObject(); resultFields.put("timestamp",1); resultFields.put("floor",1); resultFields.put(Constants.map.get(statistic.getDrivingsituation()), 1); Query query=new BasicQuery(condition.toJson(),resultFields.toJson());
查看当前目录下所有目录的占用大小
du -h --max-depth=1
查看文件大小 结果用MK这些单位来表示
ls -alh
后台启动默认数据库路径, 记得改端口 不能有--fork
nohup ./mongod --port 27071 --dbpath data/db &
这句只适用于 自动安装的
sudo service mongod stop
如果是配置不是 shard 使用命令会失败 ,因为不是 master/slave
关闭服务的方法
> use admin;
switched to db admin
> db.shutdownServer();
8
添加新用户的root和sudo权限
新建用户:
useradd username
修改密码:
passwd username
根据提示重复输入两遍userpwd.
赋予sudo权限:
visudo
找到 root ALL=(ALL)ALL
在下面添加 username ALL=(ALL)ALL
赋予root权限,苏四日没作用
usermod -a -G root mary.lee
切换用户命令
su gene
9 配置环境变量,将会非常简洁
vim /etc/profile
# 内容
export MONGODB_HOME=/opt/mongodb
export PATH=$MONGODB_HOME/bin:$PATH
# 使立即生效,在安装用户下(youknow)执行
source /etc/profile
10 分片的副本原则,然后启动
只改4个文件, 副本的都不变,除了路径和端口
此时原则是,第一个分片副本是
shard1 shard2 shard3 分别端口是22000 32000 42000
numactl --interleave=all mongod --config /data/mongodb/shard2/mongo.conf
numactl --interleave=all mongo localhost:32000
numactl --interleave=all mongod --config /data/mongodb/shard3/mongo.conf
numactl --interleave=all mongo localhost:42000
mongo localhost:22000
#使用admin数据库
use admin
#定义副本集配置
config = {
_id : "shard1",
members : [
{_id : 0, host : "localhost:22000" },
{_id : 1, host : "localhost:32000" },
{_id : 2, host : "localhost:42000" }
]
}
#初始化副本集配置
rs.initiate(config);
#查看分区状态
rs.status();
配置第二个副本集
#查看分区状态
rs.status();
numactl --interleave=all mongod --config /data/mongodb/shard12/mongo.conf
numactl --interleave=all mongo localhost:22001
numactl --interleave=all mongod --config /data/mongodb/shard22/mongo.conf
numactl --interleave=all mongo localhost:32001
numactl --interleave=all mongod --config /data/mongodb/shard32/mongo.conf
numactl --interleave=all mongo localhost:42001
use admin
#定义副本集配置
config = {
_id : "shard2",
members : [
{_id : 3, host : "localhost:22001" },
{_id : 4, host : "localhost:32001" },
{_id : 5, host : "localhost:42001" }
]
}
记得mongo.conf配置再 data/mongodb/shard1/下面
配置需要改3个地方才行
config = {
_id : "shard3",
members : [
{_id : 6, host : "localhost:22002" },
{_id : 7, host : "localhost:32002" },
{_id : 8, host : "localhost:42002" }
]
}
启动和查看config的 服务
numactl --interleave=all mongod --config /data/mongodb/config/mongo.conf
numactl --interleave=all mongo localhost:21000
启动和查看mongos的 服务
numactl --interleave=all mongod --config /data/mongodb/mongos/mongo.conf
numactl --interleave=all mongo localhost:20000
配置mongos失败了
systemLog:
destination: file
logAppend: true
path: /data/mongodb/mongos/log/mongos.log
processManagement:
fork: true
# pidFilePath: /data/mongodb/mongos/log/mongos.pid
# network interfaces
net:
port: 20000
bindIp: localhost
#监听的配置服务器,只能有1个或者3个 configs为配置服务器的副本集名字
sharding:
configDB: config/localhost:21000
PHP MongoDB Unrecognized option: sharding.configDB 解决方案
内存剩余liunx命令查看
free -m
11 mongodb 的 存儲 下載文件到 數據庫中的方法,以及普通的 mongodb的 增刪改查的方法
import com.mongodb.Block;
import com.mongodb.DBObject;
import com.mongodb.client.gridfs.GridFSBucket;
import com.mongodb.client.gridfs.GridFSDownloadStream;
import com.mongodb.client.gridfs.GridFSFindIterable;
import com.mongodb.client.gridfs.model.GridFSFile;
import com.lvmoney.common.exceptions.BusinessException;
import com.lvmoney.common.exceptions.CommonException;
import com.lvmoney.mongo.service.BaseGridFsService;
import com.lvmoney.mongo.vo.BaseGridFsByteOutVo;
import com.lvmoney.mongo.vo.BaseGridFsOutVo;
import com.lvmoney.mongo.vo.BaseGridFsQueryVo;
import com.lvmoney.mongo.vo.BaseGridFsVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.gridfs.GridFsResource;
import org.springframework.data.mongodb.gridfs.GridFsTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
/**
* @describe:
* @author: lvmoney /xxxx科技有限公司
* @version:v1.0 2019年1月10日 上午11:54:42
*/
@Service("frameBaseGridFsService")
public class BaseGridFsServiceImpl implements BaseGridFsService {
private final static Logger logger = LoggerFactory.getLogger(BaseGridFsServiceImpl.class);
@Autowired
GridFsTemplate gridFsTemplate;
@Autowired
GridFSBucket gridFSBucket;
@Override
public BaseGridFsOutVo save(BaseGridFsVo baseGridFsVo) {
long maxSize = baseGridFsVo.getMaxSize();
MultipartFile file = baseGridFsVo.getFile();
long fileSize = file.getSize();
if (maxSize < file.getSize()) {
throw new BusinessException(CommonException.Proxy.GRIDFS_FILE_SIZE);
}
String baseFileName = baseGridFsVo.getFileName();
String fileName = StringUtils.isBlank(baseFileName) ? file.getOriginalFilename() : baseFileName;
// 获得文件类型
String contentType = file.getContentType();
// 获得文件输入流
InputStream ins;
try {
ins = file.getInputStream();
// 将文件存储到mongodb中,mongodb 将会返回这个文件的具体信息
DBObject dbObj = baseGridFsVo.getDbObj();
ObjectId objectId = gridFsTemplate.store(ins, fileName, contentType, dbObj);
BaseGridFsOutVo result = new BaseGridFsOutVo();
result.setFileName(fileName);
result.setFileType(contentType);
result.setMongoFileId(objectId.toString());
result.setSize(fileSize);
return result;
} catch (IOException e) {
logger.error("文件名为:{},文件类型为:{},保存文件报错:{}", fileName, contentType, e.getMessage());
throw new BusinessException(CommonException.Proxy.GRIDFS_SAVE_ERROR);
}
}
@Override
public List<BaseGridFsOutVo> batchSave(List<BaseGridFsVo> baseGridFsVos) {
List<BaseGridFsOutVo> result = new ArrayList<>();
baseGridFsVos.forEach(baseGridFsService -> {
BaseGridFsOutVo baseGridFsOutVo = this.save(baseGridFsService);
result.add(baseGridFsOutVo);
});
return result;
}
@Override
public BaseGridFsByteOutVo getByMongoId(BaseGridFsQueryVo baseGridFsQueryVo) {
String fileId = baseGridFsQueryVo.getMongoFileId();
Query query = Query.query(Criteria.where("_id").is(fileId));
GridFSFile gridFSFile = gridFsTemplate.findOne(query);
if (gridFSFile == null) {
throw new BusinessException(CommonException.Proxy.GRIDFS_QUERY_FILE_NOT_EXSIT);
}
String fileName = gridFSFile.getFilename();
// 打开下载流对象
GridFSDownloadStream gridFS = gridFSBucket.openDownloadStream(gridFSFile.getObjectId());
// 创建gridFsSource,用于获取流对象
GridFsResource gridFsResource = new GridFsResource(gridFSFile, gridFS);
try {
BaseGridFsByteOutVo result = new BaseGridFsByteOutVo();
result.setFileByte(IOUtils.toByteArray(gridFsResource.getInputStream()));
result.setFileName(fileName);
return result;
} catch (IllegalStateException | IOException e) {
logger.error("通过_id{}获得文件报错:{}", fileId, e.getMessage());
throw new BusinessException(CommonException.Proxy.GRIDFS_QUERY_FILE_ERROR);
}
}
@Override
public List<BaseGridFsByteOutVo> batchGetByMongoId(List<BaseGridFsQueryVo> baseGridFsQueryVos) {
Criteria criteria = null;
String key = "_id";
for (int i = 0; i < baseGridFsQueryVos.size(); i++) {
String fileId = baseGridFsQueryVos.get(i).getMongoFileId();
if (i == 0) {
criteria = Criteria.where(key).is(fileId);
} else {
criteria.and(key).is(fileId);
}
}
Query query = Query.query(criteria);
GridFSFindIterable gridFSFiles = gridFsTemplate.find(query);
List<BaseGridFsByteOutVo> result = new ArrayList<>();
gridFSFiles.forEach((Block<? super GridFSFile>) gridFSFile -> {
String fileId = gridFSFile.getObjectId().toString();
BaseGridFsByteOutVo baseGridFsByteOutVo = new BaseGridFsByteOutVo();
GridFSDownloadStream gridFS = gridFSBucket.openDownloadStream(gridFSFile.getObjectId());
// 创建gridFsSource,用于获取流对象
GridFsResource gridFsResource = new GridFsResource(gridFSFile, gridFS);
try {
String fileName = gridFSFile.getFilename();
baseGridFsByteOutVo.setFileByte(IOUtils.toByteArray(gridFsResource.getInputStream()));
baseGridFsByteOutVo.setFileName(fileName);
result.add(baseGridFsByteOutVo);
} catch (IllegalStateException | IOException e) {
logger.error("通过_id{}获得文件报错:{}", fileId, e.getMessage());
throw new BusinessException(CommonException.Proxy.GRIDFS_QUERY_FILE_ERROR);
}
});
return result;
}
@Override
public void deleteByMongoId(BaseGridFsQueryVo baseGridFsQueryVo) {
gridFsTemplate.delete(new Query().addCriteria(Criteria.where("_id").is(baseGridFsQueryVo.getMongoFileId())));
}
}
import com.lvmoney.common.exceptions.BusinessException;
import com.lvmoney.common.exceptions.CommonException;
import com.lvmoney.mongo.service.BaseMongoService;
import com.lvmoney.mongo.vo.BaseMongoCollective;
import com.lvmoney.mongo.vo.BaseMongoVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
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.List;
/**
* @describe:
* @author: lvmoney /xxxx科技有限公司
* @version:v1.0 2019年1月10日 上午9:23:02
*/
@Service("frameBaseMongoService")
public class BaseMongoServiceImpl implements BaseMongoService {
@Autowired
private MongoTemplate mongoTemplate;
public void save(BaseMongoVo baseVo) {
mongoTemplate.save(baseVo.getData());
}
/**
* @param baseVo data mongo的mo collectionName 集合名
* @param baseVo 2019年1月10日上午10:01:11
* @describe:指定集合保存数据对象
* @author: lvmoney /xxxx科技有限公司
*/
public void saveByCollectionName(BaseMongoVo baseVo) {
mongoTemplate.save(baseVo.getData(), baseVo.getCollectionName());
}
public void remove(BaseMongoVo baseVo) {
mongoTemplate.remove(baseVo.getData());
}
public void removeByCollectionName(BaseMongoVo baseVo) {
mongoTemplate.remove(baseVo.getData(), baseVo.getCollectionName());
}
public void removeById(BaseMongoVo baseVo) {
Criteria criteria = Criteria.where(baseVo.getKey()).is(baseVo.getData());
criteria.and(baseVo.getKey()).is(baseVo.getData());
Query query = Query.query(criteria);
mongoTemplate.remove(query, baseVo.getCollectionName());
}
public void updateFirst(BaseMongoCollective baseCollection) {
String accordingKey = baseCollection.getKey();// 修改条件 key
String[] updateKeys = baseCollection.getKeys();// 修改内容 key数组
Object accordingValue = baseCollection.getData();// 修改条件 value
Object[] updateValues = baseCollection.getDatas();// 修改内容 value数组
Criteria criteria = Criteria.where(accordingKey).is(accordingValue);
Query query = Query.query(criteria);
Update update = new Update();
for (int i = 0; i < updateKeys.length; i++) {
update.set(updateKeys[i], updateValues[i]);
}
mongoTemplate.updateFirst(query, update, baseCollection.getCollectionName());
}
public void updateMulti(BaseMongoCollective baseCollection) {
String accordingKey = baseCollection.getKey();
Object accordingValue = baseCollection.getData();
String[] updateKeys = baseCollection.getKeys();
Object[] updateValues = baseCollection.getDatas();
Criteria criteria = Criteria.where(accordingKey).is(accordingValue);
Query query = Query.query(criteria);
Update update = new Update();
for (int i = 0; i < updateKeys.length; i++) {
update.set(updateKeys[i], updateValues[i]);
}
mongoTemplate.updateMulti(query, update, baseCollection.getCollectionName());
}
public List<?> find(BaseMongoCollective baseCollection) {
Object baseVo = baseCollection.getData();
String[] findKeys = baseCollection.getKeys();
Object[] findValues = baseCollection.getDatas();
Criteria criteria = null;
for (int i = 0; i < findKeys.length; i++) {
if (i == 0) {
criteria = Criteria.where(findKeys[i]).is(findValues[i]);
} else {
criteria.and(findKeys[i]).is(findValues[i]);
}
}
Query query = Query.query(criteria);
List<?> resultList = mongoTemplate.find(query, baseVo.getClass());
return resultList;
}
public List<?> findByCollectionName(BaseMongoCollective baseCollection) {
Object baseVo = baseCollection.getData();
String[] findKeys = baseCollection.getKeys();
Object[] findValues = baseCollection.getDatas();
String collectionName = baseCollection.getCollectionName();
Criteria criteria = null;
for (int i = 0; i < findKeys.length; i++) {
if (i == 0) {
criteria = Criteria.where(findKeys[i]).is(findValues[i]);
} else {
criteria.and(findKeys[i]).is(findValues[i]);
}
}
Query query = Query.query(criteria);
List<?> resultList = mongoTemplate.find(query, baseVo.getClass(), collectionName);
return resultList;
}
public List<?> sortFindByCollectionName(BaseMongoCollective baseCollection) {
String sortType = baseCollection.getSortType().toLowerCase();
if (StringUtils.isBlank(sortType)) {
throw new BusinessException(CommonException.Proxy.MONGO_SORT_TYPE_IS_REQUIRED);
}
if (!"desc".equals(sortType) || !"asc".equals(sortType)) {
throw new BusinessException(CommonException.Proxy.MONGO_SORT_TYPE_IS_ERROR);
}
Object baseVo = baseCollection.getData();
String[] findKeys = baseCollection.getKeys();
Object[] findValues = baseCollection.getDatas();
String collectionName = baseCollection.getCollectionName();
String sort = baseCollection.getSort();
Criteria criteria = null;
for (int i = 0; i < findKeys.length; i++) {
if (i == 0) {
criteria = Criteria.where(findKeys[i]).is(findValues[i]);
} else {
criteria.and(findKeys[i]).is(findValues[i]);
}
}
Query query = Query.query(criteria);
if (sortType.equals("desc")) {
query.with(new Sort(Direction.DESC, sort));
}
if (sortType.equals("asc")) {
query.with(new Sort(Direction.ASC, sort));
}
List<?> resultList = mongoTemplate.find(query, baseVo.getClass(), collectionName);
return resultList;
}
public Object findOne(BaseMongoCollective baseCollection) {
String[] findKeys = baseCollection.getKeys();
Object[] findValues = baseCollection.getDatas();
String collectionName = baseCollection.getCollectionName();
Criteria criteria = null;
for (int i = 0; i < findKeys.length; i++) {
if (i == 0) {
criteria = Criteria.where(findKeys[i]).is(findValues[i]);
} else {
criteria.and(findKeys[i]).is(findValues[i]);
}
}
Query query = Query.query(criteria);
return mongoTemplate.findOne(query, baseCollection.getData().getClass());
}
public Object findFirstOne(BaseMongoCollective baseCollection) {
Object baseVo = baseCollection.getData();
String[] findKeys = baseCollection.getKeys();
Object[] findValues = baseCollection.getDatas();
String collectionName = baseCollection.getCollectionName();
Criteria criteria = null;
for (int i = 0; i < findKeys.length; i++) {
if (i == 0) {
criteria = Criteria.where(findKeys[i]).is(findValues[i]);
} else {
criteria.and(findKeys[i]).is(findValues[i]);
}
}
Query query = Query.query(criteria);
return mongoTemplate.findOne(query, baseVo.getClass(), collectionName);
}
public List<?> findAll(BaseMongoVo baseVo) {
List<?> resultList = mongoTemplate.findAll(baseVo.getData().getClass());
return resultList;
}
public List<?> findAllByCollectionName(BaseMongoVo baseVo) {
List<?> resultList = mongoTemplate.findAll(baseVo.getData().getClass(), baseVo.getCollectionName());
return resultList;
}
}
10
db.statistics.update({}, {$set: {createtime:new Date()}}, {multi: 1})
mongodb 添加字段方法,在客户端工具中操作
客户端工具用:Robo 3T 1.2.1
11 集合创建索引方法
db.getCollection('blackList').ensureIndex({"customerId": 1, "description": -1}, {background: true})
原生客户端
1 3.10.1使用了 java9 的版本,下面maven是普通的吧,没明白区别 org.mongodb.driver.sync.client <dependency> <groupId>org.mongodb</groupId> <artifactId>mongodb-driver-sync</artifactId> <version>3.10.1</version> </dependency> <dependency> <groupId>org.mongodb</groupId> <artifactId>mongodb-driver</artifactId> <version>3.10.1</version> </dependency> 目前在用项目的为3.8 2 最新的这样 import com.mongodb.ConnectionString; import com.mongodb.client.MongoClients; import com.mongodb.client.MongoClient; 历史遗留的这样 import com.mongodb.MongoClient; import com.mongodb.MongoClientURI; 别的就一样了,工头的元素 mport com.mongodb.ServerAddress; import com.mongodb.client.MongoDatabase; import com.mongodb.client.MongoCollection; import org.bson.Document; import java.util.Arrays; import com.mongodb.Block; import com.mongodb.client.MongoCursor; import static com.mongodb.client.model.Filters.*; import com.mongodb.client.result.DeleteResult; import static com.mongodb.client.model.Updates.*; import com.mongodb.client.result.UpdateResult; import java.util.ArrayList; import java.util.List; 2 开始连接了 MongoClient mongoClient = MongoClients.create(); 默认27017 如果是有host,看着像是集群 MongoClient mongoClient = MongoClients.create( MongoClientSettings.builder() .applyToClusterSettings(builder -> builder.hosts(Arrays.asList(new ServerAddress("hostOne")))) .build()); 如果有host和端口 MongoClient mongoClient = MongoClients.create( MongoClientSettings.builder() .applyToClusterSettings(builder -> builder.hosts(Arrays.asList(new ServerAddress("hostOne", 27018)))) .build()); 第四个连接方式是 使用字符串 MongoClient mongoClient = MongoClients.create("mongodb://hostOne:27017,hostTwo:27018"); 4 获取集合方法 MongoCollection<Document> collection = database.getCollection("test"); 5 创建一个文档 { "name" : "MongoDB", "type" : "database", "count" : 1, "versions": [ "v3.2", "v3.0", "v2.6" ], "info" : { x : 203, y : 102 } } Document doc = new Document("name", "MongoDB") .append("type", "database") .append("count", 1) .append("versions", Arrays.asList("v3.2", "v3.0", "v2.6")) .append("info", new Document("x", 203).append("y", 102)); 6 插入1条和多条 collection.insertOne(doc); List<Document> documents = new ArrayList<Document>(); for (int i = 0; i < 100; i++) { documents.add(new Document("i", i)); } collection.insertMany(documents); 7 打印出集合的数据的数量 System.out.println(collection.countDocuments()); 8 查询 查询循环遍历出结果 MongoCursor<Document> cursor = collection.find().iterator(); try { while (cursor.hasNext()) { System.out.println(cursor.next().toJson()); } } finally { cursor.close(); } 查询一条doc myDoc = collection.find(eq("i", 71)).first(); System.out.println(myDoc.toJson()); { "_id" : { "$oid" : "5515836e58c7b4fbc756320b" }, "i" : 71 } 查询所有复合条件的 document Block<Document> printBlock = new Block<Document>() { @Override public void apply(final Document document) { System.out.println(document.toJson()); } }; collection.find(gt("i", 50)).forEach(printBlock); 查询条件哦大于小于 collection.find(and(gt("i", 50), lte("i", 100))).forEach(printBlock); 9 更新 1条和更新多条 collection.updateOne(eq("i", 10), new Document("$set", new Document("i", 110))); UpdateResult updateResult = collection.updateMany(lt("i", 100), inc("i", 100)); System.out.println(updateResult.getModifiedCount()); 10 删除一条和多条 collection.deleteOne(eq("i", 110)); DeleteResult deleteResult = collection.deleteMany(gte("i", 100)); System.out.println(deleteResult.getDeletedCount());
15 指定副本集连接 MongoClient mongoClient = MongoClients.create( MongoClientSettings.builder() .applyToClusterSettings(builder -> builder.hosts(Arrays.asList( new ServerAddress("host1", 27017), new ServerAddress("host2", 27017), new ServerAddress("host3", 27017)))) .build()); 或者 MongoClient mongoClient = MongoClients.create("mongodb://host1:27017,host2:27017,host3:27017/?replicaSet=myReplicaSet"); 16 如指定集群则指定的是mongos的ip和端口 MongoClient mongoClient = MongoClients.create( "mongodb://localhost:27017" ); 也可指定多个mongos MongoClient mongoClient = MongoClients.create( MongoClientSettings.builder() .applyToClusterSettings(builder -> builder.hosts(Arrays.asList( new ServerAddress("host1", 27017), new ServerAddress("host2", 27017)))) .build()); 17 指定证书和ssl两种方法 MongoClient mongoClient = MongoClients.create("mongodb://user1:pwd1@host1/?authSource=db1&ssl=true"); String user; // the user name String database; // the name of the database in which the user is defined char[] password; // the password as a character array // ... 17 tsl /ssl的方法 MongoCredential credential = MongoCredential.createCredential(user, database, password); MongoClientSettings settings = MongoClientSettings.builder() .credential(credential) .applyToSslSettings(builder -> builder.enabled(true)) .applyToClusterSettings(builder -> builder.hosts(Arrays.asList(new ServerAddress("host1", 27017)))) .build(); MongoClient mongoClient = MongoClients.create(settings);
1 mongodb 普通语法介绍
安装有普通版和免费集群层版
插入多条数据
db.inventory.insertMany([
// MongoDB adds the _id field with an ObjectId if _id is not present
{ item: "journal", qty: 25, status: "A",
size: { h: 14, w: 21, uom: "cm" }, tags: [ "blank", "red" ] },
size: { h: 10, w: 15.25, uom: "cm" }, tags: [ "blue" ] }
]);
可以查询内嵌的数据
db.inventory.find( { size: { h: 14, w: 21, uom: "cm" } } )
如果查询字段是数组,直接用 普通查询,就会匹配
db.inventory.find( { tags: "red" } )
精确匹配数组方法,连数组都写上
db.inventory.find( { tags: ["red", "blank"] } )
2 搭建免费层集群
1 登录 mongo的账号
2 选择 altias
3 建立项目和成员
4 建立集群 m0是免费的
https://docs.mongodb.com/manual/tutorial/atlas-free-tier-setup/
5 安全方面添加新用户
然后下载连接客户端, 建立连接地址,建立连接集群
运行项目
"C:\Program Files\MongoDB\Server\4.0\bin\mongo.exe" "mongodb+srv://mycluster-ma4u7.mongodb.net/test" --username mongodbusername
3 更多命令
插入数据,创建索引
db.myNewCollection2.insertOne( { x: 1 } )
db.myNewCollection3.createIndex( { y: 1 } )
Type | Number | Alias | Notes |
---|---|---|---|
Double | 1 | “double” | |
String | 2 | “string” | |
Object | 3 | “object” | |
Array | 4 | “array” | |
Binary data | 5 | “binData” | |
Undefined | 6 | “undefined” | Deprecated. |
ObjectId | 7 | “objectId” | |
Boolean | 8 | “bool” | |
Date | 9 | “date” | |
Null | 10 | “null” | |
Regular Expression | 11 | “regex” | |
DBPointer | 12 | “dbPointer” | Deprecated. |
JavaScript | 13 | “javascript” | |
Symbol | 14 | “symbol” | Deprecated. |
JavaScript (with scope) | 15 | “javascriptWithScope” | |
32-bit integer | 16 | “int” | |
Timestamp | 17 | “timestamp” | |
64-bit integer | 18 | “long” | |
Decimal128 | 19 | “decimal” | New in version 3.4. |
Min key | -1 | “minKey” | |
Max key | 127 | “maxKey” |
5 使用shell 命令
登录 端口 host 名 密码 指定数据库
mongo --username alice --password --authenticationDatabase admin --host mongodb0.examples.com --port 28015
可以连接一批副本集
mongo "mongodb://mongodb0.example.com.local:27017,mongodb1.example.com.local:27017,mongodb2.example.com.local:27017/?replicaSet=replA"
1 mongodb 导入数据
检测 mongodb启动的方法 window的方法
tasklist 的确可用
tasklist /FI "IMAGENAME eq mongod.exe"
在路径
https://docs.mongodb.com/guides/server/import/
如果时linux使用
ps -e| grep 'mongod'
导入的方法
mongoimport --db test --collection inventory ^
--authenticationDatabase admin --username <user> --password <password> ^
--drop --file ~\downloads\inventory.crud.json
2 mongodb统计的例子
分两个部分,先过滤$match,再$group分组统计数量
db.orders.aggregate([
{ $match: { status: "A" } },
{ $group: { _id: "$cust_id", total: { $sum: "$amount" } } }
])
第二种统计是mapreduce,据说性能不咋样,看图
db.orders.mapReduce(a,b,{query,out}),看图
第三种是distinct的方法,看图
注意从mongoDB 3.0开始ensureIndex被废弃,今后都仅仅是db.collection.createIndex的一个别名。
不确定,竟然不是 分开的 index
db.getCollection('singleride').ensureIndex({"startFloor": 1, "stopFloor": 1,"currentLoad":1,"timestamp":1}, {background: true});
2 方法和数据重名导致找不到方法了 3 emit的用户 子组件@xxx是父组件方法名,子组件通过 $emit来提交调用用的 4 使用动态变量时出了很大的问题 this.$data[chartParams.drivingcharttype[idx]]= 加上 this.$nextTick(()=> 会完全渲染,只出现最后一个也不行 变换思路直接改为 this.maxAmplitudeX=false; this.maxAmplitudeY=false; this.maxAmplitudeZ=false; 即可 放到循环外也不行, 这就是 if 和show的差距, 不明白具体坑的原理但是 用了show 秒好
1 查询指定的列,去重,批量更新或插入, 如抛出线程中断导致更新失败的话不用管 Interrupted acquiring a permit to retrieve an item from the pool ,也可能是个bug BasicDBObject queryCondition = new BasicDBObject(); BasicDBObject result = new BasicDBObject(); result.put("deviceId",1); Query query = new BasicQuery(queryCondition.toJson(),result.toJson()); List<String> kpIsList = mongoTemplate.findDistinct(query,"deviceId","kpis",String.class); BulkOperations bo = mongoTemplate.bulkOps(BulkOperations.BulkMode.UNORDERED,"sensor"); if(kpIsList!=null&&!kpIsList.isEmpty()){ for (String deviceId : kpIsList){ Query upQuery = new Query(); upQuery.addCriteria(Criteria.where("deviceId").is(deviceId)); Update update = new Update(); update.set("deviceId",deviceId); update.set("deviceType","Smart Bin"); update.set("networkType","Iot"); update.set("lastUpdateTime",new Date()); bo.upsert(upQuery,update); } BulkWriteResult result1 = bo.execute(); } }
真实可用
./mongo --port 27017 -u wisx -p wisx --authenticationDatabase 数据库名
真实可用 ./mongo --port 27017 -u wisx -p wisx --authenticationDatabase liftdb /usr/local/mongodb/bin db.createUser({user:"admin",pwd:"admin",roles:[{role:"userAdminAnyDatabase",db:'admin'}],mechanisms:["SCRAM-SHA-1"]}); db.createUser({user:"wisx",pwd:"wisx",roles:[ {role:"readWrite",db:'readdbnamedb'}, {role:"userAdmin",db:'readdbnamedb'}, {role:"dbAdmin",db:'readdbnamedb'} ],mechanisms:["MONGODB-CR"]}); db.createUser({ user:'admin', pwd:'admin', roles:[{role:'userAdminAnyDatabase',db:'admin'}] }) db.createUser({ user:'wisx', pwd:'wisx', roles:[{role:'readWrite',db:'readdbnamedb'}] }); db.createUser({user:"admin",pwd:"admin123",roles:[{role:"userAdminAnyDatabase",db:'admin'}],mechanisms:["SCRAM-SHA-1"]}); ===============================》》》》》》》》》》》》》》》》》》 mongo --port 28015
mongodb 的导入导出
mongoexport --host mylocalip --port 27017 --username "user" --password "pwd" --collection colldemo --db mydb --out colldemo.json
./mongoimport --host mylocalip --port 27017 --username user --password "pwd" --collection colldemo --db mydb --file colldemo.json