4

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

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

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

我的pojo-

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',电子邮件:'Joe@example.com',朋友:['Adam','Ben','Josh']}

试图从“朋友”构建一个 ArrayList。

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

4

1 回答 1

0

不确定您使用的是什么版本的 Jongo,但这似乎与当前的 master (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", "Joe@example.com", "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("Joe@example.com"));
        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;
    }
}
于 2015-11-09T21:18:26.443 回答