简体   繁体   English

spring data mongo - 具有查询提示的mongotemplate计数

[英]spring data mongo - mongotemplate count with query hint

The mongo docs specify that you can specify a query hint for count queries using the following syntax: mongo文档指定您可以使用以下语法为计数查询指定查询提示:

db.orders.find(
   { ord_dt: { $gt: new Date('01/01/2012') }, status: "D" }
).hint( { status: 1 } ).count()

Can you do this using the mongo template? 你能用mongo模板做到这一点吗? I have a Query object and am calling the withHint method. 我有一个Query对象,我正在调用withHint方法。 I then call mongoTemplate.count(query); 然后我调用mongoTemplate.count(query); However, I'm pretty sure it's not using the hint, though I'm not positive. 但是,我很确定它没有使用提示,但我并不积极。

Sure, there are a few forms of this including going down to the basic driver, but assuming using your defined classes you can do: 当然,有一些形式,包括转到基本驱动程序,但假设使用您定义的类,您可以这样做:

    Date date = new DateTime(2012,1,1,0,0).toDate();
    Query query = new Query();
    query.addCriteria(Criteria.where("ord_dt").gte(date));
    query.addCriteria(Criteria.where("status").is("D"));
    query.withHint("status_1");

    long count = mongoOperation.count(query, Class);

So you basically build up a Query object and use that object passed to your operation, which is .count() in this case. 所以你基本上构建一个Query对象并使用传递给你的操作的对象,在这种情况下是.count()

The "hint" here is the name of the index as a "string" name of the index to use on the collection. 这里的“提示”是索引的名称,作为要在集合上使用的索引的“字符串”名称。 Probably something like "status_1" by default, but whatever the actual name is given. 默认情况下可能类似于“status_1”,但无论实际名称是什么。

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

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