couchDB中按文档类型排序的视图
我的couchDB中有不同种类的文档,例如:couchDB中按文档类型排序的视图,couchdb,Couchdb,我的couchDB中有不同种类的文档,例如: { "_id": "c9f3ebc1-78f4-4dd1-8fc2-ab96f804287c", "_rev": "7-1e8fcc048237366e24869dadc9ba54f1", "to_customer": false, "box_type": { "id": 9, "name": "ZF3330" }, "erp_creation_date": "16/12/2017",
{
"_id": "c9f3ebc1-78f4-4dd1-8fc2-ab96f804287c",
"_rev": "7-1e8fcc048237366e24869dadc9ba54f1",
"to_customer": false,
"box_type": {
"id": 9,
"name": "ZF3330"
},
"erp_creation_date": "16/12/2017",
"type": "pallet",
"plantation": {
"id": 62,
"name": "FRF"
},
"pallet_type": {
"id": 2565,
"name": "ZF15324"
},
"creation_date": "16/12/2017",
"article_id": 3,
"updated": "2017/12/16 19:01",
"server_status": {
"in_server": true,
"errors": null,
"modified_in_server": false,
"dirty": false,
"delete_in_server": false
},
"pallet_article": {
"id": 11,
"name": "BLUE"
}
}
因此,在我所有的文档中,我都有一个字段:type
。另一方面,我有一个视图,可以获取类型为托盘| |装运
我的看法是:
function(doc) {
if (doc.completed == true && (doc.type == "shipment" || doc.type == "pallet" )){
emit([doc.type, doc.device_num, doc.num], doc);
}
}
所以在这个视图中,我总是得到一个带有视图查询结果的列表,我的问题是列表是按接收日期排序的(我猜),我需要按文档类型排序。
因此,我的问题是:如何按
document.type在视图中排序文档?视图结果总是按键排序,因此您的视图是按doc.type排序的:首先您将获得所有托盘,然后是所有装运。托盘按device_num
排序,然后按num
排序。如果您使用相同的键发出多行,则这些行将按_id排序。您可以在中找到更详细的信息
因此,您的视图实际上应该按照您希望的方式工作。;-) 非常感谢@Bernhard在other中执行此操作,我指的是第一批货,然后是palets,我必须做什么?@JoCuTo您可以将自定义值作为键的第一个元素发出(emit([doc.type=='shipping'?'0-shipping':doc.type,
),也可以在查询时通过将descending=true
附加到视图url来颠倒完整视图结果的顺序。