Results 1 to 4 of 4

Thread: MongoDB Help

  1. #1
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    259
    Rep Power
    9

    Default MongoDB Help

    I am trying to insert a document into my MongoDB. Here is my code:

    Java Code:
    MongoClient mc = new MongoClient("localhost");
    		MongoDatabase db = mc.getDatabase("db");
    		MongoCollection<Document> dbCol = db.getCollection("member");
    		
    		BasicDBObject doc = new BasicDBObject("name", "MongoDB")
    				.append("type", "database")
    		        .append("count", 1)
    		        .append("info", new BasicDBObject("x", 203).append("y", 102));
    		try
    		{
    			dbCol.insertOne(doc);
    		}
    		catch (MongoWriteException mwe)
    		{
    			System.out.println(mwe);
    		}
    		catch (MongoWriteConcernException mwce)
    		{
    			System.out.println(mwce);
    		}
    		catch (MongoException me)
    		{
    			System.out.println(me);
    		}
    On
    Java Code:
    dbCol.insertOne(doc);
    , I am getting this error:
    Java Code:
    The method insertOne(Document) in the type MongoCollection<Document> is not applicable for the arguments (BasicDBObject)
    How do I resolve this?

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: MongoDB Help

    What does the API doc for the MongoCollection class's insertOne() method say about its valid arguments?
    Is an instance of the BasicDBObject class a valid argument?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    259
    Rep Power
    9

    Default Re: MongoDB Help

    No, it is not.

    I am following a tutorial, but noticed that the method, insert, is no longer available: https://www.mkyong.com/mongodb/java-...rt-a-document/

  4. #4
    jameswarner is offline Member
    Join Date
    Mar 2016
    Location
    9345 Cemetery Dr. Scarsdale
    Posts
    5
    Rep Power
    0

    Default Re: MongoDB Help

    The method of insertOne() will be a connection to a MongoDB server for relevant database. For more information about insert record with MongoDB check: Quick Tour

Similar Threads

  1. MongoDB (java driver) problem
    By heatblazer in forum Advanced Java
    Replies: 1
    Last Post: 05-21-2013, 06:06 AM
  2. Integration mysql and mongodb in app
    By centos in forum Spring
    Replies: 0
    Last Post: 10-10-2012, 05:41 PM
  3. Java / Spring Data / Mongodb
    By gesf in forum New To Java
    Replies: 0
    Last Post: 07-13-2012, 11:27 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •