使用spring-data从MongoDB获取随机文档

4

请看Ron Tuffin在这里的答案:https://dev59.com/g3DXa4cB1Zd3GeqP8x4C - Darshan Mehta
2个回答

13
更新:
从Spring Data v2.0开始,您可以这样做:
SampleOperation matchStage = Aggregation.sample(5);
Aggregation aggregation = Aggregation.newAggregation(sampleStage);
AggregationResults<OutType> output = mongoTemplate.aggregate(aggregation, "collectionName", OutType.class);


”该段落是HTML中的一个水平线标签,没有具体的翻译含义。该回答讨论了使用spring-mongo等抽象层时可能会滞后于服务器发布的功能,因此最好自己构建BSON文档结构来实现流水线阶段。同时建议通过自定义类来实现此功能。
public class CustomAggregationOperation implements AggregationOperation {
    private DBObject operation;

    public CustomAggregationOperation (DBObject operation) {
        this.operation = operation;
    }

    @Override
    public DBObject toDBObject(AggregationOperationContext context) {
        return context.getMappedObject(operation);
    }
}

然后在你的代码中使用:
Aggregation aggregation = newAggregation(
    new CutomAggregationOperation(
        new BasicDBObject(
            "$sample",
            new BasicDBObject( "size", 15 )
        )
    )
);

由于这个实现了AggregationOperation,因此它可以很好地与现有的管道操作辅助方法配合使用。例如:
Aggregation aggregation = newAggregation(
    // custom pipeline stage
    new CutomAggregationOperation(
        new BasicDBObject(
            "$sample",
            new BasicDBObject( "size", 15 )
        )
    ),
    // Standard match pipeline stage
    match(
        Criteria.where("myDate")
            .gte(new Date(new Long("949384052490")))
            .lte(new Date(new Long("1448257684431")))
    )
);

所以说,最终一切都只是 BSON 对象。只需要一个接口包装器,让 spring-mongo 中的类方法解释结果并正确获取定义的 BSON 对象即可。

2
非常感谢,这正是我在寻找的,没有在Spring文档中找到。 - Olivier Boissé
Aggregation.newAggregation - Mehraj Malik

3

Blakes Seven已经正确回答了,但我想提供一个更好的AggregationOperation实现,该实现遵循标准的Spring实现。

import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import org.springframework.data.mongodb.core.aggregation.AggregationOperation;
import org.springframework.data.mongodb.core.aggregation.AggregationOperationContext;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.util.Assert;

public class SampleOperation implements AggregationOperation  {

    private int size;

    public SampleOperation(int size) {
        Assert.isTrue(size > 0, " Size must be positive!");
        this.size = size;
    }

    public AggregationOperation setSize(int size) {
        Assert.isTrue(size > 0, " Size must be positive!");
        this.size = size;
        return this;
    }

    @Override
    public DBObject toDBObject(AggregationOperationContext context) {
        return new BasicDBObject("$sample", context.getMappedObject(Criteria.where("size").is(size).getCriteriaObject()));
    }

}

之后,您可以使用构造函数创建SampleOperation对象,或使用setSize()方法更改其大小,并像往常一样将其应用于aggregate()函数。

更新:在SpringBoot 2.0.0+和Spring Framework 5.0中:Spring Mongo已经放弃了DBObject并替换为org.bson.Document因此,最后一段应该更新为:

    @Override
public Document toDocument(AggregationOperationContext aggregationOperationContext) {
    return new Document("$sample", aggregationOperationContext.getMappedObject(Criteria.where("size").is(size).getCriteriaObject()));

移除 @Override toDBObject


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接