You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1 line
8.6 KiB
JavaScript

"use strict";(self.webpackChunkfurion=self.webpackChunkfurion||[]).push([[6594],{5141:(n,e,o)=>{o.r(e),o.d(e,{assets:()=>d,contentTitle:()=>t,default:()=>h,frontMatter:()=>i,metadata:()=>c,toc:()=>l});var r=o(5893),s=o(1151);const i={id:"mongodb",title:"10.3 MongoDB \u64cd\u4f5c",sidebar_label:"10.3 MongoDB \u64cd\u4f5c"},t=void 0,c={id:"mongodb",title:"10.3 MongoDB \u64cd\u4f5c",description:"\u5728 Furion \u5305\u4e2d\u9ed8\u8ba4\u96c6\u6210\u4e86 EFCore\uff0c\u5982\u679c\u4e0d\u4f7f\u7528 EFCore\uff0c\u53ef\u5b89\u88c5\u7eaf\u51c0\u7248 Furion.Pure \u4ee3\u66ff Furion\u3002",source:"@site/docs/mongodb.mdx",sourceDirName:".",slug:"/mongodb",permalink:"/docs/mongodb",draft:!1,unlisted:!1,editUrl:"https://gitee.com/dotnetchina/Furion/tree/v4/handbook/docs/mongodb.mdx",tags:[],version:"current",lastUpdatedBy:"MonkSoul",lastUpdatedAt:1660021283,formattedLastUpdatedAt:"Aug 9, 2022",frontMatter:{id:"mongodb",title:"10.3 MongoDB \u64cd\u4f5c",sidebar_label:"10.3 MongoDB \u64cd\u4f5c"},sidebar:"docs",previous:{title:"10.2 Dapper \u96c6\u6210",permalink:"/docs/dapper"},next:{title:"11. SaaS \u591a\u79df\u6237",permalink:"/docs/saas"}},d={},l=[{value:"10.3.1 \u5173\u4e8e MongoDB",id:"1031-\u5173\u4e8e-mongodb",level:2},{value:"10.3.2 \u5982\u4f55\u96c6\u6210",id:"1032-\u5982\u4f55\u96c6\u6210",level:2},{value:"10.3.2.1 \u6ce8\u518c <code>MongoDB</code> \u670d\u52a1",id:"10321-\u6ce8\u518c-mongodb-\u670d\u52a1",level:3},{value:"10.3.3 \u57fa\u672c\u4f7f\u7528",id:"1033-\u57fa\u672c\u4f7f\u7528",level:2},{value:"10.3.3.1 \u5e38\u89c1\u4f8b\u5b50",id:"10331-\u5e38\u89c1\u4f8b\u5b50",level:3},{value:"10.3.3.2 \u83b7\u53d6 <code>MongoClient</code> \u5bf9\u8c61",id:"10332-\u83b7\u53d6-mongoclient-\u5bf9\u8c61",level:3},{value:"10.3.4 \u53cd\u9988\u4e0e\u5efa\u8bae",id:"1034-\u53cd\u9988\u4e0e\u5efa\u8bae",level:2}];function a(n){const e={a:"a",admonition:"admonition",code:"code",h2:"h2",h3:"h3",hr:"hr",li:"li",p:"p",pre:"pre",strong:"strong",ul:"ul",...(0,s.a)(),...n.components};return(0,r.jsxs)(r.Fragment,{children:[(0,r.jsx)(e.admonition,{title:"\u6e29\u99a8\u63d0\u9192",type:"warning",children:(0,r.jsxs)(e.p,{children:["\u5728 ",(0,r.jsx)(e.code,{children:"Furion"})," \u5305\u4e2d\u9ed8\u8ba4\u96c6\u6210\u4e86 ",(0,r.jsx)(e.code,{children:"EFCore"}),"\uff0c",(0,r.jsxs)(e.strong,{children:["\u5982\u679c\u4e0d\u4f7f\u7528 ",(0,r.jsx)(e.code,{children:"EFCore"}),"\uff0c\u53ef\u5b89\u88c5\u7eaf\u51c0\u7248 ",(0,r.jsx)(e.code,{children:"Furion.Pure"})," \u4ee3\u66ff ",(0,r.jsx)(e.code,{children:"Furion"})]}),"\u3002"]})}),"\n",(0,r.jsx)(e.admonition,{title:"\u67e5\u770b\u6700\u65b0\u62d3\u5c55\u6587\u6863",type:"tip",children:(0,r.jsx)(e.p,{children:(0,r.jsx)(e.a,{href:"https://gitee.com/dotnetchina/Furion/pulls/423",children:"https://gitee.com/dotnetchina/Furion/pulls/423"})})}),"\n",(0,r.jsx)(e.h2,{id:"1031-\u5173\u4e8e-mongodb",children:"10.3.1 \u5173\u4e8e MongoDB"}),"\n",(0,r.jsxs)(e.p,{children:[(0,r.jsx)(e.code,{children:"MongoDB"})," \u662f\u4e00\u4e2a\u57fa\u4e8e\u5206\u5e03\u5f0f\u6587\u4ef6\u5b58\u50a8\u7684\u6570\u636e\u5e93\u3002\u7531 ",(0,r.jsx)(e.code,{children:"C++"})," \u8bed\u8a00\u7f16\u5199\u3002\u65e8\u5728\u4e3a ",(0,r.jsx)(e.code,{children:"WEB"})," \u5e94\u7528\u63d0\u4f9b\u53ef\u6269\u5c55\u7684\u9ad8\u6027\u80fd\u6570\u636e\u5b58\u50a8\u89e3\u51b3\u65b9\u6848\u3002"]}),"\n",(0,r.jsxs)(e.p,{children:[(0,r.jsx)(e.code,{children:"MongoDB"})," \u662f\u4e00\u4e2a\u4ecb\u4e8e\u5173\u7cfb\u6570\u636e\u5e93\u548c\u975e\u5173\u7cfb\u6570\u636e\u5e93\u4e4b\u95f4\u7684\u4ea7\u54c1\uff0c\u662f\u975e\u5173\u7cfb\u6570\u636e\u5e93\u5f53\u4e2d\u529f\u80fd\u6700\u4e30\u5bcc\uff0c\u6700\u50cf\u5173\u7cfb\u6570\u636e\u5e93\u7684\u3002"]}),"\n",(0,r.jsx)(e.h2,{id:"1032-\u5982\u4f55\u96c6\u6210",children:"10.3.2 \u5982\u4f55\u96c6\u6210"}),"\n",(0,r.jsxs)(e.p,{children:["\u5728 ",(0,r.jsx)(e.code,{children:"Furion"})," \u6846\u67b6\u4e2d\uff0c\u5df2\u7ecf\u63a8\u51fa ",(0,r.jsx)(e.code,{children:"MongoDB"})," \u62d3\u5c55\u5305 ",(0,r.jsx)(e.a,{href:"https://www.nuget.org/packages/Furion.Extras.DatabaseAccessor.MongoDB",children:"Furion.Extras.DatabaseAccessor.MongoDB"}),"\u3002"]}),"\n",(0,r.jsxs)(e.h3,{id:"10321-\u6ce8\u518c-mongodb-\u670d\u52a1",children:["10.3.2.1 \u6ce8\u518c ",(0,r.jsx)(e.code,{children:"MongoDB"})," \u670d\u52a1"]}),"\n",(0,r.jsxs)(e.p,{children:["\u4f7f\u7528\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u8981\u5728 ",(0,r.jsx)(e.code,{children:"Startup.cs"})," \u4e2d\u6dfb\u52a0 ",(0,r.jsx)(e.code,{children:"services.AddMongoDB(connectionString)"})," \u5373\u53ef\u3002\u5982\uff1a"]}),"\n",(0,r.jsx)(e.pre,{children:(0,r.jsx)(e.code,{className:"language-cs",metastring:"showLineNumbers",children:'// \u65b9\u5f0f\u4e00\r\nservices.AddMongoDB("mongodb://localhost:27017");\r\n\r\n// \u65b9\u5f0f\u4e8c\r\nservices.AddMongoDB(new MongoClientSettings {});\r\n\r\n// \u65b9\u5f0f\u4e09\r\nservices.AddMongoDB(new MongoUrl {});\n'})}),"\n",(0,r.jsx)(e.h2,{id:"1033-\u57fa\u672c\u4f7f\u7528",children:"10.3.3 \u57fa\u672c\u4f7f\u7528"}),"\n",(0,r.jsxs)(e.p,{children:["\u5728\u4f7f\u7528\u4e4b\u524d\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u6784\u9020\u51fd\u6570\u6ce8\u5165 ",(0,r.jsx)(e.code,{children:"IMongoDBRepository"})," \u63a5\u53e3\uff0c\u5982\uff1a"]}),"\n",(0,r.jsxs)(e.ul,{children:["\n",(0,r.jsx)(e.li,{children:"\u975e\u6cdb\u578b\u7248\u672c"}),"\n"]}),"\n",(0,r.jsx)(e.pre,{children:(0,r.jsx)(e.code,{className:"language-cs",metastring:"showLineNumbers",children:"private readonly IMongoDBRepository _mongoRepository;\r\npublic PersonService(IMongoDBRepository mongoRepository)\r\n{\r\n _mongoRepository = mongoRepository;\r\n}\n"})}),"\n",(0,r.jsx)(e.h3,{id:"10331-\u5e38\u89c1\u4f8b\u5b50",children:"10.3.3.1 \u5e38\u89c1\u4f8b\u5b50"}),"\n",(0,r.jsx)(e.pre,{children:(0,r.jsx)(e.code,{className:"language-cs",metastring:"showLineNumbers",children:'var database = _mongoRepository.Context.GetDatabase("foo");\r\nvar collection = database.GetCollection<BsonDocument>("bar");\r\n\r\nawait collection.InsertOneAsync(new BsonDocument("Name", "Jack"));\r\n\r\nvar list = await collection.Find(new BsonDocument("Name", "Jack"))\r\n .ToListAsync();\r\n\r\nforeach(var document in list)\r\n{\r\n Console.WriteLine(document["Name"]);\r\n}\n'})}),"\n",(0,r.jsx)(e.pre,{children:(0,r.jsx)(e.code,{className:"language-cs",metastring:"showLineNumbers",children:'public class Person\r\n{\r\n public ObjectId Id { get; set; }\r\n public string Name { get; set; }\r\n}\r\n\r\nvar database = _mongoRepository.Context.GetDatabase("foo");\r\nvar collection = database.GetCollection<Person>("bar");\r\n\r\nawait collection.InsertOneAsync(new Person { Name = "Jack" });\r\n\r\nvar list = await collection.Find(x => x.Name == "Jack")\r\n .ToListAsync();\r\n\r\nforeach(var person in list)\r\n{\r\n Console.WriteLine(person.Name);\r\n}\n'})}),"\n",(0,r.jsxs)(e.h3,{id:"10332-\u83b7\u53d6-mongoclient-\u5bf9\u8c61",children:["10.3.3.2 \u83b7\u53d6 ",(0,r.jsx)(e.code,{children:"MongoClient"})," \u5bf9\u8c61"]}),"\n",(0,r.jsxs)(e.p,{children:[(0,r.jsx)(e.code,{children:"IMongoDBRepository"})," \u53ea\u5c01\u88c5\u4e86 ",(0,r.jsx)(e.code,{children:"MongoDB"})," \u57fa\u7840\u529f\u80fd\uff0c\u5982\u9700\u83b7\u53d6\u66f4\u591a\u64cd\u4f5c\u53ef\u901a\u8fc7 ",(0,r.jsx)(e.code,{children:".Context"})," \u5c5e\u6027\u83b7\u53d6 ",(0,r.jsx)(e.code,{children:"MongoClient"})," \u5bf9\u8c61\uff0c\u5982\uff1a"]}),"\n",(0,r.jsx)(e.pre,{children:(0,r.jsx)(e.code,{className:"language-cs",metastring:"showLineNumbers",children:"var client = _mongoRepository.Context;\n"})}),"\n",(0,r.jsx)(e.h2,{id:"1034-\u53cd\u9988\u4e0e\u5efa\u8bae",children:"10.3.4 \u53cd\u9988\u4e0e\u5efa\u8bae"}),"\n",(0,r.jsx)(e.admonition,{title:"\u4e0e\u6211\u4eec\u4ea4\u6d41",type:"note",children:(0,r.jsxs)(e.p,{children:["\u7ed9 Furion \u63d0 ",(0,r.jsx)(e.a,{href:"https://gitee.com/dotnetchina/Furion/issues/new?issue",children:"Issue"}),"\u3002"]})}),"\n",(0,r.jsx)(e.hr,{}),"\n",(0,r.jsx)(e.admonition,{title:"\u4e86\u89e3\u66f4\u591a",type:"note",children:(0,r.jsxs)(e.p,{children:["\u60f3\u4e86\u89e3\u66f4\u591a ",(0,r.jsx)(e.code,{children:"MongoDB"})," \u77e5\u8bc6\u53ef\u67e5\u9605 ",(0,r.jsx)(e.a,{href:"https://hub.fastgit.org/mongodb/mongo-csharp-driver",children:"MongoDB \u4ed3\u5e93"}),"\u3002"]})})]})}function h(n={}){const{wrapper:e}={...(0,s.a)(),...n.components};return e?(0,r.jsx)(e,{...n,children:(0,r.jsx)(a,{...n})}):a(n)}},1151:(n,e,o)=>{o.d(e,{Z:()=>c,a:()=>t});var r=o(7294);const s={},i=r.createContext(s);function t(n){const e=r.useContext(i);return r.useMemo((function(){return"function"==typeof n?n(e):{...e,...n}}),[e,n])}function c(n){let e;return e=n.disableParentContext?"function"==typeof n.components?n.components(s):n.components||s:t(n.components),r.createElement(i.Provider,{value:e},n.children)}}}]);