本文整理汇总了Java中com.mongodb.client.model.IndexOptions.unique方法的典型用法代码示例。如果您正苦于以下问题:Java IndexOptions.unique方法的具体用法?Java IndexOptions.unique怎么用?Java IndexOptions.unique使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.mongodb.client.model.IndexOptions
的用法示例。
在下文中一共展示了IndexOptions.unique方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: internalSetupIndexes
import com.mongodb.client.model.IndexOptions; //导入方法依赖的package包/类
private void internalSetupIndexes(Class<?> entityClass, MongoCollection<Document> collection) {
if (entityClass == null || collection == null) {
return;
}
MongoIndex[] indexes = entityClass.getAnnotationsByType(MongoIndex.class);
if (indexes != null && indexes.length > 0) {
for (MongoIndex index : indexes) {
Document indexDocument = new Document();
indexDocument.put(index.key(), index.direction());
IndexOptions options = new IndexOptions();
options.unique(index.unique());
options.background(index.background());
collection.createIndex(indexDocument, options);
}
}
}
示例2: removeDuplicate
import com.mongodb.client.model.IndexOptions; //导入方法依赖的package包/类
/**
* removeDuplicate
* by add a unique index
*/
private static void removeDuplicate(){
MongoClient client = new MongoClient("127.0.0.1");
MongoDatabase db = client.getDatabase("airvis");
MongoCollection preprocess = db.getCollection("pm_preProcess");
MongoCollection process = db.getCollection("pmProcess");
IndexOptions option = new IndexOptions();
option.unique(true);
process.createIndex(new Document().append("time",1).append("code",1), option);
MongoCursor cur = preprocess.find().iterator();
while(cur.hasNext()){
Document obj = (Document)cur.next();
try {
process.insertOne(obj);
}catch(MongoWriteException e){
//duplicate error
System.out.println(obj.toString());
}
}
}
示例3: removeDuplicate
import com.mongodb.client.model.IndexOptions; //导入方法依赖的package包/类
/**
* removeDuplicate
* by add a unique index
*/
private static void removeDuplicate(){
MongoClient client = new MongoClient("127.0.0.1");
MongoDatabase db = client.getDatabase("pm");
MongoCollection preprocess = db.getCollection("pm_preProcess");
MongoCollection process = db.getCollection("pmProcess");
IndexOptions option = new IndexOptions();
option.unique(true);
process.createIndex(new Document().append("time",1).append("code",1), option);
MongoCursor cur = preprocess.find().iterator();
while(cur.hasNext()){
Document obj = (Document)cur.next();
try {
process.insertOne(obj);
}catch(MongoWriteException e){
//duplicate error
System.out.println(obj.toString());
}
}
}
示例4: beforeRun
import com.mongodb.client.model.IndexOptions; //导入方法依赖的package包/类
@Override
public void beforeRun() {
super.beforeRun();
IndexOptions firstNameOption = new IndexOptions();
firstNameOption.unique(false);
personCollection.createIndex(new Document("firstName", 1), firstNameOption);
personCollection.createIndex(new Document("personalNote", "text"));
personCollection.insertMany(documents);
}
示例5: createChange
import com.mongodb.client.model.IndexOptions; //导入方法依赖的package包/类
private MongoIndexChange createChange (CompoundIndex index, boolean replace){
Document definition = Document.parse(index.def());
IndexOptions opts = new IndexOptions();
opts.name(index.name());
opts.unique(index.unique());
opts.sparse(index.sparse());
opts.background(index.background());
return new MongoIndexChange(index.name(), definition, opts, replace);
}
示例6: createIndex
import com.mongodb.client.model.IndexOptions; //导入方法依赖的package包/类
/**
* 创建索引
*
* @param collectionName 集合
* @param indexName 索引名
* @param keys 键信息,如 securityCode:1
* @param isUnique 是否唯一索引
*/
public void createIndex(String collectionName, String indexName, Map<String, Integer> keys, boolean isUnique) {
MongoCollection collection = sMongoDatabase.getCollection(collectionName);
IndexOptions indexOptions = new IndexOptions();
indexOptions.background(true);
indexOptions.unique(isUnique);
indexOptions.name(indexName);
Map<String, Object> keyIndexs = new HashMap<>(16);
for (Map.Entry<String, Integer> entry : keys.entrySet()) {
keyIndexs.put(entry.getKey(), entry.getValue() > 0 ? 1 : entry.getValue() == 0 ? 0 : -1);
}
collection.createIndex(new Document(keyIndexs), indexOptions);
}
示例7: prepareEntity
import com.mongodb.client.model.IndexOptions; //导入方法依赖的package包/类
/**
* Prepares the data structure for this Entity class in the given database, this means creating declared
* indexes etc.
*/
private MongoCollection<? extends Entity> prepareEntity( final EntityProperties properties )
{
// TODO need to add verification that index field matches existing property
Class<? extends Entity> clazz = properties.getEntityClass();
Collection c = clazz.getAnnotation( Collection.class );
MongoCollection<? extends Entity> coll = EntityCodec.getCollectionFor( db, properties );
if ( c != null && c.indexes() != null )
{
LOG.debug( "Entity class {} has indexes, ensuring that MongoDB is setup",
properties.getEntityClass() );
for ( Index index : c.indexes() )
{
IndexOptions options = new IndexOptions();
if ( index.unique() )
{
options.unique( true );
}
if ( isNotEmpty( index.name() ) )
{
options.name( index.name() );
}
Document indxFields = new Document();
for ( IndexField field : index.value() )
{
checkNotNull( properties.getProperty( field.field() ),
"Index field '%s' for index '%s' does not exist for %s", field.field(), index.name(),
clazz );
indxFields.put( field.field(),
( field.order() == IndexField.Ordering.ASC ) ? OrderBy.ASC.getIntRepresentation()
: OrderBy.DESC.getIntRepresentation() );
}
LOG.debug( "Creating index {} for Entity class {}", options.getName(),
properties.getEntityClass() );
coll.createIndex( indxFields, options );
}
}
return coll;
}
示例8: createIndex
import com.mongodb.client.model.IndexOptions; //导入方法依赖的package包/类
protected void createIndex(String name, BasicDBList dbList, MongoCollection<Document> collection) {
if (dbList.isEmpty() || name == null) {
return;
}
BasicDBObject index = (BasicDBObject) dbList.get(0);
IndexOptions options = new IndexOptions().name(name);
if (dbList.size() > 1) {
BasicDBObject opts = (BasicDBObject) dbList.get(1);
if (opts.get("unique") != null && opts.get("unique") instanceof Boolean) {
options = options.unique(opts.getBoolean("unique"));
}
if (opts.get("expireAfterSeconds") != null && opts.get("expireAfterSeconds") instanceof Integer) {
options = options.expireAfter(Long.valueOf(opts.getInt("expireAfterSeconds")), TimeUnit.SECONDS);
}
if (opts.get("background") != null && opts.get("background") instanceof Boolean) {
options = options.background(opts.getBoolean("background"));
}
if (opts.get("bits") != null && opts.get("bits") instanceof Integer) {
options = options.bits(opts.getInt("bits"));
}
if (opts.get("bucketSize") != null && opts.get("bucketSize") instanceof Double) {
options = options.bucketSize(opts.getDouble("bucketSize"));
}
if (opts.get("defaultLanguage") != null && opts.get("defaultLanguage") instanceof String) {
options = options.defaultLanguage(opts.getString("defaultLanguage"));
}
if (opts.get("languageOverride") != null && opts.get("languageOverride") instanceof String) {
options = options.languageOverride(opts.getString("languageOverride"));
}
if (opts.get("min") != null && opts.get("min") instanceof Double) {
options = options.min(opts.getDouble("min"));
}
if (opts.get("max") != null && opts.get("max") instanceof Double) {
options = options.max(opts.getDouble("max"));
}
if (opts.get("sparse") != null && opts.get("sparse") instanceof Boolean) {
options = options.sparse(opts.getBoolean("sparse"));
}
if (opts.get("textVersion") != null && opts.get("textVersion") instanceof Integer) {
options = options.textVersion(opts.getInt("textVersion"));
}
if (opts.get("version") != null && opts.get("version") instanceof Integer) {
options = options.version(opts.getInt("version"));
}
if (opts.get("weights") != null && opts.get("weights") instanceof Bson) {
options = options.weights((Bson) opts.get("weights"));
}
}
collection.createIndex(new Document(index), options);
}
示例9: createOptions
import com.mongodb.client.model.IndexOptions; //导入方法依赖的package包/类
private static IndexOptions createOptions(BsonDocument doc) {
final IndexOptions options = new IndexOptions();
options.unique(isUnique(doc));
return options;
}
示例10: getIndexs
import com.mongodb.client.model.IndexOptions; //导入方法依赖的package包/类
@Override
public List<IndexModel> getIndexs (T t) {
// 获取该类的属性
Field[] clazz_fields = clazz.getDeclaredFields();
// 获取T父对象的属性
Field[] superFields = superClazz.getDeclaredFields();
Field[] fields = new Field[clazz_fields.length + superFields.length];
// 合并T与T父对象的属性
System.arraycopy(clazz_fields, 0, fields, 0, clazz_fields.length);
// 合并T与T父对象的属性
System.arraycopy(superFields, 0, fields, clazz_fields.length, superFields.length);
// 创建索引序列
List<IndexModel> indexModels = new ArrayList<IndexModel>();
// 循环该类的属性
for (Field field: fields) {
// 如果属性被Index注解,那么
if (field.isAnnotationPresent(Index.class)) {
// 获取注解对象
Index index = field.getAnnotation(Index.class);
// MongoDB ObjectID主键或者不是主键
if(index.id() == true || index.key() == false) {
continue;
}
// 创建索引键
BasicDBObject keys = new BasicDBObject();
keys.append(field.getName(), index.order());
// 附加选项
IndexOptions indexOptions = new IndexOptions();
if(index.background()) {
indexOptions.background(index.background());
}
// 所有名字
if("".equals(index.name()) == false) {
indexOptions.name(index.name());
}
// 唯一字段
if(index.unique()) {
indexOptions.unique(index.unique());
}
if(index.sparse()) {
indexOptions.sparse(index.sparse());
}
// 创建索引
IndexModel indexModel = new IndexModel(keys,indexOptions);
// 索引
indexModels.add(indexModel);
}
}
return indexModels;
}