假设我有一个收藏,即a。
我想得到这个收藏所有有特别价值的钥匙,比如“你好”
{
"a": "its me hello",
"b": "it does not have value",
"c": "It has hello"
}
在这种情况下,我想查询返回a和c键。其中包含字符串“hello”。
有什么办法可以在 Spring 启动?
我为蒙哥炮弹找到了答案。链接到在mongoshell中工作的答案。
但无法使用mongorepository在spring boot中转换它。
我正在尝试以下代码-
try {
MongoClientURI uri = new MongoClientURI(connectionRequest.getUri());
MongoClient mongoClient = new MongoClient(uri);
for (String databaseName : mongoClient.listDatabaseNames()) {
logger.info("***Database:***" + databaseName);
MongoDatabase database = mongoClient.getDatabase(databaseName);
if(database.getName().equals("forum")|| database.getName().equals("kidsventure")) {
MongoIterable<String> collections = database.listCollectionNames();
for (String collectionName : collections) {
logger.info("***Collection:***" + collectionName);
MongoCollection<Document> mongoCollection = database.getCollection(collectionName);
Aggregation aggregation = Aggregation.newAggregation(Aggregation.project(Aggregation.ROOT), Aggregation.match(Criteria.where(connectionRequest.getWord())));
System.out.println(aggregation);
Iterable<Document> fields = mongoCollection.find();
fields.forEach(field -> {
String json = field.toJson();
JsonObject jsonObject = new JsonParser().parse(json).getAsJsonObject();
for (Map.Entry<String, JsonElement> entry : jsonObject.entrySet()) {
System.out.println("***key***" + entry.getKey() + "***value***" + entry.getValue().toString());
System.out.println(entry.getValue().toString().toLowerCase().equals(connectionRequest.getWord().toLowerCase()));
System.out.println(entry.getValue().getAsString().toLowerCase().equals(connectionRequest.getWord().toLowerCase()));
}
// for (Map.Entry<String, JsonElement> entry : jsonObject.entrySet()) {
// if (entry.getValue().getAsString().equals(connectionRequest.getUri())) {
// System.out.println("***key***" + entry.getKey());
// }
// }
});
}
}
}
} catch (Exception e) {
logger.info("***Error connecting!***" + e.getMessage());
}
1条答案
按热度按时间pftdvrlh1#
AggregateIterable<Document> queryResult = mongoCollection.aggregate(Arrays.asList (new Document("$project", new Document("data", new Document("$objectToArray", "$$ROOT"))),new Document("$unwind", "$data"),new Document("$match", new Document("data.v",new Document("$regex", "hello")))));
参考