简体   繁体   中英

Insert Array inside an object in MongoDB

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

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);

Did you mean like this?

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

Here is how i use it when using mongo3.x:

suppose you want the result to be like this: {"data": [{"key":"v1"}, {"key":"v1"}, {"key":"v1"}] }

[step1]: use Java Map to create json object which maps to the elements inside the array; that is, the {} inside []

[step1 Ans]: Map m1,m2,m3 = new HashMap(); m1.put("key", "v1"); m2.put("key", "v1"); m3.put("key", "v1"); Map m1,m2,m3 = new HashMap(); m1.put("key", "v1"); m2.put("key", "v1"); m3.put("key", "v1");

[step2]: use Java List to add all Java Map into one element.

[step2 Ans]: List list = new ArrayList(); list.add(m1); list.add(m2); list.add(m3); List list = new ArrayList(); list.add(m1); list.add(m2); list.add(m3);

[step3]: add Java list into mongo

[step3 Ans]: Document dc = new Document("key", list);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM