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); });
本文作者:Hi.PrimaryC
本文链接:https://www.cnblogs.com/cnff/p/17362463.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步