使用 java 和 mongodb api 从 mongodb 检索数组
retrieve array from mongodb using java with mongodb api
我知道有很多相同的问题,而且都得到了很好的回答。问题是所有这些问题都使用 MongoDBObject、MongoDBList 来检索数组。我的问题是我正在使用 http://api.mongodb.org/java/3.0/index.html?overview-summary.html api 并且我很难检索数组并向其添加元素。我必须使用 MongoCollection、MongoDatabase 和 MongoClient。我正在尝试解决 mongodb 课程的作业。问题陈述是找到一个数组并在mongod中更新它。
这是我试过的
Document post = null; Bson filter = new Document("permalink",
permalink); Bson projection = new Document("comments", true);
List<Document> comments = postsCollection.find(filter)
.projection(projection).into(new ArrayList<Document>());
System.out.println(comments);
post = postsCollection.find(Filters.eq("permalink",
permalink)).first();
Document newComment = new Document();
newComment.append("author", name); newComment.append("body", body);
if (email != null && (!email.equals(""))) {
newComment.append("email", email); }
comments.add(newComment);
Bson filter2 = new Document("_id", post.get("_id"));
System.out.println(comments); post =
postsCollection.find(filter).first();
postsCollection.updateOne(filter2, new Document("$unset",new
Document("comments",true))); postsCollection.updateOne(filter2, new
Document("$set", new Document( "comments", comments)));
这不会创建新评论。相反,它会在 comments 数组本身中创建另一个 comments 数组。数组应该在 student
中更新
这是json数据
{
"_id" : ObjectId("55d965eee60dd20c14e8573e"),
"title" : "test title",
"author" : "prasad",
"body" : "test body",
"permalink" : "test_title",
"tags" : [
"test",
"teat"
],
"date" : ISODate("2015-08-23T06:19:26.826Z"),
"comments" : [
{
"_id" : ObjectId("55d965eee60dd20c14e8573e"),
"comments" : [
{
"_id" : ObjectId("55d965eee60dd20c14e8573e"),
"comments" : []
},
{
"author" : "commented",
"body" : "something in comment",
"email" : "some@thing.com"
}
]
},
{
"author" : "commented",
"body" : "something in comment",
"email" : "some@thing.com"
}
]
}
你不需要写这么多代码。请检查以下代码,
public void addPostComment(final String name, final String email, final String body,
final String permalink) {
Document post = findByPermalink(permalink);
List<Document> comments = null;
Document comment = new Document();
if(post != null){
comments = (List<Document>)post.get("comments");
comment.append("author",name).append("body", body);
if(email != null){
comment.append("email", email);
}
comments.add(comment);
postsCollection.updateOne(new Document("permalink",permalink),
new Document("$set",new Document("comments",comments)));
}
}
为了避免未经检查的转换和 linter 警告,以及编写您自己的循环,请使用库的 get(final Object key, final Class<T> clazz)
方法:
List<Document> comments = posts.get("comments", docClazz)
其中 docClazz
是您创建一次的东西:
final static Class<? extends List> docClazz = new ArrayList<Document>().getClass();
这里简化了很多!
版本 - mongo-java-driver-3.12.5.jar
comments = post.getList("comments", Document.class);
如果您被迫使用旧版本的 mongo 驱动程序并且您不能使用 MKP 提到的方法,那么您可以复制该方法本身。
这里是 Kotlin 扩展
import org.bson.Document
import java.lang.String.format
fun <T> Document.getList(key: String, clazz: Class<T>, defaultValue: List<T>): List<T> {
val list = this.get(key, List::class.java)
if (list == null) {
return defaultValue
}
list.forEach {
if (!clazz.isAssignableFrom(it!!::class.java)) {
throw ClassCastException(format("List element cannot be cast to %s", clazz.getName()))
}
}
return list as List<T>
}
我知道有很多相同的问题,而且都得到了很好的回答。问题是所有这些问题都使用 MongoDBObject、MongoDBList 来检索数组。我的问题是我正在使用 http://api.mongodb.org/java/3.0/index.html?overview-summary.html api 并且我很难检索数组并向其添加元素。我必须使用 MongoCollection、MongoDatabase 和 MongoClient。我正在尝试解决 mongodb 课程的作业。问题陈述是找到一个数组并在mongod中更新它。
这是我试过的
Document post = null; Bson filter = new Document("permalink",
permalink); Bson projection = new Document("comments", true);
List<Document> comments = postsCollection.find(filter)
.projection(projection).into(new ArrayList<Document>());
System.out.println(comments);
post = postsCollection.find(Filters.eq("permalink",
permalink)).first();
Document newComment = new Document();
newComment.append("author", name); newComment.append("body", body);
if (email != null && (!email.equals(""))) {
newComment.append("email", email); }
comments.add(newComment);
Bson filter2 = new Document("_id", post.get("_id"));
System.out.println(comments); post =
postsCollection.find(filter).first();
postsCollection.updateOne(filter2, new Document("$unset",new
Document("comments",true))); postsCollection.updateOne(filter2, new
Document("$set", new Document( "comments", comments)));
这不会创建新评论。相反,它会在 comments 数组本身中创建另一个 comments 数组。数组应该在 student
中更新这是json数据
{
"_id" : ObjectId("55d965eee60dd20c14e8573e"),
"title" : "test title",
"author" : "prasad",
"body" : "test body",
"permalink" : "test_title",
"tags" : [
"test",
"teat"
],
"date" : ISODate("2015-08-23T06:19:26.826Z"),
"comments" : [
{
"_id" : ObjectId("55d965eee60dd20c14e8573e"),
"comments" : [
{
"_id" : ObjectId("55d965eee60dd20c14e8573e"),
"comments" : []
},
{
"author" : "commented",
"body" : "something in comment",
"email" : "some@thing.com"
}
]
},
{
"author" : "commented",
"body" : "something in comment",
"email" : "some@thing.com"
}
]
}
你不需要写这么多代码。请检查以下代码,
public void addPostComment(final String name, final String email, final String body,
final String permalink) {
Document post = findByPermalink(permalink);
List<Document> comments = null;
Document comment = new Document();
if(post != null){
comments = (List<Document>)post.get("comments");
comment.append("author",name).append("body", body);
if(email != null){
comment.append("email", email);
}
comments.add(comment);
postsCollection.updateOne(new Document("permalink",permalink),
new Document("$set",new Document("comments",comments)));
}
}
为了避免未经检查的转换和 linter 警告,以及编写您自己的循环,请使用库的 get(final Object key, final Class<T> clazz)
方法:
List<Document> comments = posts.get("comments", docClazz)
其中 docClazz
是您创建一次的东西:
final static Class<? extends List> docClazz = new ArrayList<Document>().getClass();
这里简化了很多!
版本 - mongo-java-driver-3.12.5.jar
comments = post.getList("comments", Document.class);
如果您被迫使用旧版本的 mongo 驱动程序并且您不能使用 MKP 提到的方法,那么您可以复制该方法本身。
这里是 Kotlin 扩展
import org.bson.Document
import java.lang.String.format
fun <T> Document.getList(key: String, clazz: Class<T>, defaultValue: List<T>): List<T> {
val list = this.get(key, List::class.java)
if (list == null) {
return defaultValue
}
list.forEach {
if (!clazz.isAssignableFrom(it!!::class.java)) {
throw ClassCastException(format("List element cannot be cast to %s", clazz.getName()))
}
}
return list as List<T>
}