MongoDB Java驱动程序:未显示未定义的值

时间:2013-06-20 12:44:05

标签: java json mongodb bson

打开mongo shell并创建一个具有未定义值的文档:

> mongo
MongoDB shell version: 2.4.0
connecting to: test
> use mydb
switched to db mydb
> db.mycol.insert( {a_number:1, a_string:"hi world", a_null:null, an_undefined:undefined} );
> db.mycol.findOne();
{
        "_id" : ObjectId("51c2f28a7aa5079cf24e3999"),
        "a_number" : 1,
        "a_string" : "hi world",
        "a_null" : null,
        "an_undefined" : null
}

正如我们所看到的,当向用户显示时,javascript将“未定义”值(存储在数据库中)转换为“null”值。但是,在db中,值仍然是“未定义的”,正如我们将要看到的那样。

让我们创建一个“bug_undefined_java_mongo.java”文件,其中包含以下内容:

import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.MongoClient;


public class bug_undefined_java_mongo
{

    String serv_n = "myserver";     // server name
    String db_n   = "mydb";         // database name
    String col_n  = "mycol";        // collection name


    public static void main(String[] args)
    {        
        new bug_undefined_java_mongo().start();        
    }


    public void start()
    {

        pr("Connecting to server ...");      
        MongoClient cli = null;
        try
        {
            cli = new MongoClient( serv_n );           
        }
        catch (Exception e)
        {
            pr("Can't connecto to server: " + e);
            System.exit(1);
        }
        if (cli == null)
        {
            pr("Can't connect to server");
            System.exit(1);
        }


        pr("Selecting db ...");
        DB db_res = cli.getDB( db_n );


        pr("Selecting collection ...");      
        DBCollection col = db_res.getCollection( col_n );


        pr("Searching documents ...");
        DBCursor cursor = null;
        try
        {
            cursor = col.find( );
        }
        catch (Exception e)
        {
            pr("Can't search for documents: " + e);
            System.exit(1);
        }


        pr("Printing documents ...");
        try
        {
            while (cursor.hasNext())
            {
                Object doc_obj = cursor.next();                
                System.out.println("doc: " + doc_obj);
            }
        }
        catch (Exception e)
        {
            pr("Can't browse documents: " + e);
            return;
        }
        finally
        {
            pr("Closing cursor ...");
            cursor.close();
        }

    }


    public void pr(String cad)
    {
        System.out.println(cad);
    }    

}

编译并运行后,我们得到了这个:

Connecting to server ...
Selecting db ...
Selecting collection ...
Searching documents ...
Printing documents ...
doc: { "_id" : { "$oid" : "51c2f0f85353d3425fcb5a14"} , "a_number" : 1.0 , "a_string" : "hi world" , "a_null" :  null }
Closing cursor ...

我们看到显示“a_null:null”对,但是......“an_undefined:undefined”对已经消失了! (关键和价值)。

为什么呢?这是一个错误吗?

谢谢

2 个答案:

答案 0 :(得分:2)

目前,java驱动程序不支持undefined,因为java中没有等效的映射。

其他驱动程序(如pymongo和js shell)在表示数据时通过将undefined强制转换为None来处理这种情况,但它是一个单独的数据类型,并且在bson spec中已弃用。

如果你在java驱动程序中需要它,那么你必须编写自己的解码器工厂代码然后设置它:

collection.setDBDecoderFactory(MyDecoder.FACTORY);

horn of mongo repo的github上提供了为undefined和工厂定义处理的最小示例。

答案 1 :(得分:0)

我知道,创建工厂可能是一个解决方案。

无论如何,可能很多开发人员会发现在驱动程序中启用映射以自动转换的可能性很有用" undefined"值为" null"值。例如,通过调用mapUndefToNull()方法:

cli = new MongoClient( myserver );
cli.mapUndefToNull(true);

在我的情况下,我在我的集​​合上运行MapReduce(它是Javascript代码),并且我必须显式地将未定义的值(在访问不存在的键时生成)转换为null,以便避免Java驱动程序删除它:

try { value = this[ key ] }  catch(e) {value = null}
if (typeof value == "undefined")  value = null;      // avoid Java driver to remove it

因此,作为建议,我希望将mapUndefToNull()方法添加到Java驱动程序中。如果可能的话。

谢谢