mongodb入门

tech2022-09-04  130

mongodb入门

This article was originally published on Code Barbarian. Thank you for supporting the partners who make SitePoint possible.

本文最初发表在Code Barbarian上 。 感谢您支持使SitePoint成为可能的合作伙伴。

Serverless architectures are becoming increasingly popular, and with good reason. In my experience, container-based orchestration frameworks have a steep learning curve and are overkill for most consumer-facing companies. With FaaS architectures, like AWS Lambda and Azure Functions, in theory the only devops you need is bundling and uploading your app.

无服务器架构正变得越来越受欢迎,这是有充分理由的。 以我的经验,基于容器的编排框架具有陡峭的学习曲线,并且对于大多数面向消费者的公司来说是过大的。 理论上 ,使用FaaS架构 (如AWS Lambda和Azure Functions) ,您唯一需要的开发工作就是捆绑和上传应用程序。

This article will walk you through setting up a Google Cloud Function in Node.js that connects to MongoDB. However, one major limitation of stateless functions is the need to establish a separate database connection every time the stateless function runs, which incurs a major performance penalty. Unfortunately, I haven’t been able to figure out how to reuse a database connection in Google Cloud Functions, the trick that works for IBM Cloud, Azure Functions, and AWS Lambda does not work for Google Cloud Functions.

本文将引导您在连接到MongoDB的Node.js中设置Google Cloud Function。 但是,无状态功能的一个主要局限性是每次无状态功能运行时都需要建立一个单独的数据库连接,这会导致严重的性能损失。 不幸的是,我一直无法弄清楚如何重用在谷歌云功能的数据库连接,窍门,对作品IBM云 , Azure的功能 ,以及AWS LAMBDA 不会为谷歌云职能的工作。

Google Cloud Functions中的“ Hello,World” (“Hello, World” in Google Cloud Functions)

Go to the Google Cloud Functions landing page and click “Try it free”.

转到Google Cloud Functions登陆页面 ,然后单击“免费试用”。

Click on the hamburger icon in the upper left and find the “Cloud Functions” link in the sidebar, then click “Create function”.

单击左上角的汉堡包图标,然后在侧栏中找到“云功能”链接,然后单击“创建功能”。

Name your function “hello-world” and leave the rest of the options in the “Create function” form unchanged. Leave “Function to execute” as “helloWorld”, because that needs to match the name of the function your code exports. Below is the code you should enter in, so you can confirm what version of Node.js your function is running on.

将函数命名为“ hello-world”,并保持“创建函数”表单中的其余选项不变。 将“要执行的功能”保留为“ helloWorld”,因为这需要与代码导出的功能名称匹配。 下面是您应该输入的代码,因此您可以确认函数运行在哪个版本的Node.js上。

exports.helloWorld = (req, res) => { res.send('Hello from Node.js ' + process.version); };

Click “Create” and wait for Google to deploy your cloud function. Once your function is deployed, click on it to display the function’s details.

点击“创建”,然后等待Google部署您的云功能。 部署功能后,单击它以显示功能的详细信息。

Click the “Trigger” tab to find your cloud function’s URL.

单击“触发器”选项卡以找到您的云功能的URL。

Copy the URL and use curl to run your cloud function.

复制URL并使用curl运行您的云功能。

$ curl https://us-central1-test21-201718.cloudfunctions.net/hello-world Hello from Node.js v6.11.5 $

Google Cloud Functions don’t give you any control over what version of Node.js you run, they run Node.js v6.11.5 currently. You can’t use async/await natively on Google Cloud Functions at the time of this writing.

Google Cloud Functions不能控制您运行的Node.js版本,它们当前运行的是Node.js v6.11.5。 在撰写本文时,您不能在Google Cloud Functions上本地使用async / await。

连接到MongoDB Atlas (Connecting to MongoDB Atlas)

Click on the “Source” tab in the function details and hit the “Edit” button. You’ll notice there’s 2 files in your source code, one of which is package.json. Edit package.json to match the below code.

单击功能详细信息中的“源”选项卡,然后单击“编辑”按钮。 您会注意到源代码中有2个文件,其中一个是package.json 。 编辑package.json以匹配以下代码。

{ "name": "sample-http", "version": "0.0.1", "dependencies": { "co": "4.6.0", "mongodb": "3.x" } }

Once you redeploy, Google Cloud will automatically install your npm dependencies for you. Now, change your index.js to match the below code, replacing the uri with your MongoDB Atlas URI.

重新部署后,Google Cloud会自动为您安装npm依赖项。 现在,更改index.js以匹配以下代码,将uri替换为MongoDB Atlas URI。

const co = require('co'); const mongodb = require('mongodb'); const uri = 'ATLAS_URI_HERE'; exports.helloWorld = (req, res) => { co(function*() { const client = yield mongodb.MongoClient.connect(uri); const docs = yield client.db('test').collection('tests').find().toArray(); res.send('Result: ' + JSON.stringify(docs)); }).catch(error => { res.send('Error: ' + error.toString()); }); };

Click “Save” to deploy your function. Once it is deployed, you should be able to curl your cloud function and get a document from MongoDB Atlas back.

单击“保存”以部署您的功能。 部署之后,您应该能够卷曲您的云功能并从MongoDB Atlas获得文档。

$ curl https://us-central1-test21-201718.cloudfunctions.net/hello-world Result: [{"_id":"5a7b7df69d07887542605888","name":"Hello!","__v":0}] $

At this point, you would try re-using the database connection using the same global state trick that works in AWS Lambda and other cloud function providers.

此时,您可以尝试使用与AWS Lambda和其他云函数提供程序相同的全局状态技巧重用数据库连接。

const co = require('co'); const mongodb = require('mongodb'); const uri = 'ATLAS_URI_HERE'; // Other cloud providers would retain this, but not Google Cloud let client = null; exports.helloWorld = (req, res) => { co(function*() { const reusedConnection = client != null; if (client == null) { client = yield mongodb.MongoClient.connect(uri); } const docs = yield client.db('test').collection('tests').find().toArray(); res.send('Result: ' + JSON.stringify(docs) + ', Reused: ' + reusedConnection); }).catch(error => { res.send('Error: ' + error.toString()); }) };

Unfortunately, the global state trick doesn’t seem to work in Google Cloud.

不幸的是,全局状态技巧似乎不适用于Google Cloud。

$ curl https://us-central1-test21-201718.cloudfunctions.net/hello-world Result: [{"_id":"5a7b7df69d07887542605888","name":"Hello!","__v":0}], Reused: false $ $ curl https://us-central1-test21-201718.cloudfunctions.net/hello-world Result: [{"_id":"5a7b7df69d07887542605888","name":"Hello!","__v":0}], Reused: false $

继续 (Moving On)

FaaS is a powerful paradigm, but purely stateless functions suffer from performance limitations when dealing with databases because establishing a new database connection is costly. Most cloud function providers have a mechanism for retaining database connections between function calls, but apparently Google Cloud Functions does not. This severely limits Google Cloud Functions’ ability to serve as a replacement for a conventional web server.

FaaS是一种强大的范例,但是在处理数据库时,纯粹的无状态功能会受到性能限制,因为建立新的数据库连接成本很高。 大多数云函数提供程序都具有一种机制,可以在函数调用之间保留数据库连接,但是显然Google Cloud Functions却没有。 这严重限制了Google Cloud Functions替代传统Web服务器的能力。

翻译自: https://www.sitepoint.com/getting-started-google-cloud-functions-mongodb/

mongodb入门

相关资源:jdk-8u281-windows-x64.exe
最新回复(0)