По сути, у меня есть коллекция User
, которая содержит коллекцию @DbRef to UserPlot
(графики). Внутри UserPlot
у меня также есть ссылка @DbRef на Plot
(сюжет). Моя цель - найти всех пользователей и охотно загрузить графики в User
(также охотно загрузить график в UserPlot)
@Data
@NoArgsConstructor
@Document(collection = "users")
public class User {
@Id private String id;
@Indexed(name = "ui_email", unique = true)
private String email;
private String password;
@Indexed(name = "ui_first_name")
private String firstName;
@Indexed(name = "ui_last_name")
private String lastName;
@DBRef
private List<UserPlot> plots;
}
@Data
@Document(collection = "users_have_plots")
public class UserPlot {
@Id
private String id;
private String userId;
@DBRef
private Plot plot;
}
@Data
@Document(collection = "plots")
@NoArgsConstructor
public class Plot {
@Id
private String id;
private Integer x;
private Integer y;
}
@Repository
public interface UserRepository extends ReactiveMongoRepository<User, String> {
@Aggregation(pipeline = {
"{$lookup:{from:'users_have_plots',let:{plots:'$plots'},pipeline:[{$match:{$expr:{$in:['$_id',{$ifNull:['$$plots.$id',[]]}]}}},{$lookup:{from:'plots',localField:'plot.$id',foreignField:'_id',as:'plot'}}],as:'plots'}}"
})
Flux<User> findAllIncludePlots();
}
@SpringBootApplication(exclude = UserDetailsServiceAutoConfiguration.class)
@EnableReactiveMongoRepositories
public class FarmApplication {
public static void main(String[] args) {
SpringApplication.run(FarmApplication.class, args);
}
@Bean
CommandLineRunner t(UserRepository userRepository) {
return a -> {
userRepository.findAllIncludePlots().map(User::getPlots).doOnNext(System.out::println).subscribe();
};
}
}
Когда я вызываю userRepository.findAllIncludePlots().map(User::getPlots).doOnNext(System.out::println).subscribe();
, я получаю эту ошибку:
org.springframework.data.mapping.MappingException: Cannot convert [Document{{_id=6426b069ca83da130cdb7f70, x=0, y=0, _class=com.vilya.farm.domain.model.Plot}}] of type class java.util.ArrayList into an instance of class com.vilya.farm.domain.model.Plot; Implement a custom Converter<class java.util.ArrayList, class com.vilya.farm.domain.model.Plot> and register it with the CustomConversions; Parent object was: UserPlot(id=6426b073ca83da130cdb7f71, userId=6412c76956d4170a7de34d92, plot=null) -> User(id=6412c76956d4170a7de34d92, [email protected], password=$2a$10$s9VgOYd.fOKZF66TnAsjWemiCYkA7aG45NJpuSNgbVxpcIGF7fWqu, firstName=f, lastName=l, plots=null) -> null
Caused by: org.springframework.data.mapping.MappingException: Cannot convert [Document{{_id=6426b069ca83da130cdb7f70, x=0, y=0, _class=com.vilya.farm.domain.model.Plot}}] of type class java.util.ArrayList into an instance of class com.vilya.farm.domain.model.Plot; Implement a custom Converter<class java.util.ArrayList, class com.vilya.farm.domain.model.Plot> and register it with the CustomConversions; Parent object was: UserPlot(id=6426b073ca83da130cdb7f71, userId=6412c76956d4170a7de34d92, plot=null) -> User(id=6412c76956d4170a7de34d92, [email protected], password=$2a$10$s9VgOYd.fOKZF66TnAsjWemiCYkA7aG45NJpuSNgbVxpcIGF7fWqu, firstName=f, lastName=l, plots=null) -> null
at org.springframework.data.mongodb.core.convert.MappingMongoConverter$DefaultConversionContext.convert(MappingMongoConverter.java:2292) ~[spring-data-mongodb-4.0.0.jar:4.0.0]
at org.springframework.data.mongodb.core.convert.MappingMongoConverter$AssociationConversionContext.convert(MappingMongoConverter.java:2208) ~[spring-data-mongodb-4.0.0.jar:4.0.0]
at org.springframework.data.mongodb.core.convert.MappingMongoConverter$AssociationConversionContext.convert(MappingMongoConverter.java:2208) ~[spring-data-mongodb-4.0.0.jar:4.0.0]
at org.springframework.data.mongodb.core.convert.MappingMongoConverter$AssociationConversionContext.convert(MappingMongoConverter.java:2208) ~[spring-data-mongodb-4.0.0.jar:4.0.0]
at org.springframework.data.mongodb.core.convert.MappingMongoConverter$AssociationConversionContext.convert(MappingMongoConverter.java:2208) ~[spring-data-mongodb-4.0.0.jar:4.0.0]
at org.springframework.data.mongodb.core.convert.MappingMongoConverter$ConversionContext.convert(MappingMongoConverter.java:2131) ~[spring-data-mongodb-4.0.0.jar:4.0.0]
at org.springframework.data.mongodb.core.convert.MappingMongoConverter$MongoDbPropertyValueProvider.getPropertyValue(MappingMongoConverter.java:1900) ~[spring-data-mongodb-4.0.0.jar:4.0.0]
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.getValueInternal(MappingMongoConverter.java:1359) ~[spring-data-mongodb-4.0.0.jar:4.0.0]
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.lambda$getDbRefResolverCallback$1(MappingMongoConverter.java:632) ~[spring-data-mongodb-4.0.0.jar:4.0.0]
at org.springframework.data.mongodb.core.convert.DefaultDbRefResolverCallback.resolve(DefaultDbRefResolverCallback.java:57) ~[spring-data-mongodb-4.0.0.jar:4.0.0]
at org.springframework.data.mongodb.core.convert.DefaultDbRefResolver.resolveDbRef(DefaultDbRefResolver.java:85) ~[spring-data-mongodb-4.0.0.jar:4.0.0]
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.readAssociation(MappingMongoConverter.java:673) ~[spring-data-mongodb-4.0.0.jar:4.0.0]
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.readProperties(MappingMongoConverter.java:597) ~[spring-data-mongodb-4.0.0.jar:4.0.0]
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.populateProperties(MappingMongoConverter.java:535) ~[spring-data-mongodb-4.0.0.jar:4.0.0]
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:513) ~[spring-data-mongodb-4.0.0.jar:4.0.0]
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.readDocument(MappingMongoConverter.java:477) ~[spring-data-mongodb-4.0.0.jar:4.0.0]
at org.springframework.data.mongodb.core.convert.MappingMongoConverter$DefaultConversionContext.convert(MappingMongoConverter.java:2325) ~[spring-data-mongodb-4.0.0.jar:4.0.0]
at org.springframework.data.mongodb.core.convert.MappingMongoConverter$AssociationConversionContext.convert(MappingMongoConverter.java:2208) ~[spring-data-mongodb-4.0.0.jar:4.0.0]
at org.springframework.data.mongodb.core.convert.MappingMongoConverter$AssociationConversionContext.convert(MappingMongoConverter.java:2208) ~[spring-data-mongodb-4.0.0.jar:4.0.0]
at org.springframework.data.mongodb.core.convert.MappingMongoConverter$ConversionContext.convert(MappingMongoConverter.java:2131) ~[spring-data-mongodb-4.0.0.jar:4.0.0]
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.readCollectionOrArray(MappingMongoConverter.java:1402) ~[spring-data-mongodb-4.0.0.jar:4.0.0]
at org.springframework.data.mongodb.core.convert.MappingMongoConverter$DefaultConversionContext.convert(MappingMongoConverter.java:2297) ~[spring-data-mongodb-4.0.0.jar:4.0.0]
at org.springframework.data.mongodb.core.convert.MappingMongoConverter$AssociationConversionContext.convert(MappingMongoConverter.java:2208) ~[spring-data-mongodb-4.0.0.jar:4.0.0]
at org.springframework.data.mongodb.core.convert.MappingMongoConverter$AssociationConversionContext.convert(MappingMongoConverter.java:2208) ~[spring-data-mongodb-4.0.0.jar:4.0.0]
at org.springframework.data.mongodb.core.convert.MappingMongoConverter$ConversionContext.convert(MappingMongoConverter.java:2131) ~[spring-data-mongodb-4.0.0.jar:4.0.0]
at org.springframework.data.mongodb.core.convert.MappingMongoConverter$MongoDbPropertyValueProvider.getPropertyValue(MappingMongoConverter.java:1900) ~[spring-data-mongodb-4.0.0.jar:4.0.0]
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.getValueInternal(MappingMongoConverter.java:1359) ~[spring-data-mongodb-4.0.0.jar:4.0.0]
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.lambda$getDbRefResolverCallback$1(MappingMongoConverter.java:632) ~[spring-data-mongodb-4.0.0.jar:4.0.0]
at org.springframework.data.mongodb.core.convert.DefaultDbRefResolverCallback.resolve(DefaultDbRefResolverCallback.java:57) ~[spring-data-mongodb-4.0.0.jar:4.0.0]
at org.springframework.data.mongodb.core.convert.DefaultDbRefResolver.resolveDbRef(DefaultDbRefResolver.java:85) ~[spring-data-mongodb-4.0.0.jar:4.0.0]
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.readAssociation(MappingMongoConverter.java:673) ~[spring-data-mongodb-4.0.0.jar:4.0.0]
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.readProperties(MappingMongoConverter.java:597) ~[spring-data-mongodb-4.0.0.jar:4.0.0]
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.populateProperties(MappingMongoConverter.java:535) ~[spring-data-mongodb-4.0.0.jar:4.0.0]
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:513) ~[spring-data-mongodb-4.0.0.jar:4.0.0]
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.readDocument(MappingMongoConverter.java:477) ~[spring-data-mongodb-4.0.0.jar:4.0.0]
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:413) ~[spring-data-mongodb-4.0.0.jar:4.0.0]
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:409) ~[spring-data-mongodb-4.0.0.jar:4.0.0]
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:121) ~[spring-data-mongodb-4.0.0.jar:4.0.0]
at org.springframework.data.mongodb.core.ReactiveMongoTemplate$ReadDocumentCallback.doWith(ReactiveMongoTemplate.java:2914) ~[spring-data-mongodb-4.0.0.jar:4.0.0]
at reactor.core.publisher.FluxConcatMapNoPrefetch$FluxConcatMapNoPrefetchSubscriber.onNext(FluxConcatMapNoPrefetch.java:183) ~[reactor-core-3.5.0.jar:3.5.0]
at reactor.core.publisher.FluxCreate$BufferAsyncSink.drain(FluxCreate.java:814) ~[reactor-core-3.5.0.jar:3.5.0]
at reactor.core.publisher.FluxCreate$BufferAsyncSink.next(FluxCreate.java:739) ~[reactor-core-3.5.0.jar:3.5.0]
at reactor.core.publisher.FluxCreate$SerializedFluxSink.next(FluxCreate.java:161) ~[reactor-core-3.5.0.jar:3.5.0]
at java.base/java.util.ArrayList.forEach(ArrayList.java:1511) ~[na:na]
at com.mongodb.reactivestreams.client.internal.BatchCursorFlux.lambda$recurseCursor$4(BatchCursorFlux.java:98) ~[mongodb-driver-reactivestreams-4.8.0.jar:na]
at reactor.core.publisher.MonoPeekTerminal$MonoTerminalPeekSubscriber.onNext(MonoPeekTerminal.java:171) ~[reactor-core-3.5.0.jar:3.5.0]
at reactor.core.publisher.MonoPeekTerminal$MonoTerminalPeekSubscriber.onNext(MonoPeekTerminal.java:180) ~[reactor-core-3.5.0.jar:3.5.0]
at reactor.core.publisher.FluxPeekFuseable$PeekConditionalSubscriber.onNext(FluxPeekFuseable.java:854) ~[reactor-core-3.5.0.jar:3.5.0]
at reactor.core.publisher.MonoCreate$DefaultMonoSink.success(MonoCreate.java:172) ~[reactor-core-3.5.0.jar:3.5.0]
at com.mongodb.reactivestreams.client.internal.MongoOperationPublisher.lambda$sinkToCallback$31(MongoOperationPublisher.java:577) ~[mongodb-driver-reactivestreams-4.8.0.jar:na]
at com.mongodb.internal.operation.AsyncQueryBatchCursor.next(AsyncQueryBatchCursor.java:173) ~[mongodb-driver-core-4.8.0.jar:na]
at com.mongodb.reactivestreams.client.internal.BatchCursor.lambda$next$0(BatchCursor.java:38) ~[mongodb-driver-reactivestreams-4.8.0.jar:na]
at reactor.core.publisher.MonoCreate.subscribe(MonoCreate.java:58) ~[reactor-core-3.5.0.jar:3.5.0]
at reactor.core.publisher.Mono.subscribe(Mono.java:4444) ~[reactor-core-3.5.0.jar:3.5.0]
at reactor.core.publisher.Mono.subscribeWith(Mono.java:4510) ~[reactor-core-3.5.0.jar:3.5.0]
at reactor.core.publisher.Mono.subscribe(Mono.java:4272) ~[reactor-core-3.5.0.jar:3.5.0]
at com.mongodb.reactivestreams.client.internal.BatchCursorFlux.recurseCursor(BatchCursorFlux.java:108) ~[mongodb-driver-reactivestreams-4.8.0.jar:na]
at com.mongodb.reactivestreams.client.internal.BatchCursorFlux.lambda$subscribe$0(BatchCursorFlux.java:60) ~[mongodb-driver-reactivestreams-4.8.0.jar:na]
at reactor.core.publisher.LambdaMonoSubscriber.onNext(LambdaMonoSubscriber.java:171) ~[reactor-core-3.5.0.jar:3.5.0]
at reactor.core.publisher.FluxMap$MapSubscriber.onNext(FluxMap.java:122) ~[reactor-core-3.5.0.jar:3.5.0]
at reactor.core.publisher.MonoNext$NextSubscriber.onNext(MonoNext.java:82) ~[reactor-core-3.5.0.jar:3.5.0]
at reactor.core.publisher.MonoNext$NextSubscriber.onNext(MonoNext.java:82) ~[reactor-core-3.5.0.jar:3.5.0]
at reactor.core.publisher.MonoFlatMap$FlatMapMain.secondComplete(MonoFlatMap.java:245) ~[reactor-core-3.5.0.jar:3.5.0]
at reactor.core.publisher.MonoFlatMap$FlatMapInner.onNext(MonoFlatMap.java:305) ~[reactor-core-3.5.0.jar:3.5.0]
at reactor.core.publisher.MonoPeekTerminal$MonoTerminalPeekSubscriber.onNext(MonoPeekTerminal.java:180) ~[reactor-core-3.5.0.jar:3.5.0]
at reactor.core.publisher.MonoCreate$DefaultMonoSink.success(MonoCreate.java:172) ~[reactor-core-3.5.0.jar:3.5.0]
at com.mongodb.reactivestreams.client.internal.MongoOperationPublisher.lambda$sinkToCallback$31(MongoOperationPublisher.java:577) ~[mongodb-driver-reactivestreams-4.8.0.jar:na]
at com.mongodb.reactivestreams.client.internal.OperationExecutorImpl.lambda$execute$2(OperationExecutorImpl.java:94) ~[mongodb-driver-reactivestreams-4.8.0.jar:na]
at com.mongodb.internal.async.ErrorHandlingResultCallback.onResult(ErrorHandlingResultCallback.java:46) ~[mongodb-driver-core-4.8.0.jar:na]
at com.mongodb.internal.async.function.AsyncCallbackSupplier.lambda$whenComplete$1(AsyncCallbackSupplier.java:97) ~[mongodb-driver-core-4.8.0.jar:na]
at com.mongodb.internal.async.function.RetryingAsyncCallbackSupplier$RetryingCallback.onResult(RetryingAsyncCallbackSupplier.java:116) ~[mongodb-driver-core-4.8.0.jar:na]
at com.mongodb.internal.async.ErrorHandlingResultCallback.onResult(ErrorHandlingResultCallback.java:46) ~[mongodb-driver-core-4.8.0.jar:na]
at com.mongodb.internal.async.function.AsyncCallbackSupplier.lambda$whenComplete$1(AsyncCallbackSupplier.java:97) ~[mongodb-driver-core-4.8.0.jar:na]
at com.mongodb.internal.async.ErrorHandlingResultCallback.onResult(ErrorHandlingResultCallback.java:46) ~[mongodb-driver-core-4.8.0.jar:na]
at com.mongodb.internal.async.function.AsyncCallbackSupplier.lambda$whenComplete$1(AsyncCallbackSupplier.java:97) ~[mongodb-driver-core-4.8.0.jar:na]
at com.mongodb.internal.operation.CommandOperationHelper.lambda$transformingReadCallback$10(CommandOperationHelper.java:332) ~[mongodb-driver-core-4.8.0.jar:na]
at com.mongodb.internal.async.ErrorHandlingResultCallback.onResult(ErrorHandlingResultCallback.java:46) ~[mongodb-driver-core-4.8.0.jar:na]
at com.mongodb.internal.connection.DefaultServer$DefaultServerProtocolExecutor$1.onResult(DefaultServer.java:242) ~[mongodb-driver-core-4.8.0.jar:na]
at com.mongodb.internal.async.ErrorHandlingResultCallback.onResult(ErrorHandlingResultCallback.java:46) ~[mongodb-driver-core-4.8.0.jar:na]
at com.mongodb.internal.connection.CommandProtocolImpl$1.onResult(CommandProtocolImpl.java:84) ~[mongodb-driver-core-4.8.0.jar:na]
at com.mongodb.internal.connection.DefaultConnectionPool$PooledConnection$1.onResult(DefaultConnectionPool.java:683) ~[mongodb-driver-core-4.8.0.jar:na]
at com.mongodb.internal.connection.UsageTrackingInternalConnection$2.onResult(UsageTrackingInternalConnection.java:159) ~[mongodb-driver-core-4.8.0.jar:na]
at com.mongodb.internal.async.ErrorHandlingResultCallback.onResult(ErrorHandlingResultCallback.java:46) ~[mongodb-driver-core-4.8.0.jar:na]
at com.mongodb.internal.connection.InternalStreamConnection$2$1.onResult(InternalStreamConnection.java:524) ~[mongodb-driver-core-4.8.0.jar:na]
at com.mongodb.internal.connection.InternalStreamConnection$2$1.onResult(InternalStreamConnection.java:501) ~[mongodb-driver-core-4.8.0.jar:na]
at com.mongodb.internal.connection.InternalStreamConnection$MessageHeaderCallback$MessageCallback.onResult(InternalStreamConnection.java:824) ~[mongodb-driver-core-4.8.0.jar:na]
at com.mongodb.internal.connection.InternalStreamConnection$MessageHeaderCallback$MessageCallback.onResult(InternalStreamConnection.java:788) ~[mongodb-driver-core-4.8.0.jar:na]
at com.mongodb.internal.connection.InternalStreamConnection$5.completed(InternalStreamConnection.java:648) ~[mongodb-driver-core-4.8.0.jar:na]
at com.mongodb.internal.connection.InternalStreamConnection$5.completed(InternalStreamConnection.java:645) ~[mongodb-driver-core-4.8.0.jar:na]
at com.mongodb.connection.netty.NettyStream.readAsync(NettyStream.java:319) ~[mongodb-driver-core-4.8.0.jar:na]
at com.mongodb.connection.netty.NettyStream.readAsync(NettyStream.java:266) ~[mongodb-driver-core-4.8.0.jar:na]
at com.mongodb.internal.connection.InternalStreamConnection.readAsync(InternalStreamConnection.java:645) ~[mongodb-driver-core-4.8.0.jar:na]
at com.mongodb.internal.connection.InternalStreamConnection.access$600(InternalStreamConnection.java:89) ~[mongodb-driver-core-4.8.0.jar:na]
at com.mongodb.internal.connection.InternalStreamConnection$MessageHeaderCallback.onResult(InternalStreamConnection.java:778) ~[mongodb-driver-core-4.8.0.jar:na]
at com.mongodb.internal.connection.InternalStreamConnection$MessageHeaderCallback.onResult(InternalStreamConnection.java:763) ~[mongodb-driver-core-4.8.0.jar:na]
at com.mongodb.internal.connection.InternalStreamConnection$5.completed(InternalStreamConnection.java:648) ~[mongodb-driver-core-4.8.0.jar:na]
at com.mongodb.internal.connection.InternalStreamConnection$5.completed(InternalStreamConnection.java:645) ~[mongodb-driver-core-4.8.0.jar:na]
at com.mongodb.connection.netty.NettyStream.readAsync(NettyStream.java:319) ~[mongodb-driver-core-4.8.0.jar:na]
at com.mongodb.connection.netty.NettyStream.handleReadResponse(NettyStream.java:347) ~[mongodb-driver-core-4.8.0.jar:na]
at com.mongodb.connection.netty.NettyStream.access$1100(NettyStream.java:105) ~[mongodb-driver-core-4.8.0.jar:na]
at com.mongodb.connection.netty.NettyStream$InboundBufferHandler.channelRead0(NettyStream.java:421) ~[mongodb-driver-core-4.8.0.jar:na]
at com.mongodb.connection.netty.NettyStream$InboundBufferHandler.channelRead0(NettyStream.java:418) ~[mongodb-driver-core-4.8.0.jar:na]
at io.netty.channel.SimpleChannelInboundHandler.channelRead(SimpleChannelInboundHandler.java:99) ~[netty-transport-4.1.85.Final.jar:4.1.85.Final]
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:444) ~[netty-transport-4.1.85.Final.jar:4.1.85.Final]
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:420) ~[netty-transport-4.1.85.Final.jar:4.1.85.Final]
at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:412) ~[netty-transport-4.1.85.Final.jar:4.1.85.Final]
at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1410) ~[netty-transport-4.1.85.Final.jar:4.1.85.Final]
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:440) ~[netty-transport-4.1.85.Final.jar:4.1.85.Final]
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:420) ~[netty-transport-4.1.85.Final.jar:4.1.85.Final]
at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:919) ~[netty-transport-4.1.85.Final.jar:4.1.85.Final]
at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:166) ~[netty-transport-4.1.85.Final.jar:4.1.85.Final]
at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:788) ~[netty-transport-4.1.85.Final.jar:4.1.85.Final]
at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:724) ~[netty-transport-4.1.85.Final.jar:4.1.85.Final]
at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:650) ~[netty-transport-4.1.85.Final.jar:4.1.85.Final]
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:562) ~[netty-transport-4.1.85.Final.jar:4.1.85.Final]
at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:997) ~[netty-common-4.1.85.Final.jar:4.1.85.Final]
at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74) ~[netty-common-4.1.85.Final.jar:4.1.85.Final]
at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30) ~[netty-common-4.1.85.Final.jar:4.1.85.Final]
at java.base/java.lang.Thread.run(Thread.java:833) ~[na:na]
Я думаю, что проблема с моим @Aggregation
, но я не могу найти причину. Пожалуйста помоги. Спасибо
Обновлено: мои данные выглядят так
vilya_be> db.plots.find({})
[
{
_id: ObjectId("6426b069ca83da130cdb7f70"),
x: 0,
y: 0,
_class: 'com.vilya.farm.domain.model.Plot'
}
]
vilya_be> db.users_have_plots.find({})
[
{
_id: ObjectId("6426b073ca83da130cdb7f71"),
userId: '6412c76956d4170a7de34d92',
plot: DBRef("plots", ObjectId("6426b069ca83da130cdb7f70")),
_class: 'com.vilya.farm.domain.model.UserPlot'
}
]
vilya_be> db.users.find({})
[
{
_id: ObjectId("6412c76956d4170a7de34d92"),
email: '[email protected]',
password: '$2a$10$s9VgOYd.fOKZF66TnAsjWemiCYkA7aG45NJpuSNgbVxpcIGF7fWqu',
firstName: 'f',
lastName: 'l',
plots: [ DBRef("users_have_plots", ObjectId("6426b073ca83da130cdb7f71")) ],
_class: 'com.vilya.farm.domain.model.User'
},
{
_id: ObjectId("6414667360e4ba4481052627"),
email: '[email protected]',
password: '$2a$10$OP52phZ61l2JX2e2TQOu9ubYFBYcPeqEZ92ox2Nyyp5e.MEZk7GhS',
firstName: 'f',
lastName: 'l',
_class: 'com.vilya.farm.domain.model.User'
}
]
Версия монго: монго: 6.0.2-focal работает на рабочем столе докера
введите класс java.util.ArrayList в экземпляр класса com.vilya.farm.domain.model.Plot;
Ошибка говорит о том, что вы получаете массив вместо объекта по ключу сюжета. Если вы проверите свой документ UserPlot
, у вас есть Plot как свойство, а не массив. Таким образом, агрегация из второго поиска возвращает массив.
Итак, вам нужно использовать $unwind
для преобразования массива в объект в агрегации. Поскольку у нас всегда есть 1 участок, мы можем его использовать. Документация $unwind
Обновленная агрегация:
{$lookup:{from:'users_have_plots',let:{plots:'$plots'},pipeline:[{$match:{$expr:{$in:['$_id',{$ifNull:['$$plots.$id',[]]}]}}},{$lookup:{from:'plots',localField:'plot.$id',foreignField:'_id',as:'plot'}},{$unwind: '$plot'}],as:'plots'}}
Дает результат объект в сюжете:
[
{
_id: ObjectId("6412c76956d4170a7de34d92"),
email: '[email protected]',
password: '$2a$10$s9VgOYd.fOKZF66TnAsjWemiCYkA7aG45NJpuSNgbVxpcIGF7fWqu',
firstName: 'f',
lastName: 'l',
plots: [
{
_id: ObjectId("6426b073ca83da130cdb7f71"),
userId: '6412c76956d4170a7de34d92',
plot: {
_id: ObjectId("6426b069ca83da130cdb7f70"),
x: 0,
y: 0,
_class: 'com.vilya.farm.domain.model.Plot'
},
_class: 'com.vilya.farm.domain.model.UserPlot'
}
],
_class: 'com.vilya.farm.domain.model.User'
},
{
_id: ObjectId("6414667360e4ba4481052627"),
email: '[email protected]',
password: '$2a$10$OP52phZ61l2JX2e2TQOu9ubYFBYcPeqEZ92ox2Nyyp5e.MEZk7GhS',
firstName: 'f',
lastName: 'l',
_class: 'com.vilya.farm.domain.model.User',
plots: []
}
]