Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java MongoDB获取子列表_Java_Mongodb_Api_Jersey - Fatal编程技术网

Java MongoDB获取子列表

Java MongoDB获取子列表,java,mongodb,api,jersey,Java,Mongodb,Api,Jersey,我正在开发一个API来获取BSON中的职位列表,如下所示 { "_id" : ObjectId("59512a4ca33bc80248fb1435"), "id" : NumberLong(1), "locationId" : NumberLong(17), "position" : [ { "latitude" : 12342.0, "longitude" : 1232342.0, "time" : "on May 04 09:51:5

我正在开发一个API来获取BSON中的职位列表,如下所示

{
"_id" : ObjectId("59512a4ca33bc80248fb1435"),
"id" : NumberLong(1),
"locationId" : NumberLong(17),
"position" : [ 
    {
        "latitude" : 12342.0,
        "longitude" : 1232342.0,
        "time" : "on May 04 09:51:52 CDT 2009"
    }, 
    {
        "latitude" : 12342.0,
        "longitude" : 1232342.0,
        "time" : "on May 04 09:51:52 CDT 2009"
    }
    ]
}
在我的Employee类中,我有一个名为
setPosition(List positions)
的方法,可以通过使用'emp.get(“positions”)从BSON获取位置数组来将列表传递给此函数吗

下面是getAllEmployees()函数,我无法通过将BSOn转换为列表来设置位置

public List<Employee> getAllEmployees() {
    DBObject query = new BasicDBObject();
    DBCursor cursor = employeeCollection.find(query);
    System.out.println("cursor.count : " + cursor.count());
    List<Employee> list = new ArrayList<Employee>();
    while (cursor.hasNext()) { 
             DBObject emp = cursor.next();
             Employee employee = new Employee();
             employee.setId((long) emp.get("id"));
             employee.setLocationId((long) emp.get("locationId"));
             DBObject pos = (DBObject) emp.get("position");
             System.out.println("pos : " +pos);
             List<Position> positions = *HERE COMES THE PROBLEM!!!!!*
             employee.setPosition(positions);
             list.add(employee);
    }
    return list;
}
public List getAllEmployees(){
DBObject query=new BasicDBObject();
DBCursor=employeeCollection.find(查询);
System.out.println(“cursor.count:+cursor.count());
列表=新的ArrayList();
while(cursor.hasNext()){
DBObject emp=cursor.next();
员工=新员工();
employee.setId((长)emp.get(“id”));
employee.setLocationId((长)emp.get(“locationId”);
DBObject pos=(DBObject)emp.get(“位置”);
系统输出打印项次(“pos:+pos”);
列表位置=*问题来了*
员工。设置职位(职位);
列表。添加(员工);
}
退货清单;
}

有没有关于如何将positions数组从mongodb直接传递到setPosition函数的建议?

最后我终于找到了这个方法。下面是
getAllEmployees()
函数。解决方案是使用
BasicDBList
而不是
BDObject

public List<Employee> getAllEmployees() {
    DBObject query = new BasicDBObject();
    DBCursor cursor = employeeCollection.find(query);
    System.out.println("cursor.count : " + cursor.count());
    List<Employee> list = new ArrayList<Employee>();
    while (cursor.hasNext()) { 
             DBObject emp = cursor.next();
             Employee employee = new Employee();
             employee.setId((long) emp.get("id"));
             employee.setLocationId((long) emp.get("locationId"));
             BasicDBList positions = (BasicDBList) emp.get("position");
             for (Object position : positions) {
                Position pos = new Position();
                pos.setLatitude((double) ((DBObject) position).get("latitude"));
                pos.setLongitude((double) ((DBObject) position).get("longitude"));
                pos.setTime((String) ((DBObject) position).get("time"));
                employee.getPosition().add(pos);
            }
             list.add(employee);
    }
    return list;
}
public List getAllEmployees(){
DBObject query=new BasicDBObject();
DBCursor=employeeCollection.find(查询);
System.out.println(“cursor.count:+cursor.count());
列表=新的ArrayList();
while(cursor.hasNext()){
DBObject emp=cursor.next();
员工=新员工();
employee.setId((长)emp.get(“id”));
employee.setLocationId((长)emp.get(“locationId”);
基本列表位置=(基本列表)emp.get(“位置”);
用于(对象位置:位置){
位置pos=新位置();
pos.setLatitude((双)((DBObject)位置).get(“latitude”);
pos.setLongitude((double)((DBObject)位置)。获取(“经度”);
pos.setTime((字符串)((DBObject)位置).get(“时间”);
employee.getPosition().add(pos);
}
列表。添加(员工);
}
退货清单;
}

我认为您从未尝试从游标中返回的
DBObject
中检索位置数据。根本没有像
emp.get(“position”)
这样的调用,这是实际获取数据所需要的。由于某种原因,当数据已经在
emp
对象中时,您正在调用
PositionService
。数据已经在那里可以使用,但您没有使用它。只需从光标中获取现有对象的列表。嗨,尼尔,我正在尝试这样做<代码>emp.get(“positions”)返回一个BSON对象,对吗?如果我将其强制转换为DBObject并分配给名为
pos
的变量,那么如何将其添加到接受
列表的
employee.setPosition(positions)
类型参数中,我真想知道为什么要这样做。驱动程序支持使用“类型化”查询,即席将BSON数据封送到定义的类中或从中封送出去。所以你似乎想在这里重新发明轮子。我认为您应该进一步查看文档。至于“在这种情况下如何做”,那么只有1。获取列表。2.迭代列表并
.get()
属性,将它们提供给类,然后附加到类的列表中。3.获取
职位列表
,并将其添加到
员工
。不需要另一个查询。