【问题标题】:spring data mongo find method has no returnspring data mongo find方法没有返回
【发布时间】:2023-04-05 04:12:01
【问题描述】:

我用的是spring scheduler+spring data mongo。

现在我遇到一个问题。MongoTemple 上的 find() 方法会阻塞。

程序块开启

List<HistoricalDataModal> historicalList = mongoOperation.find(symbolQuery, HistoricalDataModal.class,"EibHistoricalDataInf");

更新完整代码:

我将 find 替换为 findOne,总是阻止 Java代码

  public void realtime() {
          try {
                Query query = new Query();
                List<ContractDetails> contractDetailList = mongoOperation.find(query, ContractDetails.class,"EibContractInf");
                if(contractDetailList == null || contractDetailList.size() == 0) {
                    logger.error("contractdetail error");
                    return;
                }

                for(int index = 0;index < contractDetailList.size();index++) {
                    Query symbolQuery = new Query();
                    symbolQuery.addCriteria(Criteria.where("symbol").is(contractDetailList.get(index).contract().symbol()));
                    FundamentalModal fundamentalDetail = mongoOperation.findOne(symbolQuery, FundamentalModal.class,"EibFundamentalInf");
                    if(fundamentalDetail == null)
                    {
                        logger.error("fundamentalDetail error");
                        continue;
                    }
                    logger.debug("debug1");

                    logger.debug("debug");

                    /*List<*/HistoricalDataModal/*>*/ historicalList = mongoOperation.findOne(symbolQuery, HistoricalDataModal.class,"EibHistoricalDataInf");
                    logger.debug("debug");
                    if(historicalList == null /*|| historicalList.size() == 0*/) {
                        logger.error("historicalList error");
                        continue;
                    }
                    logger.debug("debug");

                }

          }catch (Exception e) {
            // TODO: handle exception
        }
      }

日志输出: img

如果我将HistoricalDataModal替换为FundamentalModal,程序不会阻塞。所以这是HistoricalDataModal的问题。但我不知道为什么。

【问题讨论】:

  • 如果你尝试 mongoOperation.findOne 会发生什么?它给你一个结果还是它仍然阻塞?
  • @DanielC。没有findOne,findOne不要阻塞。find方法会阻塞
  • 假设数据库太大,那么一种可能的解决方案是使用 Pageable 来限制从 Mongo DB 获得的数据量。像这样 Pageable myPagable = new PageRequest(0, 10);然后将其设置为这样的查询:symbolQuery.with(myPagable);它将返回 10 个文档
  • @DanielC.我使用mongo-shell获取相同的集合,只返回4条记录。所以我认为这不是因为数据库很大。
  • 还有一个问题,为什么if里面有continue语句?你因为某种原因而循环吗?您也可以尝试使用 System.out.println("debug") 而不是 logger.debug 来查看输出吗?

标签: java spring mongodb


【解决方案1】:

我将分享一个示例,说明如何验证您的 Bar 类是否正确映射到您的 Mongo 文档。

例如,它是来自 mongo shell 的 historicalDataModal 对象的集合:

> db.getCollection("historicalDataModal").find()
{ "_id" : ObjectId("599eba80ee19f50d0ceecb07"), "_class" : "com.mongodb.demo.mongodbdemo.model.HistoricalDataModal", "symbol" : "xiix", "bar" : { "m_time" : "1234", "m_open" : 23, "m_high" : 34, "m_low" : 45, "m_close" : 56, "m_volume" : NumberLong(23), "m_count" : 2, "m_wap" : 34.5 } }
{ "_id" : ObjectId("599ebaacee19f50d56141eb7"), "_class" : "com.mongodb.demo.mongodbdemo.model.HistoricalDataModal", "symbol" : "xiix", "bar" : { "m_time" : "1234", "m_open" : 23, "m_high" : 34, "m_low" : 45, "m_close" : 56, "m_volume" : NumberLong(23), "m_count" : 2, "m_wap" : 34.5 } }
{ "_id" : ObjectId("599ebab1ee19f50d932f8c29"), "_class" : "com.mongodb.demo.mongodbdemo.model.HistoricalDataModal", "symbol" : "xiix", "bar" : { "m_time" : "1234", "m_open" : 23, "m_high" : 34, "m_low" : 45, "m_close" : 56, "m_volume" : NumberLong(23), "m_count" : 2, "m_wap" : 34.5 } }
> 

