[英]MongoDB Java Driver Array
I am trying to save a set of tags within a mongodb document eg 我试图在mongodb文档中保存一组标签,例如
{
id:"104454",
tags:["tag1", "tag2"]
}
I am struggling to figure out how to do this with the Java Driver though. 我正在努力弄清楚如何使用Java驱动程序执行此操作。 I thought I would use BasicDBList
but this doesnt seem to be right. 我以为我会使用BasicDBList
但这似乎不对。
Could someone help please? 有人可以帮忙吗?
Thanks in advance. 提前致谢。
您可以使用简单数组,然后您可以执行以下操作:
doc.put("tags", array)
When saving arrays into MongoDB with Java, according to the online doc , you can use anything that extends List
. 使用Java将数组保存到MongoDB中时,根据在线文档 ,您可以使用扩展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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.