MongoDB操作记录
1.类型转换
1. MongoDb 内层数据,需要转换类型再过滤
db.model_18300_1_origin.aggregate([
{
$unwind: {
path: "$properties"
}
},
{
$match: {
$and: [
{
"properties.name": "构件图元ID"
},
{
"properties.system": 0
},
]
}
},
{
$project: {
"name": "$properties.name",
"value": {
$convert: {
"input": "$properties.value",
"to": "decimal",
"onError": "$properties.value"
}
},
"entityId": 1,
"_id": 0
}
},
{
$sort: {"value": -1}
}
])
2. 截取
2. 根据图元 ID 排序,这个图元 ID 是从字段 vname 截取出来的。
/**
* 根据图元 id 排序
**/
AggregateIterable<Document> sortIter = mongoOption.modelOption(
modelInfo.getId(),
modelInfo.getVersion(),
ModelDBTypeEnum.origin,
collection -> {
AggregateIterable<Document> aggregateIterable = collection.aggregate(
asList(
project(
Projections.fields(
Projections.excludeId(),
Projections.computed("entityId", "$entityId"),
Projections.computed("vname",
new Document("$regexFind",
new Document("input", "$vname")
.append("regex", "(?<=\\[).*?(?=\\])")
)
)
)
),
project(
Projections.fields(
Projections.computed("entityId", 1),
Projections.computed("vname", new Document("$convert", new Document("input", "$vname.match").append("to", "decimal").append("onError", "$vname.match")))
)
)
)
).allowDiskUse(true);
return aggregateIterable;
}
);
List<Document> deo = IterUtil.toList(sortIter);
3. 数组操作
1. 向数组中添加
db.model_9610_1_origin.updateMany(
{
entityId: {"$gt":600, "$lte": 1000}
},
{$push : {
"properties": {
name: "构件名称(Default)",
value: "唐三的暗器",
category: "标识数据",
system: NumberInt(0),
type: NumberInt(0)
}
}}
)
2. 数组中删除
db.model_9610_1_origin.updateMany(
{},
{ $pull: { properties: { name: "构件名称(Default)" } } }
)
3. 选择某个属性并去重
db.model_2186_1_origin.aggregate([
{
"$unwind": "$properties"
},
{
"$match": {
"properties.name": {
"$eq": "构件名称(Default)"
}
}
},
{
$group: {
_id: "$properties.value"
}
}
])
4. 判断数组不为空
match(
and(
Filters.exists("properties", true),
Filters.ne("properties", Arrays.asList())
)
)
5. 分组
db.model_9631_1_origin.aggregate([
{
"$unwind": "$properties"
},
{
"$match": {
"properties.name": {
"$eq": "装配式构件类别(自定义)"
}
}
},
{
$group: {
_id: "$properties.value", resultArray : { $push : "$entityId" }
}
}
])
6. 批量删除
匹配对应的构件 id,然后删除其对应的 properties 字段数组中,category = '装配' 的数据
List<Integer> entityList = new ArrayList<>(1,2,3,4,5,6,7);
mongoOption.modelOption(modelId, modelVersion, ModelDBTypeEnum.custom_origin, collection -> {
return collection.updateMany(Filters.in("entityId",entityList),new Document("$pull",new Document("properties",new Document("category","装配"))));
});
7. 批量修改
向数组中添加一个数组的内容
//建立索引
mongoOption.modelOption(modelId, modelVersion, ModelDBTypeEnum.custom_origin, collection -> {
return collection.createIndex(new Document("entityId",1));
});
//更新条件
List<UpdateManyModel<Document>> updateManyModelList = new ArrayList<>();
for(ModelOrigin mo : modelOriginList){
updateManyModelList.add(new UpdateManyModel<>(new Document("entityId", mo.getEntityId().intValue()),
new Document("$push", new Document("properties", new Document("$each", mo.getProperties().stream().filter(e->!Objects.equals(e.getType(),0)).collect(Collectors.toList())))),
//如果没有这个 entityId 就新增
new UpdateOptions().upsert(true)
));
}
mongoOption.modelOption(modelId, modelVersion, ModelDBTypeEnum.custom_origin, collection -> {
return collection.bulkWrite(updateManyModelList);
});