MongoDB Java驱动程序:未显示未定义的值 [英] MongoDB Java driver: Undefined values are not shown
问题描述
打开mongo shell,并使用未定义的值创建文档:
Open mongo shell and create a document with a undefined value:
> 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时,javascript会将未定义"值(存储在数据库中)转换为空"值.但是,在db中,该值仍然是"undefined",正如我们将在Java中看到的那样.
As we can see, javascript translates the "undefined" value (stored in the db) to a "null" value, when showing it to the user. But, in the db, the value is still "undefined", as we are going to see with java.
让我们创建一个"bug_undefined_java_mongo.java"文件,其内容如下:
Let's create a "bug_undefined_java_mongo.java" file, with the following content:
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);
}
}
编译并运行它后,我们得到以下信息:
After compiling and running it, we get this:
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"对已经消失了! (键和值).
We see that the "a_null:null" pair is shown, but... the "an_undefined:undefined" pair has disappeared! (both the key and the value).
为什么?是虫子吗?
谢谢
推荐答案
由于Java中没有等效的映射,因此Java驱动程序目前不支持undefined
.
Currently undefined
is not supported by the java driver as there is no equivalent mapping in java.
其他驱动程序(例如pymongo和js shell)在表示数据时通过将undefined
强制转换为None
来不同地处理此问题,但是它是单独的数据类型,在
Other drivers such as pymongo and the js shell handles this differently by casting undefined
to None
when representing the data, however it is a separate datatype and is deprecated in the bson spec.
如果您需要在Java驱动程序中使用它,则必须编写自己的解码器工厂,然后按如下所示进行设置:
If you need it in the java driver then you will have to code your own decoder factory and then set it like so:
collection.setDBDecoderFactory(MyDecoder.FACTORY);
A minimal example that has defined handling for undefined
and factory is available on github in the horn of mongo repo.
这篇关于MongoDB Java驱动程序:未显示未定义的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!