Tag: spring data mongodb

Spring Data Mongodb存储库不能正确实现inheritance

有两种类型的实体,映射到单个MongoDB集合中的两个Java类: @Document public class Superclass { … } @Document(collection = “superclass”) public class Subclass extends Superclass { … } 和这些实体的两个存储库: public interface SuperclassRepository extends MongoRepository {} public interface SubclassRepository extends MongoRepository {} MongoRepositories无法正确处理实体的inheritance。 在查询所有Subclass对象(例如SubclassRepository.findAll() )时,结果集包含Superclass对象,这些对象被实例化(或者至少已经尝试实例化),对于属于Subclass字段具有空值,但不是Superclass一部分。 预期的结果是SubclassRepository应该只返回Subclass对象,而SuperclassRepository应该返回Superclass和Subclass对象。 它在Spring Data JPA中以这种方式工作。 有没有人遇到过这个bug,并且有解决方法吗?

@CompoundIndex在Spring Data MongoDB中不起作用

我正在使用Spring Data MongoDB开发应用程序。 我想在我的一个模型上创建一个复合索引。 我在顶部添加了@CompoundIndex注释,如下所示: @Document @CompoundIndexes({ @CompoundIndex(name = “name_”, def = “{ ‘tenantId’: 1, ‘name’: 1 }”, unique = true) }) public class MyModel { } 但是,不会创建索引。 我也尝试将@CompoundIndex直接放在类上面。 该集合仍然缺少索引。 创建时,相同的索引定义正常工作: mongoTemplate.indexOps(MyModel.class).ensureIndex(new Index().named(“name_”).on(“tenantId”, Direction.ASC).on(“name”, Direction.ASC).unique()); 我更喜欢使用索引的基于注释的定义。 任何想法为什么这不起作用?

Spring数据mongodb审计无法正常工作..(Java配置)

我目前正在使用Spring数据mongodb 1.6.0-RELEASE,我知道它有审计function。 我把@EnableMongoAuditing注释放在我的配置类之上。 我的豆子在下面: @Document public class MyBean{ @Id private AnotherCustomBean anotherCustomBean = new AnotherCustomBean(); @CreatedDate private Date creationDate; @LastModifiedDate private Date lastModifiedDate; . . . 当我用mongoTemplate.save(myBean);保存这个bean时mongoTemplate.save(myBean); 它没有设置创建日期和上次修改日期…它没有错误。 任何帮助,将不胜感激, 谢谢。

用mongoTemplate分页

我有一个可查询的查询: Query query = new Query().with(new PageRequests(page, size)) 如何使用MongoTemplate执行它? 我没有看到返回Page的单个方法。

如何配置spring-data-mongodb以通过属性使用副本集

我目前正在编写一个应该使用MongoDB副本的应用程序。 它是一个基于Spring Boot的应用程序,以下属性可以很好地连接到一台服务器: spring.data.mongodb.host=localhost spring.data.mongodb.port=27017 spring.data.mongodb.database=demo 这对我的本地开发环境来说绝对没问题。 但是后来它应该针对MongoDB副本集运行,所以我必须提供至少2个,更好的3个副本集种子,但我怎么能用属性来做呢? 我在这个页面上看了一下: http : //docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html ,但是没有提到的副本集的显式属性。 提供以逗号分隔的地址列表,如下所示: spring.data.mongodb.host=127.0.0.1,127.0.1.1,127.0.2.1 spring.data.mongodb.uri=mongo://127.0.0.1,mongo://127.0.0.1:27018 (我一个接一个地试过。) 这也不起作用(实际上,它会产生一个exception,让Spring使用默认配置)。 我也尝试使用以下config.xml,没有运气: 我知道上面的配置略有不同,但我目前正在尝试的是获得一个exception,它向我显示没有可访问的副本集节点。 任何想法,提示?

如何在spring-boot中禁用spring-data-mongodb自动配置

有没有人尝试在spring-boot中禁用mongodb的自动配置? 我正在尝试使用spring-data-mongodb进行spring-boot; 使用基于java的配置; 使用spring-boot 1.2.1.RELEASE,我导入spring-boot-starter-web及其父pom进行依赖管理。 我还导入spring-data-mongodb(尝试过spring-boot-starter-mongodb)。 我需要连接到两个不同的MongoDB服务器。 所以我需要为mongo连接,MongoTemplate等配置两组实例。 我还想禁用自动配置。 由于我连接到多个服务器,因此我不需要自动配置单个默认的MongoTemplate和GridFsTemplate bean。 我的主要课程如下: @Configuration @EnableAutoConfiguration(exclude={MongoAutoConfiguration.class, MongoDataAutoConfiguration.class}) @ComponentScan //@SpringBootApplication // @Configuration @EnableAutoConfiguration @ComponentScan public class MainRunner { public static void main(String[] args) { SpringApplication.run(MainRunner.class, args); } } 我的两个mongo配置类看起来像这样: @Configuration @EnableMongoRepositories(basePackageClasses = {Test1Repository.class}, mongoTemplateRef = “template1”, includeFilters = {@ComponentScan.Filter(type = FilterType.REGEX, pattern = “.*Test1Repository”)} ) public class Mongo1Config […]

Spring Data MongoDB和批量更新

我正在使用Spring Data MongoDB,并希望像下面描述的那样执行批量更新: http : //docs.mongodb.org/manual/reference/method/Bulk.find.update/#Bulk.find.update 使用常规驱动程序时,它看起来像这样: 以下示例初始化项集合的Bulk()操作构建器,并将各种多更新操作添加到操作列表中。 var bulk = db.items.initializeUnorderedBulkOp(); bulk.find( { status: “D” } ).update( { $set: { status: “I”, points: “0” } } ); bulk.find( { item: null } ).update( { $set: { item: “TBD” } } ); bulk.execute() 有没有办法用Spring Data MongoDB实现类似的结果?

使用部分定义的模式映射文档

我正在使用Spring和MongoDB作为数据库编写演示应用程序。 我的主要域类看起来像: @Document public class Person { @Id private String id; //Some other fields private DBObject additionalData; } 关键是additionalData是一个没有指定模式的子文档,它是一种用户定义的JSON。 但是当我解析这个json(使用(DBObject)JSON.parse(value)表达式)时,它在MongoDB中存储为一个字符串,我需要它是一个嵌套的文档结构。 搜索了几个小时,没有找到解决方案。 有任何想法吗?

如何使用spring数据在mongo中运行js文件

在mongo shell中,可以使用load命令运行js文件: load(“path/to/file/file.js”) 如何使用spring-data做到这一点? 或者Java中的任何其他方式。 我试过了: BasicDBObject obj = new BasicDBObject(); obj.append( “$load” , “/path/file.js” ); CommandResult t=mongoTemplate.executeCommand(obj); 和: obj.append( “$eval” , “load(\”/path/file.js\”)” ); 但它不起作用。

Spring数据mongodb搜索ISO日期

我正在尝试使用查询搜索出生日期 criteria = Criteria.where(“dob”).lte(new DateTime().toDate()); 并且spring数据mongodb生成以下查询: MongoTemplate:使用查询查找: { “dob” : { “$lte” : { “$date” : “2015-05-16T07:55:23.257Z”}}} fields:类为null:类com.temp.model.User in collection:user 但我没有得到任何结果。 我在mongodb的dob字段: {“dob” : ISODate(“1991-01-23T00:00:00Z”)} 我如何以ISODate格式搜索dob ?