英文:
AWS Lambda Java function to update query in MongoDB
问题
以下是翻译好的内容:
我对AWS Lambda还很新,我已经创建了示例Lambda函数并成功部署。现在我想要将我的Lambda与MongoDB连接,并且需要使用查询来更新集合,但是没有找到相关的信息。
我尝试集成了Spring到Lambda中,但是经过研究后我发现Spring不被推荐在Lambda中使用,也不是一个好的实践。我想要连接到MongoDB数据库。
以下是示例Java代码:
public class App implements RequestHandler<Object, Object> {
@Override
public Object handleRequest(Object o, Context context) {
System.out.println("欢迎来到Lambda函数!");
// 在这里添加连接MongoDB和更新集合的代码
return null;
}
}
英文:
I am new to AWS lambda, I have created sample lambda function and deploy it successfully. Now I want to connect my lambda with Mongodb and need to update collection using query but didn't get any information for the same.
I have tried integrating spring in lambda but after researching I found out that spring is not recommended and not good practice to use in lambda. I want to connect to the MongoDB database.
Below is the sample java code.
public class App implements RequestHandler<Object, Object> {
@Override
public Object handleRequest(Object o, Context context) {
System.out.println("welcome to lambda function yeh.!!!");
return null;
}
}
答案1
得分: 2
你可以在不使用Spring的情况下轻松连接到MongoDB。
有很多教程可以帮助你。
如果要在Lambda函数内使用它,请确保:
- 正确打包所有依赖项(例如,使用Maven Shade插件)
- 手动处理连接,因为你的应用程序不会持续运行。这并不是一个简单的任务,但也有很多在线资源介绍如何处理这个问题。
因为这是一个堆栈溢出(SO)的问题,你应该尝试首先自己实现。如果你遇到问题,请发布你的代码,你将得到进一步的支持!
英文:
You can easily connect to MongoDB without Spring.
There are a lot of tutorials out there which will help you.
For using it inside lambda, make sure to
- package all your dependencies correctly (e.g. use the maven shade plugin)
- handle your connections manually, as your application is not running continuously. That's not a trivial task, but there are also a lot of sources online how to do this.
As this is SO, you should try to do your first implementation yourself & if you're facing issues, please post your code and you'll get further support!
答案2
得分: 1
<!-- 在pom.xml中添加以下Maven依赖项 -->
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>2.12.3</version>
</dependency>
// 在处理程序中添加以下代码以在MongoDB中更新查询。
@Override
public Object handleRequest(Object o, Context context) {
System.out.println("欢迎使用Lambda函数!");
try {
MongoClient mongo = new MongoClient("localhost", 27017);
DB db = mongo.getDB("test");
DBCollection col = db.getCollection("contact");
System.out.println("数据数量:" + col.count());
BasicDBObject setValue = new BasicDBObject();
BasicDBObject query = new BasicDBObject();
query.put("contacts.mobileNo", "7090909090");
setValue.put("contacts.$.userId", "userId12121212");
BasicDBObject set = new BasicDBObject("$set", setValue);
col.updateMulti(query, set);
// 关闭资源
mongo.close();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
英文:
added below maven dependency in pom.xml
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>2.12.3</version>
</dependency>
> written below code in handler to update query in mongoDB .
@Override
public Object handleRequest(Object o, Context context) {
System.out.println("welcome to lambda function yeh.!!!");
try {
MongoClient mongo = new MongoClient("localhost", 27017);
DB db = mongo.getDB("test");
DBCollection col = db.getCollection("contact");
System.out.println("data count" + col.count());
BasicDBObject setValue = new BasicDBObject();
BasicDBObject query = new BasicDBObject();
query.put("contacts.mobileNo", "7090909090");
setValue.put("contacts.$.userId", "userId12121212");
BasicDBObject set = new BasicDBObject("$set", setValue);
col.updateMulti(query, set);
//close resources
mongo.close();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论