Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6813043
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:31:24+00:00 2026-05-26T20:31:24+00:00

Im using java/mongodb. I would like to create a table (collection) with some users.

  • 0

Im using java/mongodb. I would like to create a table (collection) with some users. Well, its working im just not sure about my coding style. Could be this good if i just want to add 3 new persons to the collection?

    BasicDBObject doc = new BasicDBObject();
    doc.put("name", "klaus");
    doc.put("age", 30);
    doc.put("city", "new york");
    col.insert(doc);

    BasicDBObject doc2 = new BasicDBObject();
    doc2.put("name", "mirko");
    doc2.put("age", 23);
    doc2.put("city", "madrid");
    col.insert(doc2);

    BasicDBObject doc3 = new BasicDBObject();
    doc3.put("name", "jon");
    doc3.put("age", 34);
    doc3.put("city", "unknown");
    col.insert(doc3);

Its look so “long”. To add only 3 Persons I have to create always a new BasicDBObject and insert it with the same line code (col.insert(bla))? It cant be ^^ And an other question too: At the first time(doc) im adding name, age and city as column. Why do I have to add “name”, “age” and “city” again and again… I just want to add “mirko”, 23 and “madrid” for doc2.

The last thing what bugs me is that I can add a new Dokument to the Collection with the same(!) values. I could add a new jon with 34 years and the same city. Is this ok? And if yes, I would like to change it. Howto?

Thank you!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T20:31:25+00:00Added an answer on May 26, 2026 at 8:31 pm

    Use json for it..

    String json = "{'key' : 'value'}"; /* Create json formatted data here */
    
    DBObject dbObject = (DBObject)JSON.parse(json);
    
    collection.insert(dbObject);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using mongodb to store information of users. I wanted to create a
I am using Java 1.4 with Log4J. Some of my code involves serializing and
I've just started using Java's enums in my own projects (I have to use
I am using Mongodb Java driver for my Java client application. it needs to
Casbah is another layer on top of Java driver for MongoDb. Would this reduce
Im using mongodb + java. I tried to insert a new document into an
I need to know how to use where in MongoDB queries using Java Driver.
I want to store image documents in MongoDB. I am using java. Any links
I am new to mongoDB. I am using java and mongoDB. I have a
Using java in one process, how can we create another process that is an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.