简体   繁体   English

Spring数据mongodb查询文档

[英]spring data mongodb query document

I am facing this issue(getting null response) when i am trying to Query in Java using 我在尝试使用Java进行查询时遇到此问题(获取空响应)

I need to based on placed time stamp range and releases desc and status. 我需要根据放置的时间戳范围并发布desc和状态。

// My document as follows:
<ordersAuditRequest>
    <ordersAudit>
        <createTS>2013-04-19 12:19:17.165</createTS>
        <orderSnapshot>
            <orderId>43060151</orderId>
            <placedTS>2013-04-19 12:19:17.165</placedTS>
            <releases>
                <ffmCenterDesc>TW</ffmCenterDesc>
                <relStatus>d   </relStatus>
            </releases>
    </ordersAudit>
 </ordersAuditRequest>

I am using following query but it returns null. 我正在使用以下查询,但它返回null。

Query query = new Query();
query.addCriteria(Criteria.where("orderSnapshot.releases.ffmCenterDesc").is(ffmCenterDesc)
                                 .and("orderSnapshot.releases.relStatus").is(relStatus)
                                 .andOperator(
                                        Criteria.where("orderSnapshot.placedTS").gt(orderPlacedStart),
                                        Criteria.where("orderSnapshot.placedTS").lt(orderPlacedEnd)
                                 )
                 );

I can't reproduce your problem, which suggests that the issue is with the values in the database and the values you're passing in to the query (ie they're not matching). 我无法重现您的问题,这表明问题出在数据库中的值以及您要传递给查询的值(即它们不匹配)。 This is not unusual when you're trying to match dates, as you need to make sure they're stored as ISODates in the database and queried using java.util.date in the query. 当您尝试匹配日期时,这并不罕见,因为您需要确保将日期存储为ISODates并在查询中使用java.util.date进行查询。

I have a test that shows your query working, but I've made a number of assumptions about your data. 我有一个测试可以显示您的查询正常运行,但是我对您的数据做了很多假设。

My test looks like this, hopefully this will help point you in the correct direction, or if you give me more feedback I can re-create your problem more accurately. 我的测试看起来像这样,希望这可以帮助您指出正确的方向,或者如果您给我更多的反馈,我可以更准确地重新创建您的问题。

@Test
public void shouldBeAbleToQuerySpringDataWithDates() throws Exception {
    // Setup - insert test data into the DB
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd' 'hh:mm:ss.SSS");
    MongoTemplate mongoTemplate = new MongoTemplate(new Mongo(), "TheDatabase");
    // cleanup old test data
    mongoTemplate.getCollection("ordersAudit").drop();

    Release release = new Release("TW", "d");
    OrderSnapshot orderSnapshot = new OrderSnapshot(43060151, dateFormat.parse("2013-04-19 12:19:17.165"), release);
    OrdersAudit ordersAudit = new OrdersAudit(dateFormat.parse("2013-04-19 12:19:17.165"), orderSnapshot);

    mongoTemplate.save(ordersAudit);

    // Create and run the query
    Date from = dateFormat.parse("2013-04-01 01:00:05.000");
    Date to = dateFormat.parse("2014-04-01 01:00:05.000");

    Query query = new Query();
    query.addCriteria(Criteria.where("orderSnapshot.releases.ffmCenterDesc").is("TW")
                              .and("orderSnapshot.releases.relStatus").is("d")
                              .andOperator(
                                          Criteria.where("orderSnapshot.placedTS").gt(from),
                                          Criteria.where("orderSnapshot.placedTS").lt(to)
                                          )
                     );

    // Check the results
    List<OrdersAudit> results = mongoTemplate.find(query, OrdersAudit.class);
    Assert.assertEquals(1, results.size());
}

public class OrdersAudit {
    private Date createdTS;
    private OrderSnapshot orderSnapshot;

    public OrdersAudit(final Date createdTS, final OrderSnapshot orderSnapshot) {
        this.createdTS = createdTS;
        this.orderSnapshot = orderSnapshot;
    }
}

public class OrderSnapshot {
    private long orderId;
    private Date placedTS;
    private Release releases;

    public OrderSnapshot(final long orderId, final Date placedTS, final Release releases) {
        this.orderId = orderId;
        this.placedTS = placedTS;
        this.releases = releases;
    }
}

public class Release {
    String ffmCenterDesc;
    String relStatus;

    public Release(final String ffmCenterDesc, final String relStatus) {
        this.ffmCenterDesc = ffmCenterDesc;
        this.relStatus = relStatus;
    }
}

Notes: 笔记:

  • This is a TestNG class, not JUnit. 这是一个TestNG类,而不是JUnit。
  • I've used SimpleDateFormat to create Java Date classes, this is just for ease of use. 我已经使用SimpleDateFormat创建Java Date类,这只是为了易于使用。
  • The XML value you pasted for relStatus included spaces, which I have stripped. 您为relStatus粘贴的XML值包含空格,我已经删除了空格。

You showed us the document structure in XML, not JSON, so I've had to assume what your data looks like. 您向我们展示了XML而不是JSON的文档结构,因此我不得不假设您的数据是什么样的。 I've translated it almost directly into JSON, so it looks like this in the database: 我几乎将其直接转换为JSON,因此在数据库中看起来像这样:

{
    "_id" : ObjectId("51d689843004ec60b17f50de"),
    "_class" : "OrdersAudit",
    "createdTS" : ISODate("2013-04-18T23:19:17.165Z"),
    "orderSnapshot" : {
    "orderId" : NumberLong(43060151),
    "placedTS" : ISODate("2013-04-18T23:19:17.165Z"),
        "releases" : {
            "ffmCenterDesc" : "TW",
            "relStatus" : "d"
        }
    }
}

You can find what yours really looks like by doing a db.<collectionName>.findOne() call in the mongoDB shell. 您可以通过在mongoDB shell中执行db。<collectionName> .findOne()来找到您的真实外观。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM