项目基座需要将mongoDB的链接实现,低代码引擎内置了mongo对象,可用户操作MongoDB数据集
collection 创建删除数据集
示例如下:
def template = mongo.template();
def collection ;
// 判断Collection是否存在,如果存在直接返回,如果不存在创建后返回
if (template.collectionExists("persion")) {
collection = template.getCollection("persion");
} else {
collection = template.createCollection("persion");
}
def collectionNamespace = collection.getNamespace().toString();
// 删除 Collection
template.dropCollection("persion")
print collectionNamespace
运行结果:
aop.persion
collection 数据集增删改查
示例如下:
import org.bson.Document;
import com.mongodb.client.model.Filters;
def template = mongo.template();
def collection ;
// 判断Collection是否存在,如果存在直接返回,如果不存在创建后返回
if (template.collectionExists("persion")) {
collection = template.getCollection("persion");
} else {
collection = template.createCollection("persion");
}
// Json String 转 Document
def docByJson = Document.parse('{"name": "aop" , "age": 1.5}')
docByJson.append("pId" , "123")
def docByJsonSaved = collection.insertOne(docByJson)
// Map 转 Document
def docByMap = new Document(["name": "MapToDocuemt" , "age" : 2]);
docByMap.append("pId" , "456")
def docByMapSaved = collection.insertOne(docByMap)
// Update
def filter = Filters.eq("pId", "456");
def update = new Document('''$set''', new Document("name" , "123 new name"));
def updateResult = collection.updateOne(filter, update);
// collection.find()
// TDocument findOneAndUpdate(Bson filter, Bson update);
// @Nullable
// TDocument findOneAndDelete(Bson filter);
/*
filter 为条件
projection 为返回字段描述,设置为 0 不显示
first 返回第一条
(skip 跳过多少行 limit 读多少行)组合后可以实现分页
forEach 遍历游标
*/
def find = collection.find(filter).projection(new Document(["_id": 0 , "name" :0 ])).first();
def findList = []
collection.find().projection(new Document(["_id": 0])).skip(1).limit(10).forEach(row -> {
findList << row
})
// 删除
def dr = collection.deleteOne(filter);
def fad = collection.findOneAndDelete(filter);
def dm = collection.deleteMany(filter);
json {
countDocuments collection.countDocuments()
docByJsonRe docByJsonSaved
docByMapRe docByMapSaved
f filter.toString()
s update
updateRe updateResult
findRe find
findListR findList
deleteResult dr
findOneAndDelete fad
deleteMany dm
}
运行结果:
{"countDocuments":1,"docByJsonRe":{"insertedId":{"objectId":true,"int64":false,"boolean":false,"double":false,"dateTime":false,"int32":false,"null":false,"string":false,"regularExpression":false,"javaScriptWithScope":false,"document":false,"timestamp":false,"DBPointer":false,"number":false,"value":{"date":"2022-07-12T08:55:02+0000","timestamp":1657616102},"javaScript":false,"decimal128":false,"array":false,"symbol":false,"binary":false,"bsonType":"OBJECT_ID"}},"docByMapRe":{"insertedId":{"objectId":true,"int64":false,"boolean":false,"double":false,"dateTime":false,"int32":false,"null":false,"string":false,"regularExpression":false,"javaScriptWithScope":false,"document":false,"timestamp":false,"DBPointer":false,"number":false,"value":{"date":"2022-07-12T08:55:02+0000","timestamp":1657616102},"javaScript":false,"decimal128":false,"array":false,"symbol":false,"binary":false,"bsonType":"OBJECT_ID"}},"f":"Filter{fieldName='pId', value=456}","s":{"$set":{"name":"123 new name"}},"updateRe":{"modifiedCount":1,"matchedCount":1,"upsertedId":null},"findRe":{"age":2,"pId":"456"},"findListR":[{"name":"123 new name","age":2,"pId":"456"}],"deleteResult":{"deletedCount":1},"findOneAndDelete":null,"deleteMany":{"deletedCount":0}}