mongoose findOneAndUpdate返回更新的文档,但不更新数据库

时间:2018-01-31 15:27:41

标签: node.js mongodb mongoose mongoose-schema

考虑以下mongoose查询:

@SpringBootApplication
@EnableBinding(Sink.class)
public class So48543143Application {

    public static void main(String[] args) {
        SpringApplication.run(So48543143Application.class, args);
    }

    @Bean
    public ApplicationRunner runner(MessageChannel routeChannel) {
        return args -> {
            routeChannel.send(new GenericMessage<>("foo"));
        };
    }

    @ServiceActivator(inputChannel = "routeChannel")
    @Bean
    public AbstractMappingMessageRouter router(MyBinderAwareChannelResolver resolver) {
        ExpressionEvaluatingRouter router = new ExpressionEvaluatingRouter(new LiteralExpression("foo"));
        router.setDefaultOutputChannelName("default");
        router.setChannelResolver(resolver);
        return router;
    }

    @Bean
    public MyBinderAwareChannelResolver binderAwareChannelResolver(BindingService bindingService,
            AbstractBindingTargetFactory<? extends MessageChannel> bindingTargetFactory,
            DynamicDestinationsBindable dynamicDestinationsBindable) {
        return new MyBinderAwareChannelResolver(bindingService, bindingTargetFactory, dynamicDestinationsBindable);
    }

    public static class MyBinderAwareChannelResolver extends BinderAwareChannelResolver {

        public MyBinderAwareChannelResolver(BindingService bindingService,
                AbstractBindingTargetFactory<? extends MessageChannel> bindingTargetFactory,
                DynamicDestinationsBindable dynamicDestinationsBindable) {
            super(bindingService, bindingTargetFactory, dynamicDestinationsBindable);
        }

        @Override
        public MessageChannel resolveDestination(String channelName) {
            MessageChannel channel = super.resolveDestination(channelName);
            DirectFieldAccessor dfa = new DirectFieldAccessor(channel);
            AbstractDispatcher dispatcher = (AbstractDispatcher) dfa.getPropertyValue("dispatcher");
            dfa = new DirectFieldAccessor(dispatcher);
            @SuppressWarnings("unchecked")
            Set<MessageHandler> handlers = (Set<MessageHandler>) dfa.getPropertyValue("handlers");
            // there should be exactly one handler
            MessageHandler handler = handlers.iterator().next();
            dfa = new DirectFieldAccessor(handler);
            dfa.setPropertyValue("embedHeaders", false);
            return channel;
        }

    }

}

这会返回包含属性mongoose.model(collection).findOneAndUpdate({[fieldName]: fieldValue}, {$set:{processed:1}}, {new:true}, function(err, doc){ console.log(doc); callback(err, doc); }); 的文档,但它不会更新mongodb表。

我没有在mongoose架构上设置已处理的属性。因此架构将类似于:

processed:1

我可以设置任何选项,以便在更新数据库记录的同时返回更新的文档吗?

2 个答案:

答案 0 :(得分:1)

Mongoose只能使用模式中定义的字段更新表。因此,如果Mongoose模式中不存在processed字段,则不会更新表。

答案 1 :(得分:0)

您可以在更新时设置strict选项属性。这将覆盖架构,并使您可以添加所需的不在定义架构中的任何属性。

model.findOneAndUpdate(where, data, { ...options, strict: true });

请参阅:http://mongoosejs.com/docs/guide.html#strict

相关问题