在 MongoDB 中的对象中插入数组

发布于 2024-12-11 18:47:26 字数 1351 浏览 0 评论 0原文

我是 MongoDB 的新手,我想像这样插入 mongodb 数据,但我不知道

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

我的代码是

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

    }

如何更新的: 使用此代码

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

我单独存储每个图像,因为同一张图像的标签可能是这样的

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

},

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

真心难拥有 2024-12-18 18:47:26

基本上,您使用 BasicDBObject 进行键值映射,使用 BasicDBList 进行数组对象。对于您问题中的对象,您可以这样做:

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

api 中有一些方便的方法可以使其不那么冗长。

映射的工作原理如下:

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

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);
顾北清歌寒 2024-12-18 18:47:26

你的意思是这样吗?

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

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);
    }
走过海棠暮 2024-12-18 18:47:26

以下是我在使用 mongo3.x 时的使用方式:

假设您希望结果如下所示: {"data": [{"key":"v1"}, {"key":"v1"} , {"key":"v1"}] }

[step1]: 使用 Java Map 创建 json 对象,该对象映射到数组内的元素;也就是[]里面的{}

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

[步骤2]:使用Java List将所有Java Map添加到一个元素中。

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

[step3]: 将 Java 列表添加到 mongo

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

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

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

[step3]: add Java list into mongo

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文