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