使用带有mongodb api的java从mongodb中检索数组

Pra*_*kar 3 java mongodb mongodb-java

我知道有很多问题,并且答案很好.问题是所有这些问题都使用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)));
Run Code Online (Sandbox Code Playgroud)

这不会创建新评论.相反,它在comments数组本身中创建了另一个注释数组.数组应该在学生中更新

这是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"
    }
]
Run Code Online (Sandbox Code Playgroud)

}

小智 5

您不需要编写这么多代码。请检查以下代码,

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

        }
}
Run Code Online (Sandbox Code Playgroud)


Mat*_*att 5

要避免未经检查的强制转换和linter警告,以及编写自己的循环,请使用libary的get(final Object key, final Class<T> clazz)方法:

List<Document> comments = posts.get("comments", docClazz)
Run Code Online (Sandbox Code Playgroud)

docClazz你创建的东西在哪里:

final static Class<? extends List> docClazz = new ArrayList<Document>().getClass();
Run Code Online (Sandbox Code Playgroud)