我是mongodb的新手,正在使用jongo。

我正在尝试将Bson数组映射到Java ArrayList。

有没有简单的方法可以做到这一点?

我的宝贝

public class Member {

@Id
String _id;
String username;
String password;
String email;

ArrayList<String> friends;

public Member() {
    friends = new ArrayList<String>();
}

public Member(String username, String password, String email) {
    this.username = username;
    this.password = password;
    this.email    = email;
    friends       = new ArrayList<String>();
}

some methods
...
}

我的Bson对象看起来像-

{用户名:'Joe',密码:'123456',电子邮件:'[email protected]', friend :['Adam','Ben','Josh']}

试图从“ friend ”建立一个ArrayList。

我从jongo获得的数组列表不包含任何内容

最佳答案

不知道您使用的是哪个版本的Jongo,但这似乎与当前的版本(1.3-SNAPSHOT)兼容。这是适用于您的pojo的JUnit测试用例:

import java.util.ArrayList;
import java.util.Arrays;

import org.bson.types.ObjectId;
import org.jongo.MongoCollection;
import org.jongo.marshall.jackson.oid.MongoId;
import org.jongo.util.JongoEmbeddedRule;
import org.jongo.util.MongoEmbeddedRule;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;


public class ArrayListTest {
    public static @ClassRule MongoEmbeddedRule mongoRule = new MongoEmbeddedRule();
    public @Rule JongoEmbeddedRule jongoRule = new JongoEmbeddedRule(mongoRule);

    MongoCollection collection;

    @Before
    public void before() {
        collection = jongoRule.getJongo().getCollection("members");
    }

    @Test
    public void shouldPopulateArrayList() {
        Member joe = member("Joe", "123456", "[email protected]", "Adam", "Ben", "Josh");

        int count = collection.save(joe).getN();
        assertThat("Joe was saved", count, equalTo(1));

        Member joeInMongo = collection
                .findOne("{_id: #}", joe._id)
                .as(Member.class);

        assertThat(joeInMongo.username, equalTo("Joe"));
        assertThat(joeInMongo.password, equalTo("123456"));
        assertThat(joeInMongo.email, equalTo("[email protected]"));
        assertThat(joeInMongo.friends, equalTo(Arrays.asList("Adam", "Ben", "Josh")));
    }

    public static class Member {

        @MongoId
        String _id;
        String username;
        String password;
        String email;

        ArrayList<String> friends;

        public Member() {
            friends = new ArrayList<String>();
        }

        public Member(String username, String password, String email) {
            this.username = username;
            this.password = password;
            this.email    = email;
            friends       = new ArrayList<String>();
        }
    }

    public static Member member( String username, String password, String email, String... friends ) {
        Member member = new Member(username, password, email);
        member._id = new ObjectId().toString();
        for( String friend : friends ) {
            member.friends.add(friend);
        }
        return member;
    }
}

08-18 13:41