有没有一种简单的方法可以使用mongo和jongo将Bson数组映射到Java ArrayList?

时间:2015-05-05 13:02:51

标签: java mongodb arraylist jongo

我是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对象看起来像 -

  

{username:&#39; Joe&#39;,密码:&#39; 123456&#39; ,电子邮件:&#39; Joe@example.com',朋友:[&#39; Adam&#39;,&#39; Ben&#39;&#39; Josh&#39;]}

尝试从&#34; friends&#34;构建一个ArrayList。

我从jongo获得的数组列表中没有任何内容

1 个答案:

答案 0 :(得分:0)

不确定您正在使用的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", "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;
    }
}