如何使用Java Spring Boot更新MognoDB中的数组字段?

huangapple go评论92阅读模式
英文:

How to Update array field in MognoDB using Java Spring Boot?

问题

@GetMapping("/scannQr/{audienceId}/{eventName}/{Date}")
public ResponseBody scannQr(@PathVariable String audienceId , @PathVariable String eventName , @PathVariable String Date) {
    Map<String, Object> result = new HashMap<>();
    Update update = new Update();
    Audience audience  = audienceRepository.findByAudienceIdAndEventName(audienceId, eventName);
    //check Audience if his name in database
    if(audience == null) {
        result.put("Sorry Can't found Audience", audience);
        return new ResponseBody("failed",null);
    }
    for(int count = 0 ; count<audience.getQrCodes().size() ; count++) {
         Map<String , Object> xx = (Map<String, Object>) audience.getQrCodes().get(count);
         String DateForToday = LocalDate.now().toString();
         
         if(DateForToday.equals(xx.get(audience.getAudienceName()+"Day"+count).toString()) && xx.get("Scan").equals(false)) {
             System.out.println("Iam in For Loop");
             update.set(xx.get("Scan").toString(), true);
             
             Query query = new Query();
                query.addCriteria(Criteria.where("audienceId").is(audienceId));
             
             mongoOperation.findAndModify(query, update, Audience.class);
             result.put("success", audience);
             return new ResponseBody("success",result);
         }
         if(DateForToday.equals(xx.get(audience.getAudienceName()+"Day"+count).toString()) && xx.get("Scan").equals(true) ) {
             result.put("Qr For this Day But is Scanned", null);
             return new ResponseBody("failed",result);
         }
    }
    result.put("Qr is not for this Day", null);
    return new ResponseBody("failed",result);
}
"status": "success",
"result": {
    "success": {
        "audienceId": "5e8e478509270606a25334bf",
        "eventName": "skills",
        "audienceName": "wesam",
        "audienceEmail": "wesam@gmail.com",
        "audienceNumber": "0796945849",
        "qrCodes": [
            {
                "Scan": false,
                "mohammad qabbaniDay0": "2020-04-09"
            },
            {
                "Scan": false,
                "mohammad qabbaniDay1": "2020-04-10"
            }
        ],
        "pathQr": "var/www/html/storage/PRQRS/skills",
        "link": "test.com/mohammad qabbani/null"
    }
}
英文:

I want to Update Scan to true if qr Code is scan i have issue with query .

