Array of MongoDB Java drivers

I am trying to save a tag set in a mongodb document, for example.

{
    id:"104454",
    tags:["tag1", "tag2"]
}

I am struggling to figure out how to do this with the Java driver. I thought I would use it BasicDBList, but that doesn't seem right.

Can anyone help?

Thanks in advance.

+3
source share
2 answers

You can use simple arrays, and then you can do something like:

doc.put("tags", array)
+5
source

When storing arrays in MongoDB with Java according to an online doc, you can use anything that extends List.

So, using your example, it will look something like this:

ArrayList tags = new ArrayList();
tags.add("tag1");
tags.add("tag2");

BasicDBObject doc = new BasicDBObject(new ObjectId(), tags);
+3
source

Source: https://habr.com/ru/post/1776583/


All Articles