jjzjj

mongodb - org.bson.codecs.configuration.CodecConfigurationException : Can't find a codec for class org. springframework.data.mongodb.core.query.GeoCommand

coder 2023-11-07 原文

我正在使用如下聚合:

    final List<AggregationOperation> aggregations = new ArrayList<>();
    Polygon polygon = new Polygon(new Point(-26.28125, 42.19231862526141), new Point(100.28125, 64.7157757187955),
            new Point(100.28125, 42.19231862526141), new Point(-26.28125, 64.7157757187955));
    AggregationOperation match = new MatchOperation(Criteria.where("location").within(polygon));
    aggregationOperations.add(match);
    aggregations.add(project("_id", "location","distance",User.COLLECTION_NAME)
            .and("$geoHash").substring(0,slice).as("geo"));
    aggregations.add(group("geo").count().as("count")
            .avg("location.lng").as("lon")
            .avg("location.lat").as("lat")
            .first(User.COLLECTION_NAME).as(User.COLLECTION_NAME));
    final Aggregation aggregation = newAggregation(aggregations);
    AggregationResults<ClusteredLocation> groupResults =
            mongoTemplate.aggregate(aggregation, UserLocation.COLLECTION_NAME, ClusteredLocation.class);
    return groupResults.getMappedResults();

正在创建的聚合如下: { “聚合”:“集合”,“管道”:[ { “$match”:{ “位置”:{ “$geoWithin”:{ “$java”:org.springframework.data。 mongodb.core.query.GeoCommand@d502fd15 } } } }, { "$lookup": { "from": "users", "localField": "_id", "foreignField": "_id", "as": "用户”}},{“$project”:{“_id”:1,“位置”:1,“距离”:1,“用户”:1,“geo”:{“$substr”:[“$geoHash ", 0, 3] } } }, { "$group": { "_id": "$geo", "count": { "$sum": 1 }, "lon": { "$avg": "$location.lng"}, "lat": { "$avg": "$location.lat"}, "users": { "$first": "$users"} } } ] }

异常如下:

org.bson.codecs.configuration.CodecConfigurationException:找不到类 org.springframework.data.mongodb.core.query.GeoCommand 的编解码器。

我在匹配操作中做错了什么吗?

最佳答案

您可以通过使用 TypedAggregation 或明确提供输入类型来解决此问题。这两种策略都在模板中强制执行查询映射。

TypedAggregation agg = newAggregation(UserLocation.class, aggregations);
mongoTemplate.aggregate(agg, COLLECTION_NAME, ClusteredLocation.class);

// or

Aggregation agg = newAggregation(aggregations);
mongoTemplate.aggregate(agg, UserLocation.class, COLLECTION_NAME, ClusteredLocation.class);

关于mongodb - org.bson.codecs.configuration.CodecConfigurationException : Can't find a codec for class org. springframework.data.mongodb.core.query.GeoCommand,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50474452/

