使用java在Mongodb中插入文档

时间:2014-10-06 07:54:19

标签: java mongodb

我有一个名为school的Collection,我想使用java插入文档。我使用下面的代码

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() );
  }

} }

并且工作正常。

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

1 个答案:

答案 0 :(得分:1)

考虑您有一个表格如下:

<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中,您可以使用注释将类创建为需要处理请求的Controller

@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层,该层提供数据库逻辑与业务逻辑的分离。

希望它有所帮助。