本文介绍了使用java在Mongodb中插入文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 school 的集合,我想使用 java 插入文档.我使用了下面的代码

I have a Collection named school and i wanted to insert document using java.I used the below code

import com.mongodb.MongoClient;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.BasicDBObject;

public class MongoDBJDBC{
public static void main( String args[] ){
  try{
 // To connect to mongodb server
     MongoClient mongoClient = new MongoClient( "localhost" , 27017 );
     // Now connect to your databases
     DB db = mongoClient.getDB( "cms" );
 System.out.println("Connect to database successfully");
     boolean auth = db.authenticate(myUserName, myPassword);
System.out.println("Authentication: "+auth);
     DBCollection school = db.getCollection("school");
     System.out.println("Collection mycol selected successfully");
     BasicDBObject doc = new BasicDBObject("title", "name").
        append("description", "about the school").
        append("likes", 1000).
        append("url", "http://www.tutorialspoint.com/mongodb/").
        append("by", "mytutorials point");
     school.insert(doc);
     System.out.println("Document inserted successfully");
  }catch(Exception e){
     System.err.println( e.getClass().getName() + ": " + e.getMessage() );
  }

}}

并且它工作正常.

在上面的程序中,文档值是硬编码的,在应用程序环境中,用户无法看到源代码.现在我想从用户界面插入文档值作为用户输入.请帮帮我

In the above program the document values are hardcoded in which in application environment the user cant see the source code.Now i want to insert document values as user entry from UI.Please Help me

推荐答案

考虑你有一个如下表格:

Consider You have a form as below:

<form action="/test/dbOperation" method="POST">
    <input type="text" id="title" name="title">
    <input type="text" id="description" name="description">
    <input type="text" id="likes" name="likes">
    <input type="text" id="url" name="url">
    <input type="text" id="by" name="by">
    <button type="submit" value="Submit">
</form>

现在在 Spring 中,您可以使用注解将类作为控制器来处理请求

Now in Spring you can use annotation to make a class as Controller where you need to handle the request

@Controller
@RequestMapping("/test")
public class formCntrl {
    @RequestMapping(value = "/dbOperation", method = RequestMethod.POST)
    public @ResponseBody String (HttpServletResponse resp,
        @RequestParam("title") String title,
        @RequestParam("description") String description,
        @RequestParam("likes") int likes,
        @RequestParam("url") String url,
        @RequestParam("by") String by) {
            try{
                // To connect to mongodb server
                MongoClient mongoClient = new MongoClient( "localhost" , 27017 );
               // Now connect to your databases
               DB db = mongoClient.getDB( "cms" );
               System.out.println("Connect to database successfully");
               boolean auth = db.authenticate(myUserName, myPassword);
               System.out.println("Authentication: "+auth);
               DBCollection school = db.getCollection("school");
               System.out.println("Collection mycol selected successfully");
               BasicDBObject doc = new BasicDBObject("title", title).
                   append("description", description).
                   append("likes", likes).
                   append("url", url).
                   append("by", by);
               school.insert(doc);
               System.out.println("Document inserted successfully");
             }catch(Exception e){
                 System.err.println( e.getClass().getName() + ": " + e.getMessage() );
             }
    }
}

通常在控制器中混淆数据库逻辑不是一件好事.使用 Spring MVC,其中您可以拥有 DAO 层,该层提供数据库逻辑与业务逻辑的分离.

Generally its not a good deal to mix up the database logic in controller. Use Spring MVC where in you can have DAO layer which provides separation of database logic with business logic.

希望有帮助.

这篇关于使用java在Mongodb中插入文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 12:46
查看更多