如何比较不常见的两个馆藏和存档文件

时间:2019-03-19 01:34:40

标签: mongodb spring-boot spring-data aggregation-framework spring-data-mongodb

我有两个集合,例如CollectionA和CollectionB都具有通用文件名,即主机名

集合A:

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  </head>
  <body>
    <script>
    function addStudents(studentList){ 
      $('#rangeResult').text(studentList);

      document.write(new Date().toLocaleDateString());

      var students = [];
      //The following loop was added to the function
      for (var i = 0; i < studentList.length; i++) {
        document.write('<br><input type="checkbox" name="students" id="i" value="i">'+ studentList[i]);
      }
      //Also added the buttons to the function with document.write rather than html tags outside of the function
      document.write('<br><input type="button" value="Submit Early Release" onclick="google.script.host.close()" />');
      document.write('<input type="button" value="Close" onclick="google.script.host.close()" />');
    };

    google.script.run.withSuccessHandler(addStudents).earlyReleaseList();
    </script> 
  </body>
</html>

集合B

Expanded( child: Container( child: ConstrainedBox( constraints: BoxConstraints.expand(), child: Ink.image( image: AssetImage( 'path/the_image.png'), fit: BoxFit.fill, child: InkWell( onTap: null, ), ), ), ), ),

我要实现的是我收到集合B的帖子

  • 我想根据主机名检查记录B中是否存在该记录并更新所有值。如果没有插入新记录(我已经读过,可以通过使用upsert来实现-仍在寻找使之工作的方法)

    • 我想检查主机名是否存在于集合A中,如果没有将记录从集合B中移到另一个集合C中(作为存档记录)。即在上面的hostname = vm02中,集合B中的记录应该是移到收藏集C

我如何使用springboot mongodb anyhelp来实现这一目标。我必须保存Collection B的代码如下,我想对其进行更新以实现上述期望的结果

    {

  "hostname": "vm01",
  "id": "1",
  "status": "online",

}

更新1:以下内容符合我的预期,但我认为应该有一种更好的方法。

{

  "hostname": "vm01",
  "id": "string",
  "installedversion": "string",

}

{

  "hostname": "vm02",
  "id": "string",
  "installedversion": "string",

}

Update2:使用以下代码,我可以将记录移至另一个集合

public RscInstalltionStatusDTO save(RscInstalltionStatusDTO rscInstalltionStatusDTO) {
    log.debug("Request to save RscInstalltionStatus : {}", rscInstalltionStatusDTO);

    RscInstalltionStatus rscInstalltionStatus = rscInstalltionStatusMapper.toEntity(rscInstalltionStatusDTO);
    rscInstalltionStatus = rscInstalltionStatusRepository.save(rscInstalltionStatus);
    return rscInstalltionStatusMapper.toDto(rscInstalltionStatus);
}

我在这里遇到的这个问题是它返回多个记录

1 个答案:

答案 0 :(得分:0)

找到了解决方案,也许还有其他最好的解决方案,但是对我来说,这个方法很有效

创建一个类customeAggregationGeneration(可在SO答案中找到并进行扩展以满足我的需求)

public class CustomProjectAggregationOperation implements AggregationOperation {

private String jsonOperation;

public CustomProjectAggregationOperation(String jsonOperation) {
    this.jsonOperation = jsonOperation;
}

@Override
public Document toDocument(AggregationOperationContext aggregationOperationContext) {
    return aggregationOperationContext.getMappedObject(Document.parse(jsonOperation));
}
}



  String lookupquery = "{$lookup :{from:\"vmdetails\",localField:\"hostname\",foreignField:\"hostname\"as:\"rscinstall\"}}";
        String matchquery = "{ $match: { \"rscinstall\": { $eq: [] } }}";
        String projectquery = "{$project:{\"rscinstall\":0}}";

        AggregationOperation lookupOpertaion = new CustomProjectAggregationOperation(lookupquery);
        AggregationOperation matchOperation = new CustomProjectAggregationOperation(matchquery);
        AggregationOperation projectOperation = new CustomProjectAggregationOperation(projectquery);

        Aggregation aggregation = Aggregation.newAggregation(lookupOpertaion, matchOperation, projectOperation);
        ArrayList<Document> results1 = (ArrayList<Document>) mongoTemplate.aggregate(aggregation, "rsc_installtion_status", Document.class).getRawResults().get("result");

        // System.out.println(results1);

        for (Document doc : results1) {

         //   System.out.print(doc.get("_id").toString());
            mongoTemplate.insert(doc, "RscInstallArchive");
            delete(doc.get("_id").toString());