@GetMapping(&quot;/scannQr/{audienceId}/{eventName}/{Date}&quot;)
public ResponseBody scannQr(@PathVariable String audienceId , @PathVariable String eventName , @PathVariable String Date) {
Map&lt;String, Object&gt; result = new HashMap&lt;&gt;();
Update update = new Update();
Audience audience  = audienceRepository.findByAudienceIdAndEventName(audienceId, eventName);
//check Audience if his name in database
if(audience == null) {
result.put(&quot;Sorry Can`t found Audience&quot;, audience);
return new ResponseBody(&quot;failed&quot;,null);
}
for(int count = 0 ; count&lt;audience.getQrCodes().size() ; count++) {
Map&lt;String , Object&gt; xx = (Map&lt;String, Object&gt;) audience.getQrCodes().get(count);
//System.out.println(&quot;Key is :&quot;+xx.get(audience.getAudienceName()+&quot;Day&quot;+count));
//System.out.println(LocalDate.now());
//System.out.println(LocalDate.now() +&quot; == &quot; + xx.get(audience.getAudienceName()+&quot;Day&quot;+0));
String DateForToday = LocalDate.now().toString();
if(DateForToday.equals(xx.get(audience.getAudienceName()+&quot;Day&quot;+count).toString()) &amp;&amp; xx.get(&quot;Scan&quot;).equals(false)) {
System.out.println(&quot;Iam in For Loop&quot;);
update.set(xx.get(&quot;Scan&quot;).toString(), true);
System.out.println(update.set(xx.get(&quot;Scan&quot;).toString(), true));
// Query to Update Scan = false to Scan True
Query query = new Query();
query.addCriteria(Criteria
.where(&quot;audienceId&quot;).is(audienceId));
System.out.println(query);
mongoOperation.findAndModify(query, update, Audience.class);
result.put(&quot;success&quot;, audience);
return new ResponseBody(&quot;success&quot;,result);
}
if(DateForToday.equals(xx.get(audience.getAudienceName()+&quot;Day&quot;+count).toString()) &amp;&amp; xx.get(&quot;Scan&quot;).equals(true) ) {
result.put(&quot;Qr For this Day But is Scanned&quot;, null);
return new ResponseBody(&quot;failed&quot;,result);
}
}
result.put(&quot;Qr is not for this Day&quot;, null);
return new ResponseBody(&quot;failed&quot;,result);
}
 &quot;status&quot;: &quot;success&quot;,
&quot;result&quot;: {
&quot;success&quot;: {
&quot;audienceId&quot;: &quot;5e8e478509270606a25334bf&quot;,
&quot;eventName&quot;: &quot;skills&quot;,
&quot;audienceName&quot;: &quot;wesam&quot;,
&quot;audienceEmail&quot;: &quot;wesam@gmail.com&quot;,
&quot;audienceNumber&quot;: &quot;0796945849&quot;,
&quot;qrCodes&quot;: [
{
&quot;Scan&quot;: false,
&quot;mohammad qabbaniDay0&quot;: &quot;2020-04-09&quot;
},
{
&quot;Scan&quot;: false,
&quot;mohammad qabbaniDay1&quot;: &quot;2020-04-10&quot;
}
],
&quot;pathQr&quot;: &quot;var/www/html/storage/PRQRS/skills&quot;,
&quot;link&quot;: &quot;test.com/mohammad qabbani/null&quot;
}
}
}

答案1

得分: 0

这将更新qrCodes数组的子文档字段Scan的所有元素为true

假设输入文档如下:

{
"audienceId": "5e8e478509270606a25334bf",
"eventName": "skills",
"audienceName": "wesam",
"audienceEmail": "wesam@gmail.com",
"audienceNumber": "0796945849",
"qrCodes": [
{
"Scan": false,
"mohammad qabbaniDay0": "2020-04-09"
},
{
"Scan": false,
"mohammad qabbaniDay1": "2020-04-10"
}
],
"pathQr": "var/www/html/storage/PRQRS/skills",
"link": "test.com/mohammad qabbani/null"
}

Spring Data MongoDB 代码:

String audienceId = "5e8e478509270606a25334bf";

Query query = new Query();
query.addCriteria(Criteria.where("audienceId").is(audienceId));
Update update = new Update().set("qrCodes.$[].Scan", true);

MongoOperations mongoTemplate = new MongoTemplate(MongoClients.create(), "testDB");
Audience previous = mongoTemplate.findAndModify(query, update, Audience.class);
英文:

This updates all the elements of the qrCodes array sub-document field Scan to true.

Assuming the input document is:

{
&quot;audienceId&quot;: &quot;5e8e478509270606a25334bf&quot;,
&quot;eventName&quot;: &quot;skills&quot;,
&quot;audienceName&quot;: &quot;wesam&quot;,
&quot;audienceEmail&quot;: &quot;wesam@gmail.com&quot;,
&quot;audienceNumber&quot;: &quot;0796945849&quot;,
&quot;qrCodes&quot;: [
{
&quot;Scan&quot;: false,
&quot;mohammad qabbaniDay0&quot;: &quot;2020-04-09&quot;
},
{
&quot;Scan&quot;: false,
&quot;mohammad qabbaniDay1&quot;: &quot;2020-04-10&quot;
}
],
&quot;pathQr&quot;: &quot;var/www/html/storage/PRQRS/skills&quot;,
&quot;link&quot;: &quot;test.com/mohammad qabbani/null&quot;
}

The Spring Data MongoDB code:

String audienceId = &quot;5e8e478509270606a25334bf&quot;;
Query query = new Query();
query.addCriteria(Criteria.where(&quot;audienceId&quot;).is(audienceId));
Update update = new Update().set(&quot;qrCodes.$[].Scan&quot;, true);
MongoOperations mongoTemplate = new MongoTemplate(MongoClients.create(), &quot;testDB&quot;);
Audience previous = mongoTemplate.findAndModify(query, update, Audience.class);

huangapple
  • 本文由 发表于 2020年4月9日 06:11:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/61110863.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定