• <legend id='aB13S'><style id='aB13S'><dir id='aB13S'><q id='aB13S'></q></dir></style></legend>
    <i id='aB13S'><tr id='aB13S'><dt id='aB13S'><q id='aB13S'><span id='aB13S'><b id='aB13S'><form id='aB13S'><ins id='aB13S'></ins><ul id='aB13S'></ul><sub id='aB13S'></sub></form><legend id='aB13S'></legend><bdo id='aB13S'><pre id='aB13S'><center id='aB13S'></center></pre></bdo></b><th id='aB13S'></th></span></q></dt></tr></i><div id='aB13S'><tfoot id='aB13S'></tfoot><dl id='aB13S'><fieldset id='aB13S'></fieldset></dl></div>
    <tfoot id='aB13S'></tfoot>

    <small id='aB13S'></small><noframes id='aB13S'>

      • <bdo id='aB13S'></bdo><ul id='aB13S'></ul>

      1. 使用 spring-data 来自 MongoDB 的随机文档

        时间:2024-08-23

          <bdo id='lJPIc'></bdo><ul id='lJPIc'></ul>
          <legend id='lJPIc'><style id='lJPIc'><dir id='lJPIc'><q id='lJPIc'></q></dir></style></legend>
          <i id='lJPIc'><tr id='lJPIc'><dt id='lJPIc'><q id='lJPIc'><span id='lJPIc'><b id='lJPIc'><form id='lJPIc'><ins id='lJPIc'></ins><ul id='lJPIc'></ul><sub id='lJPIc'></sub></form><legend id='lJPIc'></legend><bdo id='lJPIc'><pre id='lJPIc'><center id='lJPIc'></center></pre></bdo></b><th id='lJPIc'></th></span></q></dt></tr></i><div id='lJPIc'><tfoot id='lJPIc'></tfoot><dl id='lJPIc'><fieldset id='lJPIc'></fieldset></dl></div>

          <small id='lJPIc'></small><noframes id='lJPIc'>

            <tbody id='lJPIc'></tbody>
          <tfoot id='lJPIc'></tfoot>

                • 本文介绍了使用 spring-data 来自 MongoDB 的随机文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我可以通过使用这个 mongodb 本机查询来做到这一点:

                  I'm able to do it by using this mongodb native query:

                  db.books.aggregate(
                     [ { $sample: { size: 15 } } ]
                  )
                  

                  但是如何在 spring-data-mongodb 中做到呢?

                  But how to do it in spring-data-mongodb ?

                  聚合类="nofollow">Spring 聚合框架

                  I found no similar operation in Aggregation class of Spring Aggregation Framework

                  推荐答案

                  更新:

                  从 Spring Data v2.0 开始,您可以这样做:

                  Starting with v2.0 of Spring Data you can do this:

                  SampleOperation matchStage = Aggregation.sample(5);
                  Aggregation aggregation = Aggregation.newAggregation(sampleStage);
                  AggregationResults<OutType> output = mongoTemplate.aggregate(aggregation, "collectionName", OutType.class);
                  

                  <小时>

                  原答案:

                  像 spring-mongo 这样的抽象层总是会落后于服务器发布的功能.因此,您最好自己为流水线阶段构建 BSON 文档结构.

                  Abstraction layers like spring-mongo are always going to lag way behind server released features. So you are best off contructing the BSON document structure for the pipeline stage yourself.

                  在自定义类中实现:

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

                  然后在你的代码中使用:

                  And then use in your code:

                  Aggregation aggregation = newAggregation(
                      new CutomAggregationOperation(
                          new BasicDBObject(
                              "$sample",
                              new BasicDBObject( "size", 15 )
                          )
                      )
                  );
                  

                  由于它实现了 AggregationOperation,因此它可以很好地与现有的管道操作辅助方法一起使用.即:

                  Since this implements AggregationOperation this works well with the exising pipeline operation helper methods. i.e:

                  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 对象.

                  So again, everything is just a BSON Object at the end of the day. It's just a matter of having an interface wrapper so that the class methods in spring-mongo interpret the result and get your defined BSON Object correctly.

                  这篇关于使用 spring-data 来自 MongoDB 的随机文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:春季数据中的@Transient 不起作用 下一篇:Spring Data Mongo 似乎忽略了 XML 配置中的主机

                  相关文章

                • <legend id='KJJuD'><style id='KJJuD'><dir id='KJJuD'><q id='KJJuD'></q></dir></style></legend>
                  <i id='KJJuD'><tr id='KJJuD'><dt id='KJJuD'><q id='KJJuD'><span id='KJJuD'><b id='KJJuD'><form id='KJJuD'><ins id='KJJuD'></ins><ul id='KJJuD'></ul><sub id='KJJuD'></sub></form><legend id='KJJuD'></legend><bdo id='KJJuD'><pre id='KJJuD'><center id='KJJuD'></center></pre></bdo></b><th id='KJJuD'></th></span></q></dt></tr></i><div id='KJJuD'><tfoot id='KJJuD'></tfoot><dl id='KJJuD'><fieldset id='KJJuD'></fieldset></dl></div>
                • <tfoot id='KJJuD'></tfoot>

                  <small id='KJJuD'></small><noframes id='KJJuD'>

                  • <bdo id='KJJuD'></bdo><ul id='KJJuD'></ul>