{"id":156617,"date":"2019-09-10T08:24:00","date_gmt":"2019-09-10T00:24:00","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=156617"},"modified":"2019-08-26T09:24:51","modified_gmt":"2019-08-26T01:24:51","slug":"mongodb-talk","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/mongodb-talk.html","title":{"rendered":"\u6559\u4f60\u5982\u4f55\u7528 MongoDB \u5b9e\u73b0\u8bc4\u8bba\u699c\u529f\u80fd"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\nMongodb\u5f88\u9002\u5408\u505a\u8fd9\u4ef6\u4e8b,api\u7684\u8c03\u7528\u4ec5\u4ec5\u662f\u4f7f\u7528\u5230\u4e86\u5165\u95e8\u7ea7\u522b\u7684CRUD,\u7406\u6e05\u695a\u4e86\u601d\u8def,\u7f16\u7801\u4e5f\u4f1a\u987a\u98ce\u987a\u6c34,\u6240\u4ee5\u4f60\u4f1a\u53d1\u73b0\u6211\u5728\u8fd9\u7bc7\u535a\u5ba2\u4e2d\u8bf4\u7684\u6bd4\u7f16\u7801\u8fd8\u591a\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
\u8bc4\u8bba\u699c\u9884\u671f\u7684\u529f\u80fd<\/strong><\/div>\n

\u5c31\u50cf\u662fStackOverFlow\u7684\u90a3\u6837, \u7528\u6237\u53ef\u4ee5\u53d1\u51fa\u81ea\u5df1\u7684\u63d0\u95ee,\u5176\u4ed6\u7528\u6237\u6765\u89e3\u7b54, \u540c\u65f6\u697c\u4e3b\u53ef\u4ee5\u56de\u590d\u522b\u4eba\u7684\u8bc4\u8bba,\u522b\u4eba\u4f9d\u7136\u53ef\u4ee5\u56de\u590d\u697c\u4e3b<\/p>\n

\u6570\u636e\u7ed3\u6784<\/strong><\/div>\n

mongodb\u53ef\u4ee5\u5b58\u50a8\u6587\u6863\u554a, \u5176\u5b9e\u6211\u4eec\u8981\u505a\u7684\u5c31\u662f\u6784\u5efa\u4e00\u4e2a\u5408\u9002\u7684\u7c7b,\u8bc4\u8bba\u5e2e\u4e5f\u5c31\u6210\u529f\u4e00\u5927\u534a\u4e86<\/p>\n

\u95ee\u9898\/ \u8bc4\u8bba \u5b9e\u4f53\u5982\u4e0b<\/strong><\/div>\n
\u95ee\u9898<\/strong><\/span><\/div>\n
public class Problem implements Serializable {\r\n@Id\r\nprivate String _id;\/\/ key\r\nprivate String nickname; \/\/ \u7528\u6237\u540d\r\nprivate String avator; \/\/ \u7528\u6237\u5934\u50cf\r\n\r\nprivate String userId; \/\/ \u6237\u540did\r\nprivate String title ; \/\/ \u6807\u9898\r\nprivate String content ;  \/\/ \u5185\u5bb9\r\nprivate boolean answered ; \/\/ \u662f\u5426\u5df2\u7ecf\u56de\u7b54\r\nprivate Date createTime ; \/\/ \u521b\u5efa\u65f6\u95f4\r\nprivate boolean flag =false; \/\/ \u6807\u8bb0\u662f\u5426\u662f\u672c\u4eba,\u9ed8\u8ba4\u662f\u975e\u672c\u4eba\r\nprivate List<Answer> answerList; \/\/ \u95ee\u9898\u7684\u56de\u7b54\u5217\u8868\r\n}<\/code><\/pre>\n
\u8bc4\u8bba<\/strong><\/span><\/div>\n
public class Answer {\r\n\r\n    private String id;\/\/ \u5f53\u524d\u56de\u7b54\u7684\u552f\u4e00\u6807\u8bc6\r\n    private String nickname; \/\/ \u7528\u6237\u540d\r\n    private String avator; \/\/ \u7528\u6237\u5934\u50cf\r\n\r\n    private  Integer userId; \/\/ \u56de\u7b54\u7684\u7528\u6237\u7684id\r\n    private  String Content ; \/\/ \u56de\u7b54\u7684\u5185\u5bb9\r\n    private  Date time;\r\n    private  boolean flag = false;\/\/ \u9ed8\u8ba4false.\u4e0d\u662f\u672c\u4eba\r\n    private  Integer group; \/\/ \u5206\u7ec4\u7684\u6807\u8bb0\r\n}<\/code><\/pre>\n
\u601d\u8def<\/strong><\/div>\n

Answer\u5b9e\u4f53\u4e2d,\u5e76\u6ca1\u6709\u6dfb\u52a0\u4e00\u4e2a\u96c6\u5408\u7528\u6765\u5b58\u653eAnswer\u7c7b\u578b\u7684\u5b9e\u4f53,\u5982\u679c\u6dfb\u52a0\u4e0a\u8fd9\u4e2a\u96c6\u5408\u7684\u8bdd,\u786e\u5b9e\u60f3\u6cd5\u8fd8\u4e0d\u9519,\u56de\u590d\u4e2d\u6709\u5176\u4ed6\u4eba\u5bf9\u81ea\u5df1\u7684\u56de\u590d,\u5929\u751f\u7684\u6811\u5f62\u7ed3\u6784,\u4f46\u662f\u8003\u8651\u5230\u524d\u7aef\u7684\u6e32\u67d3\u7684\u96be\u5ea6\u52a0\u5927,\u653e\u5f03\u4e86\u8fd9\u79cd\u65b9\u6848<\/p>\n

\u95ee\u9898\u7684\u5b9e\u4f53\u7c7b\u4e2d\u7ef4\u62a4\u4e86\u4e00\u4e2a\u56de\u7b54\u7684\u5b9e\u4f53\u7c7b\u7684\u96c6\u5408,\u6240\u6709\u9488\u5bf9\u697c\u4e3b\u95ee\u9898\u7684\u56de\u7b54\u5b9e\u4f8b\u5168\u90e8\u653e\u5728\u8fd9\u4e2a\u96c6\u5408\u4e2d, \u4e5f\u5305\u62ec\u697c\u4e3b\u5bf9\u95ee\u9898\u56de\u7b54\u8005\u7684\u56de\u590d, \u8fd8\u5305\u542b\u56de\u7b54\u8005\u5bf9\u95ee\u9898\u7684\u56de\u590d<\/p>\n

\u4e8e\u662f\u8fd9\u6837\u5c31\u4ec5\u4ec5\u5b58\u5728\u4e24\u5c42,\u4e00\u4e2a\u95ee\u9898\u4e2d\u7ef4\u62a4\u7740\u5bf9\u8fd9\u4e2a\u95ee\u9898\u7684\u5168\u90e8\u56de\u590d,\u524d\u7aef\u6e32\u67d3\u7684\u96be\u5ea6\u5927\u5927\u964d\u4f4e,\u4f46\u662f\u540e\u6765\u5374\u6765\u4e8b\u4e86
\n\u7528\u6237\u67e5\u8be2\u4e00\u4e2a\u95ee\u9898\u7684\u8be6\u60c5\u65f6,\u540e\u7aef\u5982\u4f55\u5904\u7406<\/p>\n

\u5f53\u7528\u6237\u67e5\u8be2\u4e00\u4e2a\u95ee\u9898\u7684\u8be6\u60c5\u65f6,\u540e\u7aef\u62ff\u7740\u95ee\u9898\u7684id,\u53bb\u6570\u636e\u5e93\u4e2d\u5c06\u95ee\u9898\u7684\u5b9e\u4f8b\u53d6\u51fa\u6765,\u7d27\u63a5\u7740\u5904\u7406Answer\u96c6\u5408,\u5c06\u6309\u7167\u65f6\u95f4\u6392\u5e8f\u7684\u96c6\u5408\u6309\u7167\u6211\u4eec\u6307\u5b9a\u7684\u65b9\u5f0f\u5206\u7ec4,\u518d\u6309\u65f6\u95f4\u6392\u5e8f<\/p>\n

\u6309\u4ec0\u4e48\u5206\u7ec4\u5462?<\/strong><\/span><\/div>\n

\u5f53\u65f6\u662f\u6309\u7167\u4e0d\u540c\u7684\u7528\u6237\u5206\u7ec4, \u540c\u4e00\u4e2a\u7528\u6237\u7684\u5168\u90e8\u8bc4\u8bba,\u5df2\u7ecf\u697c\u4e3b\u5bf9\u5b83\u7684\u56de\u590d,\u4ee5\u53ca\u522b\u4eba\u5bf9\u5b83\u7684\u56de\u590d\u90fd\u653e\u5728\u4e00\u8d77, \u6240\u4ee5\u9700\u8981\u4e00\u4e2a\u5b57\u6bb5,group(\u6211\u9009\u7684\u7528\u6237id), \u4e13\u95e8\u5b58\u50a8\u5206\u7ec4\u7684\u6807\u5fd7. \u7ec4\u5185\u7684\u5b9e\u4f8b\u518d\u6309\u7167\u65f6\u95f4\u6392\u5e8f,\u8fd9\u6837\u6574\u4f53\u7684\u5c42\u6b21\u5c31\u5212\u5206\u597d\u4e86<\/p>\n

public JsonResult problemDetail(@PathVariable String problemId){\r\n    Optional<Problem> byId = problemRepository.findById(problemId);\r\n    if (!byId.isPresent()){\r\n        return JsonResult.fail(\"\u60a8\u6ca1\u6709\u83b7\u53d6\u5230\u8be6\u60c5\u9875,\u8bf7\u8054\u7cfb\u7ba1\u7406\u5458\");\r\n    }\r\n    Problem problem = byId.get();\r\n    if (problem.getAnswerList().size()>0){\r\n            Map<Integer, List<Answer>> collect = problem.getAnswerList()\r\n            .stream().collect(Collectors.groupingBy(Answer::getGroup));\r\n            ArrayList<Answer> list = new ArrayList<>();\r\n            collect.forEach((k,v)->{\r\n                list.addAll(v);\r\n            });\r\n            problem.setAnswerList(list);\r\n        }\r\n    return JsonResult.ok(\"\u8fd4\u56de\u8be6\u60c5\u9875\"+problem);\r\n}<\/code><\/pre>\n
\u5b9a\u4f4d\u51fa\u5f53\u524d\u7528\u6237\u7684\u8bc4\u8bba<\/strong><\/div>\n

\u5982\u679c\u524d\u7aef\u60f3\u5728\u9875\u9762\u7684\u5206\u5de6\u53f3\u4e24\u90e8\u5206\u5c55\u793a\u81ea\u5df1\u7684\u8bc4\u8bba\u548c\u522b\u4eba\u7684\u8bc4\u8bba,\u5c31\u9700\u8981\u4e00\u4e2a\u6807\u8bb0,\u65e2\u7136\u4e0a\u9762\u90fd\u5df2\u7ecf\u5728\u904d\u5386\u4e86,\u591a\u52a0\u4e00\u4e2a\u5224\u65ad\u4e5f\u65e0\u59a8, \u62ff\u7740\u524d\u7aef\u63d0\u4ea4\u8fc7\u6765\u7684\u7528\u6237id\u548cAnswer\u4e2d\u7684userId\u6bd4\u5bf9, \u5982\u679c\u76f8\u7b49,\u5c31\u628a\u8fd9\u4e2a\u8bc4\u8bba\u7684flag\u6807\u8bb0\u4e3atrue, \u524d\u7aef\u6839\u636e\u8fd9\u4e2a\u6807\u8bb0\u533a\u5206, \u4ece\u800c\u7ed9\u7528\u6237\u66f4\u591a\u7684\u6743\u9650,\u6bd4\u5982\u5220\u9664\u81ea\u5df1\u7684\u8bc4\u8bba<\/p>\n

\u5c40\u9650\u6027<\/strong><\/div>\n

\u5982\u679c\u6ca1\u4e2a\u95ee\u9898\u90fd\u50cf\u7f51\u6613\u97f3\u4e50\u90a3\u79cd,\u4e0a\u4e07\u6761\u8bc4\u8bba,\u8fd9\u6837\u7684\u8bdd,\u4f30\u8ba1\u5c31\u5e9f\u4e86,\u867d\u7136\u4f7f\u7528stream\u4f1a\u5feb,\u4f46\u662f\u4e5f\u625b\u4e0d\u4f4f\u91cf\u554a, \u4f46\u662f\u6570\u91cf\u5c0f\u7684\u8bdd,\u8fd8\u662f\u53ef\u4ee5\u63a5\u53d7\u7684, \u5176\u5b9e\u7406\u60f3\u7684\u72b6\u6001\u662f\u8bc4\u8bba\u53ef\u4ee5\u4ee5\u5206\u9875\u7684\u5f62\u5f0f\u83b7\u53d6\u51fa\u6765, \u611f\u89c9\u624d\u6b63\u5b97\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

\u5c31\u50cf\u662fStackOverFlow\u7684\u90a3\u6837, \u7528\u6237\u53ef\u4ee5\u53d1\u51fa\u81ea\u5df1\u7684\u63d0\u95ee,\u5176\u4ed6\u7528\u6237\u6765\u89e3\u7b54, \u540c\u65f6\u697c\u4e3b\u53ef\u4ee5\u56de\u590d\u522b\u4eba\u7684\u8bc4\u8bba […]<\/p>\n","protected":false},"author":63,"featured_media":156620,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-156617","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/156617","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/63"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=156617"}],"version-history":[{"count":3,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/156617\/revisions"}],"predecessor-version":[{"id":157074,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/156617\/revisions\/157074"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/156620"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=156617"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=156617"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=156617"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}