本文整理汇总了Java中com.mongodb.client.ListIndexesIterable类的典型用法代码示例。如果您正苦于以下问题:Java ListIndexesIterable类的具体用法?Java ListIndexesIterable怎么用?Java ListIndexesIterable使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ListIndexesIterable类属于com.mongodb.client包,在下文中一共展示了ListIndexesIterable类的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getCollectionIndexes
import com.mongodb.client.ListIndexesIterable; //导入依赖的package包/类
/**
*
* @param dbName
* @param collName
* @return
*/
List<BsonDocument> getCollectionIndexes(String dbName, String collName) {
List<BsonDocument> ret = new ArrayList<>();
ListIndexesIterable<Document> indexes = client
.getDatabase(dbName)
.getCollection(collName, BsonDocument.class)
.listIndexes();
indexes.iterator().forEachRemaining(
i -> {
BsonDocument bi = BsonDocument.parse(i.toJson());
BsonValue name = bi.remove("name");
bi.put("_id", name);
ret.add(bi);
});
return ret;
}
示例2: getIndexNames
import com.mongodb.client.ListIndexesIterable; //导入依赖的package包/类
public static Set<String> getIndexNames(MongoCollection<Document> collection) {
ListIndexesIterable<Document> currentIndexes = collection.listIndexes();
MongoCursor<Document> cursor = currentIndexes.iterator();
Set<String> indexNames = new HashSet<>();
while (cursor.hasNext()) {
Object next = cursor.next();
String name = ((Document) next).get("name").toString();
indexNames.add(name);
}
return indexNames;
}
示例3: emptySecondaryIndexTest
import com.mongodb.client.ListIndexesIterable; //导入依赖的package包/类
@Test
public void emptySecondaryIndexTest() throws Exception {
final TestRunner runner = TestRunners.newTestRunner(new StoreInMongo());
addMongoService(runner);
runner.setProperty(MongoProps.DATABASE, MONGO_DATABASE_NAME);
runner.setProperty(MongoProps.COLLECTION, "index_test");
// runner.setProperty(MongoProps.INDEX, "[{\"a\": 1}]");
runner.run();
// Connect to mongo to inspect indexes
MongoClient client = mongo.getMongoClient();
ListIndexesIterable<Document> indexes = client.getDatabase(MONGO_DATABASE_NAME).getCollection("index_test").listIndexes();
List<Document> indexesArray = new ArrayList<>();
indexesArray = indexes.into(indexesArray);
// Check for compound index
boolean hasCompound = false;
for (Document doc : indexesArray) {
if (doc.get("name").toString().equals("t_1_s_1")) {
hasCompound = true;
}
}
assertFalse("Should not have compound index", hasCompound);
}
示例4: loadDBIndexes
import com.mongodb.client.ListIndexesIterable; //导入依赖的package包/类
private Set<InternalIndex> loadDBIndexes() {
final ListIndexesIterable<BsonDocument> bsonIndexes = collection.listIndexes(BsonDocument.class);
final Set<InternalIndex> indexes = Sets.newLinkedHashSet();
for(BsonDocument bsonIndex : bsonIndexes) {
indexes.add(InternalIndex.fromBson(bsonIndex));
}
return indexes;
}
示例5: createIndex
import com.mongodb.client.ListIndexesIterable; //导入依赖的package包/类
private void createIndex(IndexDefinition indexDefinition, boolean force) {
// create index if not exists... use index name too from indexDefinition
// object
if (indexDefinition == null) {
return;
}
String indexName = IndexNameFactory.createIndexName(indexDefinition);
MongoCollection<Document> collection = MongoDBFactory.getCollection(instanceName, tableName);
int limit = Integer.parseInt(
MultiValueConfigLoader.getConfig("MONGODB-" + instanceName + ".limit", MultiValueConfigLoader.getConfig("MONGODB-default.limit", "250")));
boolean indexExists = false;
ListIndexesIterable<Document> indexInfo = collection.listIndexes();
for (Document dbObject : indexInfo) {
Object name = dbObject.get("name");
if (name != null && name.toString().equals(indexName)) {
indexExists = true;
break;
}
}
if (force || collection.count() < limit) {
createIt(indexDefinition, force, indexName, collection);
} else {
if (!indexExists) {
log.warn(tableName + " collection has more than " + limit + " items. please index manually");
}
}
}
示例6: queryIndex
import com.mongodb.client.ListIndexesIterable; //导入依赖的package包/类
@Override
public Map<String, Object> queryIndex () {
// get current collection name
String collectionNameForT = getCollectionName(collectionNames.get());
// Indexes Iterator
ListIndexesIterable iterable = database.getCollection(collectionNameForT).listIndexes();
// cycle the indexes
Iterator iterator = iterable.iterator();
List<String> indexes = new ArrayList<String>();
List<Keys> keys = new ArrayList<Keys>();
Map<String, Object> map = new HashMap<String, Object>();
while (iterator.hasNext()) {
// read value
Document document = (Document) iterator.next();
Keys indexKey = new Keys();
// cycle key
for (String key :document.get("key", Document.class).keySet()) {
indexes.add(key);
indexKey.setKey(key);
// key's order value
indexKey.setOrder(document.get("key", Document.class).getInteger(key));
}
indexKey.setName(document.getString("name"));
indexKey.setNs(document.getString("ns"));
indexKey.setBackground(document.getBoolean("background"));
indexKey.setSparse(document.getBoolean("sparse"));
keys.add(indexKey);
}
map.put("indexes", indexes);
map.put("keys", keys);
// gc
gc();
return map;
}
示例7: listIndexes
import com.mongodb.client.ListIndexesIterable; //导入依赖的package包/类
@Override
public ListIndexesIterable<Document> listIndexes()
{
return collection.listIndexes();
}
示例8: testInsertDynamicityEnabledDBAndCollectionAndIndex
import com.mongodb.client.ListIndexesIterable; //导入依赖的package包/类
@Test
public void testInsertDynamicityEnabledDBAndCollectionAndIndex() {
assertEquals(0, testCollection.count());
mongo.getDatabase("otherDB").drop();
db.getCollection("otherCollection").drop();
assertFalse("The otherDB database should not exist",
StreamSupport.stream(mongo.listDatabaseNames().spliterator(), false)
.anyMatch("otherDB"::equals));
String body = "{\"_id\": \"testInsertDynamicityEnabledDBAndCollection\", \"a\" : 1, \"b\" : 2}";
Map<String, Object> headers = new HashMap<String, Object>();
headers.put(MongoDbConstants.DATABASE, "otherDB");
headers.put(MongoDbConstants.COLLECTION, "otherCollection");
List<DBObject> objIndex = new ArrayList<DBObject>();
DBObject index1 = new BasicDBObject();
index1.put("a", 1);
DBObject index2 = new BasicDBObject();
index2.put("b", -1);
objIndex.add(index1);
objIndex.add(index2);
headers.put(MongoDbConstants.COLLECTION_INDEX, objIndex);
Object result = template.requestBodyAndHeaders("direct:dynamicityEnabled", body, headers);
assertEquals("Response isn't of type WriteResult", BasicDBObject.class, result.getClass());
MongoCollection<BasicDBObject> dynamicCollection = mongo.getDatabase("otherDB").getCollection("otherCollection", BasicDBObject.class);
ListIndexesIterable<DBObject> indexInfos = dynamicCollection.listIndexes(DBObject.class);
MongoCursor<DBObject> iterator = indexInfos.iterator();
iterator.next();
BasicDBObject key1 = (BasicDBObject) iterator.next().get("key");
BasicDBObject key2 = (BasicDBObject) iterator.next().get("key");
assertTrue("No index on the field a", key1.containsField("a") && "1".equals(key1.getString("a")));
assertTrue("No index on the field b", key2.containsField("b") && "-1".equals(key2.getString("b")));
DBObject b = dynamicCollection.find(new BasicDBObject("_id", "testInsertDynamicityEnabledDBAndCollection")).first();
assertNotNull("No record with 'testInsertDynamicityEnabledDBAndCollection' _id", b);
b = testCollection.find(new BasicDBObject("_id", "testInsertDynamicityEnabledDBOnly")).first();
assertNull("There is a record with 'testInsertDynamicityEnabledDBAndCollection' _id in the test collection", b);
assertTrue("The otherDB database should exist",
StreamSupport.stream(mongo.listDatabaseNames().spliterator(), false)
.anyMatch("otherDB"::equals));
}
示例9: testUpdate
import com.mongodb.client.ListIndexesIterable; //导入依赖的package包/类
@Test
public void testUpdate() {
TestObject o = new TestObject();
o.setName("bar");
o.setValue(new BigDecimal("5"));
Assert.assertEquals("bar", o.getName());
String id = o.getId();
TestObject save = Tester.dao.save(o);
Assert.assertEquals("bar", save.getName());
ABaseTest.assertEquals(new BigDecimal("5"), save.getValue());
Assert.assertNotNull(save.getId());
Assert.assertNotNull(save.getDt());
TestObject find = Tester.dao.findById(id);
Assert.assertNotNull(find);
Assert.assertEquals("bar", find.getName());
ABaseTest.assertEquals(new BigDecimal("5"), find.getValue());
Assert.assertEquals(id, find.getId());
Assert.assertNotNull(find.getDt());
find.setName("blubb");
TestObject save2 = Tester.dao.save(find);
Assert.assertNotNull(save2);
Assert.assertEquals("blubb", save2.getName());
ABaseTest.assertEquals(new BigDecimal("5"), save2.getValue());
Assert.assertEquals(id, save2.getId());
Assert.assertNotNull(save2.getDt());
TestObject find3 = Tester.dao.findByName("blubb");
Assert.assertNotNull(find3);
Assert.assertEquals("blubb", find3.getName());
ABaseTest.assertEquals(new BigDecimal("5"), find3.getValue());
Assert.assertEquals(id, find3.getId());
Assert.assertNotNull(find3.getDt());
Tester.dao.delete(id);
TestObject find2 = Tester.dao.findById(id);
Assert.assertNull(find2);
ListIndexesIterable<Document> listIndexes = ABaseTest.mongo.getDatabase(ABaseTest.dbName).getCollection("TestObject").listIndexes();
MongoCursor<Document> iterator = listIndexes.iterator();
while (iterator.hasNext()) {
Object index = iterator.next();
System.out.println(index.toString());
}
}