java - Mongodb database is not created -
i creating mongodb database , trying insert records in problem database not created database name "mymongodb" , collection name chanel when run it,it gives error , build successful
package databaseconnection; import com.mongodb.basicdbobject; import com.mongodb.db; import com.mongodb.dbcollection; import com.mongodb.mongoclient; import java.net.unknownhostexception; public class insertdriver { public static void main(string args[])throws unknownhostexception { db db=(new mongoclient("localhost",8080)).getdb("mymongodb"); dbcollection dbcollection=db.getcollection("chanel"); basicdbobject basicdbobject=new basicdbobject(); basicdbobject.put("name", "dhiraj"); basicdbobject.put("subscription", 4100); dbcollection.insert(basicdbobject); } } exception in thread "main" java.lang.nosuchmethoderror: com.mongodb.readpreference.primary()lcom/mongodb/readpreference; @ com.mongodb.mongoclientoptions$builder.<init>(mongoclientoptions.java:52) @ com.mongodb.mongoclient.<init>(mongoclient.java:128) @ com.mongodb.mongoclient.<init>(mongoclient.java:117) @ databaseconnection.insertdriver.main(insertdriver.java:21)
one thing want clear, databases created in mongodb when insert data in collection of database.
first of all, check mongodb running on machine (by default run on port 27017)?
try insert sample data mongo shell.
sample commands:
use testdb db.testcollection.insert({"name":"dev"});
it inset data in testcollection
of testdb
database. can find using :
db.testcollection.find()
if working fine. proceed java driver.
you code looks besides 8080 port (i assuming manually changed port 27017 8080) , make sure mongodb running.
Comments
Post a Comment