4

I'm new to mongodb and I am using jongo.

I am trying to map a Bson array to Java ArrayList.

Is there a simple way to do that?

my 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    
...
}

My Bson object looks like -

{username: 'Joe',password: '123456' ,email: 'Joe@example.com', friends : ['Adam','Ben', 'Josh']}

Trying to build an ArrayList from "friends".

The array list I get from jongo contains nothing

Jonny C
  • 1,943
  • 3
  • 20
  • 36
yairbr
  • 69
  • 1
  • 5

1 Answers1

0

Not sure what version of Jongo you are using, but this seems to be working against the current master (1.3-SNAPSHOT). Here is a working JUnit test case for your pojo:

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;
    }
}
Christian Trimble
  • 2,126
  • 16
  • 27