Insert and retrieval order in couchbase

时间:2019-02-18 00:27:42

标签: json xml sorting couchbase

Is there a way we can maintain the insertion and retrieval order same for json document. eg.

config : {
“x” : {
“p” : 2,
“a” : 3
},
“a” : {
“b” = 23,
“l” = 56
}
}

When I store above doc and try to retrieve the same, the order of elements is getting changed. I want the ordering to remain as is.

I understand its an object and there is no guarantee for the ordering. But my use case is to take this doc and covert to an xml and load on the system. If ordering is not same xml load fails.

Note: I don’t want to push xml in db.

3 个答案:

答案 0 :(得分:1)

如果您使用的是N1QL,则在每个JSON对象中获得的字段将始终按字母顺序生成。这就是序列化代码的工作方式。无法在查询引擎本身中对其进行更改。

答案 1 :(得分:1)

使用Couchbase Java SDK,您可以使用RawJsonDocument像字符串一样访问未经处理的原始文档内容,如下所示:

String docId = ...;
RawJsonDocument rawDoc = bucket.get(RawJsonDocument.create(docId));
String json = rawDoc.content(); // Unprocessed. Parse it however you want.

答案 2 :(得分:0)

这可能取决于您如何将JSON转换为XML。例如,如果涉及到Newtonsoft,则可以使用JsonProperty attribute并指定订单。