in Education by
I have two collections for example CollectionA and CollectionB both have common filed which is hostname Collection A : { "hostname": "vm01", "id": "1", "status": "online", } Collection B { "hostname": "vm01", "id": "string", "installedversion": "string", } { "hostname": "vm02", "id": "string", "installedversion": "string", } what i want to achieve is when i receive a post message for collection B I want to check if the record exists in Collection B based on hostname and update all the values. if not insert the new record ( i have read it can be achieved by using upsert -- still looking how to make it work) I want to check if the hostname is present in Collection A , if not move the record from collection B to another collection which is collection C ( as archive records).ie in the above hostname=vm02 record from collection B should be moved to collectionC how can i achieve this using springboot mongodb anyhelp is appreciated.The code which i have to save the Collection B is as follows which i want to update to achieve the above desired result 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); } Update 1 : The below works as i expected but I think there should be a better way to do this. public RscInstalltionStatusDTO save(RscInstalltionStatusDTO rscInstalltionStatusDTO) { log.debug("Request to save RscInstalltionStatus : {}", rscInstalltionStatusDTO); RscInstalltionStatus rscInstalltionStatus = rscInstalltionStatusMapper.toEntity(rscInstalltionStatusDTO); System.out.print(rscInstalltionStatus.getHostname()); Query query = new Query(Criteria.where("hostname").is(rscInstalltionStatus.getHostname())); Update update = new Update(); update.set("configdownload",rscInstalltionStatus.getConfigdownload()); update.set("rscpkgdownload",rscInstalltionStatus.getRscpkgdownload()); update.set("configextraction",rscInstalltionStatus.getConfigextraction()); update.set("rscpkgextraction",rscInstalltionStatus.getRscpkgextraction()); update.set("rscstartup",rscInstalltionStatus.getRscstartup()); update.set("installedversion",rscInstalltionStatus.getInstalledversion()); mongoTemplate.upsert(query, update,RscInstalltionStatus.class); rscInstalltionStatus = rscInstalltionStatusRepository.findByHostname(rscInstalltionStatus.getHostname()); return rscInstalltionStatusMapper.toDto(rscInstalltionStatus); } Update2 : with the below code i am able to get move the records to another collection String query = "{$lookup:{ from: \"vmdetails\",let: {rschostname: \"$hostname\"},pipeline:[{$match:{$expr:{$ne :[\"$hostname\",\"$$rschostname\"]}}}],as: \"rscInstall\"}},{$unwind:\"$rscInstall\"},{$project:{\"_id\":0,\"rscInstall\":0}}"; AggregationOperation rscInstalltionStatusTypedAggregation = new CustomProjectAggregationOperation(query); LookupOperation lookupOperation = LookupOperation.newLookup().from("vmdetails").localField("hostname").foreignField("hostname").as("rscInstall"); UnwindOperation unwindOperation = Aggregation.unwind("$rscInstall"); ProjectionOperation projectionOperation = Aggregation.project("_id","rscInstall").andExclude("_id","rscInstall"); OutOperation outOperation = Aggregation.out("RscInstallArchive"); Aggregation aggregation = Aggregation.newAggregation(rscInstalltionStatusTypedAggregation,unwindOperation,projectionOperation,outOperation); List results = mongoTemplate.aggregate(aggregation,"rsc_installtion_status",BasicDBObject.class).getMappedResults(); this issue which i have here is it returns multiple records JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
Found the solution , there may be other best solutions but for me this one worked create a class customeAggregationGeneration (found in SO answers and extended to match my needs) 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 results1 = (ArrayList) 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());

Related questions

0 votes
    How will you compare two lists in Python?...
asked Nov 26, 2020 in Technology by JackTerrance
0 votes
    Which of these method of class String is used to compare two String objects for their equality? (a) ... questions and answers pdf, java interview questions for beginners...
asked Oct 25, 2021 in Education by JackTerrance
0 votes
    How might you approve all mappings in the archive all the while?...
asked Jun 7, 2021 in Technology by JackTerrance
0 votes
    In one Google sheet workbook, lets say I have Sheet1 with 5 rows as Sheet1 And I have Sheet2 as ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 6, 2022 in Education by JackTerrance
0 votes
    Lets say I have this following table: +------+-------------------------+-- ... JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 2, 2022 in Education by JackTerrance
0 votes
    Consider two given file paths as strings: /path/to/dir and /path/to/dir/file In Java, how can ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 19, 2022 in Education by JackTerrance
0 votes
    Consider two given file paths as strings: /path/to/dir and /path/to/dir/file In Java, how can ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 14, 2022 in Education by JackTerrance
0 votes
    I am trying to compare two variables in which are usually strings. These variables are generated from a ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jan 13, 2022 in Education by JackTerrance
0 votes
    Is there a way to compare two structure variables?...
asked Nov 9, 2020 in Technology by JackTerrance
0 votes
    first time posting. I'm currently trying to open an .xlsx file with zipfile on Python, finding all ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 21, 2022 in Education by JackTerrance
0 votes
    We have a live MySQL database that is 99% INSERTs, around 100 per second. We want to archive the ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 24, 2022 in Education by JackTerrance
0 votes
    What are you going to do if there is no Functional Spec or any documents related to the system and developer who wrote ... company anymore, but you have a system and need to test?...
asked Oct 17, 2020 in Technology by JackTerrance
0 votes
    I have an issue while building my Solr index (Lucene & Solr 3.4.0 on an Apache Tomcat 6.0.33) ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 15, 2022 in Education by JackTerrance
0 votes
    how many maximum worksheet are available per Calc spreadsheet documents . Select the correct answer from above options...
asked Dec 14, 2021 in Education by JackTerrance
...