如您所见,Bar 对象的属性以m_ 开头,例如m_timem_openm_high。实际上 Bar 类具有带有该前缀的属性。但是构造函数没有相同的前缀,实际上Bar构造函数有timeopenhigh没有m_前缀。

为了查看这种行为的替代方法,请尝试使用以下 Barclass,您将看到构造函数与 Bar 类的成员具有相同的前缀

选项 1:没有构造函数的 Bar 类

public class Bar {

    private String m_time;
    private double m_open;
    private double m_high;
    private double m_low;
    private double m_close;
    private long m_volume;
    private int m_count;
    private double m_wap;


    public String time() {
        return m_time;
    }

    public double open() {
        return m_open;
    }

    public double high() {
        return m_high;
    }

    public double low() {
        return m_low;
    }

    public double close() {
        return m_close;
    }

    public long volume() {
        return m_volume;
    }

    public int count() {
        return m_count;
    }

    public double wap() {
        return m_wap;
    }

}

选项 2 具有相同前缀名称的构造函数的 Bar 类

public class Bar {

    private String m_time;
    private double m_open;
    private double m_high;
    private double m_low;
    private double m_close;
    private long m_volume;
    private int m_count;
    private double m_wap;

    public Bar(String m_time, double m_open, double m_high, double m_low, double m_close, long m_volume, int m_count, double m_wap) {
        this.m_time = m_time;
        this.m_open = m_open;
        this.m_high = m_high;
        this.m_low = m_low;
        this.m_close = m_close;
        this.m_volume = m_volume;
        this.m_count = m_count;
        this.m_wap = m_wap;
    }

    public String time() {
        return m_time;
    }

    public double open() {
        return m_open;
    }

    public double high() {
        return m_high;
    }

    public double low() {
        return m_low;
    }

    public double close() {
        return m_close;
    }

    public long volume() {
        return m_volume;
    }

    public int count() {
        return m_count;
    }

    public double wap() {
        return m_wap;
    }

}

【讨论】:

  • 非常感谢
【解决方案2】:

Bar.java

package com.ib.client;

public class Bar {

    private String m_time;
    private double m_open;
    private double m_high;
    private double m_low;
    private double m_close;
    private long m_volume;
    private int m_count;
    private double m_wap;

    public Bar(String time, double open, double high, double low, double close, long volume, int count, double wap) {
        this.m_time = time;
        this.m_open = open;
        this.m_high = high;
        this.m_low = low;
        this.m_close = close;
        this.m_volume = volume;
        this.m_count = count;
        this.m_wap = wap;
    }

    public String time() {
        return m_time;
    }

    public double open() {
        return m_open;
    }

    public double high() {
        return m_high;
    }

    public double low() {
        return m_low;
    }

    public double close() {
        return m_close;
    }

    public long volume() {
        return m_volume;
    }

    public int count() {
        return m_count;
    }

    public double wap() {
        return m_wap;
    }

}

HistoricalDataModal.java

package pro.elijah.batch.modal;

import com.ib.client.Bar;

public class HistoricalDataModal {

    private String symbol;
    private Bar bar;
    public String getSymbol() {
        return symbol;
    }
    public void setSymbol(String symbol) {
        this.symbol = symbol;
    }
    public Bar getBar() {
        return bar;
    }
    public void setBar(Bar bar) {
        this.bar = bar;
    }
    @Override
    public String toString() {
        return "HistoricalDataModal [symbol=" + symbol + ", bar=" + bar + "]";
    }



}

【讨论】:

    猜你喜欢
    • 2020-04-15
    • 1970-01-01
    • 2020-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-10
    • 1970-01-01
    相关资源
    最近更新 更多