英文:
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("/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);
//System.out.println("Key is :"+xx.get(audience.getAudienceName()+"Day"+count));
//System.out.println(LocalDate.now());
//System.out.println(LocalDate.now() +" == " + xx.get(audience.getAudienceName()+"Day"+0));
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);
System.out.println(update.set(xx.get("Scan").toString(), true));
// Query to Update Scan = false to Scan True
Query query = new Query();
query.addCriteria(Criteria
.where("audienceId").is(audienceId));
System.out.println(query);
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"
}
}
}
答案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:
{
"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"
}
The Spring Data MongoDB code:
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);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论