0

I am fairly new to MongoDB and i've got an issue on how to store data from an Array into a mongodb database? For some reason when i implement the following code i get a lot of errors.

    private void storeData(String[] trainSeats, String[] customerName, String[] traincustomerSeats){

        MongoClient dbb = new MongoClient("localhost", 27017);
        DB dbs = dbb.getDB("Train Customers and Seats");
        DBCollection coll1 = dbs.getCollection("CW1");
        DBCollection coll2 = dbs.getCollection("CW2");
        DBCollection coll3 = dbs.getCollection("CW3");

        BasicDBObject oneDoc = new BasicDBObject();
        for (int k = 1; k < trainSeats.length; k++){
            oneDoc.append(String.valueOf(k), trainSeats[k]);
        }
        coll1.insert(oneDoc);

        BasicDBObject twoDoc = new BasicDBObject();
        for(int k = 1; k < customerName.length; k++){
            twoDoc.append(String.valueOf(k), customerName[k]);
        }
        coll2.insert(twoDoc);

        BasicDBObject threeDoc = new BasicDBObject();
        for(int k = 1; k < traincustomerSeats.length; k++){
            threeDoc.append(String.valueOf(k), traincustomerSeats[k]);
        }
        coll3.insert(threeDoc);

        optionEnter(trainSeats,customerName,traincustomerSeats);
    }

Here are the errors i get when i run the above code:

click to view the errors

enter image description here

4
  • Can you include the errors in your post? Commented Mar 18, 2020 at 2:44
  • If you have the option of using Spring Data, it makes life a lot easier. Commented Mar 18, 2020 at 2:54
  • MongoDB Java Tutorials have exaples to write data to the database. Commented Mar 18, 2020 at 2:55
  • 1
    @JDHernandez i added an image link on the post - those are the errors Commented Mar 18, 2020 at 2:58

1 Answer 1

1

I saw your error log. You are getting this error because your database name "Train Customers and Seats" have lot of spaces ' ', which is not allowed in the database name.

Rename your database to "Train_Customers_and_Seats" or "TrainCustomersAndSeats"

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.