在网上挖掘后,我无法弄清楚BSON是否支持匿名对象数组。我想要这个的原因是对于具有以下架构的日志系统
{
ERROR_LOG: [
{
message: "There was an error recording the file",
code: 1133
},
{
message: "There was an error saving the file",
code: 1135
}
],
FILE_LOG: [
{
message: "File 1234.txt edited",
code: 5445
},
{
message: "File transfer success",
code: 2332
}
],
... etc.
}
可能是这种架构设计效率不高。如果不支持匿名对象,我愿意接受不同模式的建议。
答案 0 :(得分:1)
确实如此。
在引擎盖下,文档将获得递增的名称“0”,“1”,...但是bson库将隐藏您的名称。
如果数组中的元素也可以混合使用类型:
{
array: [
{
message: "There was an error recording the file",
code: 1133
},
1234,
"etc."
]
}
编辑:
来自此处的文档:http://api.mongodb.org/c/current/tutorial.html#bson
我认为你应该能够做到这样的事情:
bson doc[1];
bson_init( doc );
bson_append_start_array( b, "ERROR_LOG" );
bson_append_start_object( doc, "0" );
bson_append_string( doc, "message", "There was an error recording the file" );
bson_append_int( doc, "code", 1133 );
bson_append_finish_object( doc );
bson_append_start_object( doc, "1" );
bson_append_string( doc, "message", "There was an error saving the file" );
bson_append_int( doc, "code", 1135 );
bson_append_finish_object( doc );
bson_append_finish_array( b );
bson_append_start_array( b, "FILE_LOG" );
bson_append_start_object( doc, "1" );
bson_append_string( doc, "message", "File 1234.txt edited" );
bson_append_int( doc, "code", 5445 );
bson_append_finish_object( doc );
bson_append_start_object( doc, "1" );
bson_append_string( doc, "message", "File transfer success" );
bson_append_int( doc, "code", 2332 );
bson_append_finish_object( doc );
bson_append_finish_array( b );
bson_finish( doc );
请注意,C驱动程序骗了我,你需要跟踪数组元素的'索引'名称。