Sure, here’s the translation: AWS Lambda Java函数用于更新MongoDB中的查询

huangapple go评论177阅读模式
英文:

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&lt;Object, Object&gt; {


    @Override
    public Object handleRequest(Object o, Context context) {

        System.out.println(&quot;welcome to lambda function yeh.!!!&quot;);

        return null;
    }

}

答案1

得分: 2

你可以在不使用Spring的情况下轻松连接到MongoDB。

有很多教程可以帮助你。

如果要在Lambda函数内使用它,请确保:

因为这是一个堆栈溢出(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

  &lt;dependency&gt;
            &lt;groupId&gt;org.mongodb&lt;/groupId&gt;
            &lt;artifactId&gt;mongo-java-driver&lt;/artifactId&gt;
            &lt;version&gt;2.12.3&lt;/version&gt;
  &lt;/dependency&gt;

> written below code in handler to update query in mongoDB .

@Override
    public Object handleRequest(Object o, Context context) {

        System.out.println(&quot;welcome to lambda function yeh.!!!&quot;);
        try {
            MongoClient mongo = new MongoClient(&quot;localhost&quot;, 27017);
            DB db = mongo.getDB(&quot;test&quot;);
            DBCollection col = db.getCollection(&quot;contact&quot;);
            System.out.println(&quot;data count&quot; + col.count());

            BasicDBObject setValue = new BasicDBObject();
            BasicDBObject query = new BasicDBObject();
            query.put(&quot;contacts.mobileNo&quot;, &quot;7090909090&quot;);
            setValue.put(&quot;contacts.$.userId&quot;, &quot;userId12121212&quot;);
            BasicDBObject set = new BasicDBObject(&quot;$set&quot;, setValue);
            col.updateMulti(query, set);
            //close resources
            mongo.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

huangapple
  • 本文由 发表于 2020年7月27日 14:28:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/63109782.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定