3

I am trying to save a set of tags within a mongodb document e.g.

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

I am struggling to figure out how to do this with the Java Driver though. I thought I would use BasicDBList but this doesnt seem to be right.

Could someone help please?

Thanks in advance.

2
  • mongodb.org/display/DOCS/Java+Tutorial Commented Nov 23, 2010 at 23:29
  • I have already read through this but as far as I can see it doesn't show how to create JSON arrays? Commented Nov 23, 2010 at 23:34

2 Answers 2

5

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

doc.put("tags", array)
Sign up to request clarification or add additional context in comments.

Comments

3

When saving arrays into MongoDB with Java, according to the online doc, you can use anything that extends List.

So, using your example, that would be something like the following:

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

BasicDBObject doc = new BasicDBObject(new ObjectId(), tags);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.