gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 09:16:49 25 4
gpt4 key购买 nike

我是 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: 'Joe',password: '123456' ,email: 'Joe@example.com', friends : ['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", "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;
}
}

关于java - 有没有一种简单的方法可以使用 mongo 和 jongo 将 Bson 数组映射到 Java ArrayList?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30053800/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com