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

  • SEARCH
  • Home
  • 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 6730173
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:22:34+00:00 2026-05-26T10:22:34+00:00

I am new at MongoDB and I want to insert to mongodb data like

  • 0

I am new at MongoDB and I want to insert to mongodb data like this but I couldn’t figure out how

{
  image = "cab"
  tags = [
            [ "NNP", 0 ],
            [ "NN", 1 ]
         ]
},
{
  image = "castle"
  tags = [
            [ "NNP", 2 ],
            [ "NN", 1 ],
         ]
}

my code is

    BasicDBObject obj = new BasicDBObject();
    obj.put("images", ....);
    for(Tag tag:tags){
    BasicDBObject tagsObj = new BasicDBObject();
    tagsObj.put("NNP",tag.getNNP());
    tagsObj.put("NN",tag.getNN());
    obj.put("tags",tagsObj);

    }

UPDATE:
using this code

Mongo m = new Mongo();
DB db = m.getDB("test");
DBCollection coll = db.getCollection("tags");
for(Tag tag:tags){
    BasicDBList dbl = new BasicDBList();
    dbl.add(new BasicDBObject("NNP",tag.getNNP()));
    dbl.add(new BasicDBObject("NN", tag.getNNP()));
    BasicDBObject outer=new BasicDBObject("images", currentImageName).append("tags", dbl);
    coll.insert(outer);
                }

I store every image alone cause the tags might be like this for the same image

  {
      image = "cab",
      tags = [
                { "NNP", 0 },
                { "NN", 1 }
             ],
             [  {"NNP", 4 },
                { "NN", 5 } 
             ],
             [
                {"NNP", 0 },
                { "NN", 4 }
             ]

},

Thanks

  • 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-26T10:22:35+00:00Added an answer on May 26, 2026 at 10:22 am

    Basically you use BasicDBObject for key-value mappings and BasicDBList for array objects. For the object in your question, you’d do this:

    BasicDBList dbl = new BasicDBList();
    dbl.add(new BasicDBObject("NNP",0));
    dbl.add(new BasicDBObject("NN", 1));
    BasicDBOBject outer=new BasicDBObject("image", "cab").append("tags", dbl);
    

    There’s some convenience methods in the api to make this a bit less verbose.

    The mapping works like this:

    for:  {"A":1}  use: new BasicDBObject("A",1)
    for: {"A":1, "B":2}  use: new BasicDBObject("A",1).append("B",2)
    for: {"A":{"B":2}}  use: new BasicDBObject("A",new BasicDBObject("B",2))
    for: {"A":["B","C"]} use:
            BasicDBList dbl = new BasicDBList();
            dbl.add("B");
            dbl.add("C");
             ->  new BasicDBObject("A",dbl);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to MongoDB and trying out some fundamentals, but this astonished me. I'm
I am new to MongoDB, but am looking at it to solve this problem:
I have data something like this in mongodb { _id : ObjectId(4f0ee7310b09f7a254000001), createdAt :
I have this regex on mongodb query to match words by prefix: {sentence: new
I want to run a data transformation / migration for MongoDB. I found the
I am having trouble with expressing this query in C# MongoDB, I want it
i am new to mongoDB. I am using mongoDB in web app. I want
Here's my db: $db = new Mongo(mongodb://u:pw@server.com:37068/dbname); I want to echo a list/array of
I want to try Mongodb w/ mongoengine. I'm new to Django and databases and
I am new to mongodb/mongomapper and can't find an answer to this. I have

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.