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 8441303
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T08:32:09+00:00 2026-06-10T08:32:09+00:00

We are using a Java server and Mongo DB [plain Java-Mongo and not Morphia

  • 0

We are using a Java server and Mongo DB [plain Java-Mongo and not Morphia or some such tool for the CRUD].
We are having a Image Pojo class and its related Metadata like below,

public class Img{
    private String name;
    private List<Metadata> imgMetaList = new ArrayList<Metadata>();

    //Getters, setters etc...
    public List<Metadata> getImgMetaList() {        
        return imgMetaList;
    }
}

Metadata class has some data, implementing Serializable didn’t work, so
i extended ReflectionDBObject,

public class Metadata extends ReflectionDBObject{
    private String tag;
    private String val;

    //Getters, setters etc...      
}

I want to save the Img into Mongo. I used the following code and it worked.

BasicDBObject updateQuery = new BasicDBObject();
updateQuery.put("name", img.getName());
BasicDBObject updMetadata = new BasicDBObject();
updMetadata.put("$push", new BasicDBObject("imgMetaList",img.getImgMetaList()) );
collection.update(updateQuery,updMetadata, true, false);    

This inserts a document in Mongo as below,

{
    "_id" : ObjectId("503a1991db2e9f431cf0d162"),
    "name" : "test.jpg",
    "imgMetaList" : [
        [
            {
                    "Tag" : "tag1",
                    "Val" : "val1",
                    "_id" : null
            }
        ]
    ]
}

Here there are 2 issues,
1. The code is inserting two square brackets instead of one to house the array
2. why isn’t the _id getting generated for the list.

Please let me know.
Regards, Vish

  • 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-06-10T08:32:10+00:00Added an answer on June 10, 2026 at 8:32 am

    $push appends a value to an array. If the array does not exist yet, a new array is created. The item you are pushing is an array, so you end up with an array inside an array.

    An _id is not being generated for the collection because this is normal behaviour for MongoDB. When you put a sub-document in an document, the sub-document does not get an _id. Hence the array does not have an _id.

    Perhaps instead you are asking why your metadata document, inside the array, has a null _id. This is a behaviour of the ReflectionDBObject class: it includes an _id field, but it is up to you to set its value. You can set this _id to a non-null value by calling set_id() on your ReflectionDBObject instance. A good place to do this might be in the constructor of your Metadata class. To generate an _id, use the ObjectId.get() static method (org.bson.types.ObjectId).

    Your code for inserting the image in MongoDB is more complicated than it needs to be. The following will work (and will get rid of the nested array):

    BasicDBObject imageDoc = new BasicDBObject();
    imageDoc("name", img.getName());
    imageDoc("imgMetaList", img.getImgMetaList());
    collection.insert(imageDoc);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently looking in to some file uploading using Java Server Faces. I've
I'm using java server pages for the first time and I'm having a problem
I'm trying to post data on a server using java HttpUrlConnection class. It seems
I'm trying to send an image using a Java server to my Android device
I have a Java based web-application using Java Server Faces and Facelets. I am
I am using java on sql server and I have a DB problem of
I have developed a Java server using Eclipse that accepts TCP socket connection from
I am using IMAP protocol to fetch mails from Google server using java mail.
Does anyone have any code for connecting to a remote server using java and
i am trying to link a Unity game to a Java server using C#

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.