Reputation: 1455
I was looking to use the $natural
operator in Spring Data MongoDB, as documented here:
https://docs.mongodb.org/v3.0/reference/operator/meta/natural/
Is this possible to do using the MongoTemplate
class? Thanks.
Upvotes: 2
Views: 1235
Reputation: 18119
It is possible to use $natural
in at least three styles where 1. and 2. are probably what you're looking for:
Sort
with Query
Query query = new Query().with(new Sort(Direction.ASC, "$natural"));
use the query afterwards with MongoTemplate
. The query carries a sort document like:
{ "$natural" : 1}
BasicQuery
BasicQuery
allows using own DBObject
s for the query document, the fields ("projection") and sorting.
BasicQuery basicQuery = new BasicQuery(new BasicDBObject());
basicQuery.setSortObject(new BasicDBObject("$natural", 1));
execute
and CollectionCallback
This is the most extensive way in which you're getting access to the DBCollection
and you can use the native MongoDB driver API. See Spring Data Mongo docs for more details.
Upvotes: 2