有关mongodb - org.bson.codecs.configuration.CodecConfigurationException : Can't find a codec for class org. springframework.data.mongodb.core.query.GeoCommand的更多相关文章

  1. ruby-on-rails - 测试我的 Ruby gem:Shoulda::Matchers:Module (NoMethodError) 的未定义方法 `configure' - 2

    我正在开发我的第一个Rubygem,并捆绑了cucumber、rspec和shoulda-matches进行测试。当我运行rspec时,出现以下错误:/app/my_gem/spec/spec_helper.rb:6:in`':undefinedmethod`configure'forShoulda::Matchers:Module(NoMethodError)这是我的gem规范:#my_gem.gemspec...Gem::Specification.newdo|spec|......spec.add_development_dependency"activemodel"spec.a

  2. ruby - 如何将 Puma::Configuration 传递给 Sinatra? - 2

    这是我的网络应用:classFront我是这样开始的(请不要建议使用Rack):Front.start!这是我的Puma配置对象,我不知道如何传递给它:require'puma/configuration'Puma::Configuration.new({log_requests:true,debug:true})说真的,怎么样? 最佳答案 配置与您运行的方式紧密相关puma服务器。运行的标准方式puma-pumaCLI命令。为了配置puma配置文件config/puma.rb或config/puma/.rb应该提供(参见examp

  3. ruby-on-rails - 在没有 :total_entries to improve a lengthy query 的情况下使用 will_paginate - 2

    我有一个will_paginate的当前实现,它使用paginate_by_sql方法来构建要分页的集合。我们有一个针对total_entries的自定义查询,它非常复杂并且给我们的数据库带来了很大的负载。因此,我们想从分页中完全删除total_entries。换句话说,我们只需要一个“下一个-上一个”按钮,而不是“上一个1[2]345下一个”的典型分页显示。但我们需要了解一些事情。我们是否显示上一个链接?这当然只会发生在当前选择中显示的记录之前存在的记录我们是否显示下一个链接?如果显示集合中的最后一条记录,则不会显示此内容来自docsAqueryforcountingrowswill

  4. ruby-on-rails - rails 生成 rspec :install config/environments/development. rb:1:in `<top (required)>': 未定义方法 `configure' - 2

    首先,这是我的版本:Greg-Nowickis-MacBook-Pro:sample_appGreg_Nowicki$ruby-vruby2.0.0p451(2014-02-24revision45167)[x86_64-darwin13.1.0]Greg-Nowickis-MacBook-Pro:sample_appGreg_Nowicki$rails-vRails4.0.4我正在学习HartlRails教程并安装rspec进行测试。我已将gem'rspec-rails'添加到我的gemfile中,当我运行railsgeneraterspec:install时,这就是我得到的:Gre

  5. ruby-on-rails - `method_missing':#<Rails::Application::Configuration:0x00> 的未定义方法 `action_mailer' - 2

    我正在构建一个Rails应用程序并且使用的是Rails4.0.1。我有一个错误,并注意到它在3个月前被称为rails上的一个错误,所以我决定:捆绑更新并获得rails4.0.3这样做之后,测试和服务器都不会启动,并且会抛出错误:gems/railties-4.0.3/lib/rails/railtie/configuration.rb:95:in`method_missing':undefinedmethod`action_mailer'for#(NoMethodError)目前我在config/environments/*中注释掉了action_mailer行,但最好能找到一个真正的

  6. ruby - 全新应用程序服务器上的 Rails 'parse_query' 错误 - 2

    我已经在OSX10.10.3上安装了:自制软件,命令行工具,然后使用rbenv安装ruby​​:ruby2.2.2p95(2015-04-13revision50295)[x86_64-darwin14]Rails4.2.2当我创建一个新的应用程序(使用默认的sqlite数据库)并尝试运行它时,它说:=>BootingWEBrick=>Rails4.2.2applicationstartingindevelopmentonhttp://localhost:3000=>Run`railsserver-h`formorestartupoptions=>Ctrl-Ctoshutdownser

  7. ruby-on-rails - 对于新的 ActiveRecord 模型,为什么有些 has_many :through associations add a (1=0) predicate and distinct clause to the sql query? - 2

    每当我实例化一个新的ActiveRecord模型(一个尚未持久化到数据库中的模型)并尝试访问构建模型上的一些各种关联时,Rails查询构建器有时会:将(1=0)谓词添加到查询的where子句。在select语句中添加“distinct”子句。我认为这只会在has_many:through关联连接两个或多个表时发生。我想知道为什么它添加了(1=0)谓词以及distinct子句。对于(1=0)谓词,新模型是否已保存到数据库应该无关紧要(对吧?)。我不知道为什么要添加distinct子句。我在下面有一个简单的例子。classAssignment#s.assignment_attachment

  8. ruby-on-rails - RSpec.configure 和请求对象 - 2

    我有一个作为RESTfulAPI构建的Rails3.1应用程序。计划是根据通过AuthorizationHTTPheader在每个请求上传递的APIkey来处理身份验证。为了在RSpec中对此进行测试,我想在config.beforeblock中设置request.env["HTTP_AUTHORIZATION"]属性:RSpec.configuredo|config|config.mock_with:rspecconfig.use_transactional_fixtures=trueconfig.before(:each)do#SetAPIkeyinAuthorizationhea

  9. ruby-on-rails - 弃用警告 : Dangerous query method (method whose arguments are used as raw SQL) called with non-attribute argument(s) - 2

    我将我的Rails5.1.4应用更新到了5.2.0。我的一个模型中有以下范围:scope:by_category,lambda{|category_slug|category_ids=Category.find_by(slug:category_slug)&.subtree_idswhere(category_id:category_ids)}由于该范围,Rails返回以下错误:DEPRECATIONWARNING:Dangerousquerymethod(methodwhoseargumentsareusedasrawSQL)calledwithnon-attributeargume

  10. ruby - Savon :Module 的未定义方法 `configure' - 2

    我在使用此代码片段的gem中遇到上述错误Savon.configuredo|config|config.log=falseconfig.log_level=:errorHTTPI.log=falseend这段代码过去曾在Travis上运行过,所以我不确定为什么在我更改自述文件时会发生变化。 最佳答案 这种困惑的部分原因是我的情况——继承要维护的gem——以及gemspec中的这一行:gem.add_dependency'savon'没有versionnumberspecified,所以最新的运行切换到使用Savon2,它放弃了Sav

随机推荐