nbcp.myoql.db.mongo.component.MongoBaseEntity_Extend.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ktmyoql Show documentation
Show all versions of ktmyoql Show documentation
kotlin orm -- mysql,mongo , just like ktorm
The newest version!
@file:JvmName("MyOqlMongo")
@file:JvmMultifileClass
package nbcp.myoql.db.mongo
import nbcp.myoql.db.mongo.component.MongoBaseMetaCollection
import nbcp.myoql.db.mongo.extend.mongoEquals
import org.bson.types.ObjectId
import org.springframework.data.mongodb.core.query.Criteria
fun , T : Any> M.query(): MongoQueryClip = MongoQueryClip(this);
fun , T : Any> M.query(whereData: Criteria): MongoQueryClip {
var ret = MongoQueryClip(this);
ret.where(whereData);
return ret;
}
fun , T : Any> M.queryById(id: String): MongoQueryClip =
this.query().where("id" mongoEquals id);
fun , E : Any> M.updateById(id: String): MongoUpdateClip {
var idValue = id.trim()
if (idValue.isEmpty()) {
throw RuntimeException("按id更新mongo数据时,id不能为空!")
}
return MongoUpdateClip(this).where("id" mongoEquals idValue);
}
/**
* 按实体单条更新。 默认使用Id更新,不会更新id。
*/
fun , E : Any> M.updateWithEntity(entity: E): MongoSetEntityUpdateClip {
return MongoSetEntityUpdateClip(this, entity);
}
fun > M.batchInsert(): MongoInsertClip {
return MongoInsertClip(this);
}
fun , E : Any> M.updateById(id: ObjectId): MongoUpdateClip {
var ret = this.update();
ret.where("id" mongoEquals id);
return ret;
}
fun , E : Any> M.update(): MongoUpdateClip {
return MongoUpdateClip(this);
}
fun > M.delete(): MongoDeleteClip = MongoDeleteClip(this)
fun > M.deleteById(id: String): MongoDeleteClip {
var ret = MongoDeleteClip(this);
ret.where("id" mongoEquals id)
return ret;
}
/**
* 聚合。
*/
fun , E : Any> M.aggregate(): MongoAggregateClip {
/*
db.articles.aggregate( [
{ $match : { score : { $gt : 70, $lte : 90 } } },
{ $group: { _id: null, count: { $sum: 1 } } }
] );
*/
var ret = MongoAggregateClip(this);
return ret;
}
///**
// * 如果存在,就Update,否则 Insert
// */
//fun , E : Serializable> M.save(entity: E, unionKey: ((M) -> MongoColumnName)): Int {
// var ret = this.updateWithEntity(entity).whereColumn(unionKey).exec()
// if (ret > 0) return ret;
// this.doInsert(entity);
// ret = db.affectRowCount;
// return ret;
//}