YouKnowMe
YouKnowMe
  • 发布:2023-04-07 22:02
  • 更新:2023-04-10 11:15
  • 阅读:193

【报Bug】数组类型字段的元素有重复的时候联表查询数据缺失,急!急!急!

分类:uniCloud

产品分类: uniCloud/App

示例代码:
// test.schema.json  
{  
    "bsonType": "object",  
    "required": [],  
    "permission": {  
        "read": true,  
        "create": false,  
        "update": false,  
        "delete": false  
    },  
    "properties": {  
        "_id": {  
            "description": "ID,系统自动生成"  
        },  
        "other_table_id": {  
            "bsonType": "array",  
            "description": "业务员站点",  
            "title": "业务员站点",  
            "foreignKey": "test2._id"  
        }  
    }  
}  

// test2.schema.json  
{  
    "bsonType": "object",  
    "required": [],  
    "permission": {  
        "read": false,  
        "create": false,  
        "update": false,  
        "delete": false  
    },  
    "properties": {  
        "_id": {  
            "description": "ID,系统自动生成"  
        },  
        "name": {  
            "bsonType": "string"  
        }  
    }  
}  

// test 数据  
{  
    "_id": {  
        "$oid": "64301e2828064a03b7bb13ff"  
    },  
    "other_table_id": [{  
        "$oid": "64301dec28064a03b7bb0358"  
    }, {  
        "$oid": "64301dec28064a03b7bb0358"  
    }]  
}  

// test2 数据  
{  
    "_id": {  
        "$oid": "64301dec28064a03b7bb0358"  
    },  
    "name": "i am test2"  
}  

操作步骤:
// JQL查询  
const t = db.collection('test').field('other_table_id').getTemp()  
const t2 = db.collection('test2').field('_id, name as text').getTemp()  
db.collection(t, t2).get()  

预期结果:
// 期望的结果  
[  
    {  
        "_id": "64301e2828064a03b7bb13ff",  
        "other_table_id": [  
            {  
                "_id": "64301dec28064a03b7bb0358",  
                "text": "i am test2"  
            },  
            {  
                "_id": "64301dec28064a03b7bb0358",  
                "text": "i am test2"  
            }  
        ]  
    }  
]  

实际结果:
// 实际返回的结果  
[  
    {  
        "_id": "64301e2828064a03b7bb13ff",  
        "other_table_id": [  
            {  
                "_id": "64301dec28064a03b7bb0358",  
                "text": "i am test2"  
            }  
        ]  
    }  
]  

bug描述:

test表中有1个字段other_table_id是关联到test2表的,当test中的other_table_id数组中有重复元素的时候,联表查询返回的数据少了一条

// test.schema.json  
{  
    "bsonType": "object",  
    "required": [],  
    "permission": {  
        "read": true,  
        "create": false,  
        "update": false,  
        "delete": false  
    },  
    "properties": {  
        "_id": {  
            "description": "ID,系统自动生成"  
        },  
        "other_table_id": {  
            "bsonType": "array",  
            "description": "业务员站点",  
            "title": "业务员站点",  
            "foreignKey": "test2._id"  
        }  
    }  
}  

// test2.schema.json  
{  
    "bsonType": "object",  
    "required": [],  
    "permission": {  
        "read": false,  
        "create": false,  
        "update": false,  
        "delete": false  
    },  
    "properties": {  
        "_id": {  
            "description": "ID,系统自动生成"  
        },  
        "name": {  
            "bsonType": "string"  
        }  
    }  
}  

// test 数据  
{  
    "_id": {  
        "$oid": "64301e2828064a03b7bb13ff"  
    },  
    "other_table_id": [{  
        "$oid": "64301dec28064a03b7bb0358"  
    }, {  
        "$oid": "64301dec28064a03b7bb0358"  
    }]  
}  

// test2 数据  
{  
    "_id": {  
        "$oid": "64301dec28064a03b7bb0358"  
    },  
    "name": "i am test2"  
}  

// JQL查询  
const t = db.collection('test').field('other_table_id').getTemp()  
const t2 = db.collection('test2').field('_id, name as text').getTemp()  
db.collection(t, t2).get()  

// 返回的结果  
[  
    {  
        "_id": "64301e2828064a03b7bb13ff",  
        "other_table_id": [  
            {  
                "_id": "64301dec28064a03b7bb0358",  
                "text": "i am test2"  
            }  
        ]  
    }  
]  

期望的返回结果:other_table_id中有2个元素才对

急!急!急!

客户的数据查询出错,发现都是这种情况下的问题,这是我模拟的数据

2023-04-07 22:02 负责人:无 分享
已邀请:
DCloud_uniCloud_WYQ

DCloud_uniCloud_WYQ

目前设计如此,我们看下需不需要调整

  • YouKnowMe (作者)

    这么设计明显是有问题的,字段用数组类型存储数据,那么元素在数组中的顺序可能是有重要含义的,用联表查询的主要目的也是翻译和关联一些副表信息,不应该自动过滤重复和随意改变数组的元素顺序,除非有明确的指令提示

    例如:field1: [ A, B C, A, C] , field2: [ U1, U2, U3, U4, U5] , field1和field2的元素顺序可能是有关系的,一 一 对应的

    2023-04-10 15:06

  • DCloud_uniCloud_WYQ

    回复 YouKnowMe: 那如果C在副表没有呢?并且主表对副表是可能一对多的,这个字段可以理解成只是主表这个字段对应的所有数据库记录,而不是每一项对应的数据库记录。

    2023-04-10 19:46

要回复问题请先登录注册