spring data mongodb query document

左心房为你撑大大i 提交于 2020-01-16 11:57:31

问题


I am facing this issue(getting null response) when i am trying to Query in Java using

I need to based on placed time stamp range and releases desc and status.

// 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.

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)
                                 )
                 );

回答1:


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 (i.e. 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.

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.
  • I've used SimpleDateFormat to create Java Date classes, this is just for ease of use.
  • The XML value you pasted for relStatus included spaces, which I have stripped.

You showed us the document structure in XML, not JSON, so I've had to assume what your data looks like. I've translated it almost directly into JSON, so it looks like this in the database:

{
    "_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.



来源:https://stackoverflow.com/questions/17078296/spring-data-mongodb-query-document

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!