杨杨杨
杨杨杨
  • 发布:2023-05-09 11:51
  • 更新:2023-05-09 15:08
  • 阅读:165

【报Bug】lookup中 当字段为数组时结果不正确

分类:uniCloud

产品分类: uniCloud/App

操作步骤:

const dbCmd = db.command
const $ = dbCmd.aggregate
db.collection('card-dadaili').aggregate().lookup({
from: 'uni-id-users',
let: {
uid: '$uid',
},
pipeline: $.pipeline()
.match(dbCmd.expr($.and([
$.eq(['$fpath', '$$uid']),
]))).group({
_id: null,
len: $.sum(1)
})
.done(),
as: 'son_len',
}).end()

预期结果:

数组结构也可以查到结果

实际结果:

查询类型为字符串有结果 为数组无结果

bug描述:


const dbCmd = db.command  
const $ = dbCmd.aggregate  
db.collection('card-dadaili').aggregate().lookup({  
  from: 'uni-id-users',  
  let: {  
    uid: '$uid',  
  },  
  pipeline: $.pipeline()  
    .match(dbCmd.expr($.and([  
      $.eq(['$fpath', '$$uid']),  
    ]))).group({  
            _id: null,  
            len: $.sum(1)  
        })  
    .done(),  
  as: 'son_len',  
}).end()

uni-id-users 中{"fid": "63e4d216e1a35c8785885853", "fpath": [
"631704e3b3fd93000137ae00",
"63aa867f53a7f30001a5d139",
"63e33db9f43e606dd86da900",
"63e4c172f43e606dd8b56e79",
"63e4caa128064aae263f75c4",
"63e4d17d09e2987ceedc9fa7",
"63e4d216e1a35c8785885853"
]}

结果 "son_len": [ ]
如果 使用 $.eq(['$fid', '$$uid']), 则结果正确 是查询数组还有其他关键字吗?

2023-05-09 11:51 负责人:DCloud_uniCloud_WYQ 分享
已邀请:
DCloud_uniCloud_WYQ

DCloud_uniCloud_WYQ

应该使用$in而不是$eq

  • 杨杨杨 (作者)

    使用const dbCmd = db.command

    const $ = dbCmd.aggregate

    db.collection('card-dadaili').aggregate().lookup({

    from: 'uni-id-users',

    let: {

    uid: '$uid',

    },

    pipeline: $.pipeline()

    .match(dbCmd.expr($.and([

    $.eq(['$fid', '$$uid']),

    ]))).project({

    fid: true,

    fpath: true,

    })

    .done(),

    as: 'son_len',

    }).end()

    可以得到结果

    [ {

    "_id": "64575ee0819ce8deeeaffd0f",

    "choucheng": 1,

    "son_len": [

    {

    "_id": "645870490c801ca8789360d9",

    "fid": "6450adbd28064a75874e24e7",

    "fpath": [

    "62600f60b76df10001c900bc",

    "6450adbd28064a75874e24e7"

    ]

    },

    {

    "_id": "6458a830e1a35c371bc07321",

    "fid": "6450adbd28064a75874e24e7",

    "fpath": [

    "62600f60b76df10001c900bc",

    "6450adbd28064a75874e24e7"

    ]

    },

    {

    "_id": "6459be5a09e298919803cb50",

    "fid": "6450adbd28064a75874e24e7",

    "fpath": [

    "62600f60b76df10001c900bc",

    "6450adbd28064a75874e24e7"

    ]

    }

    ],

    "uid": "6450adbd28064a75874e24e7"

    },

    {

    "_id": "64575f24819ce8deeeb018e5",

    "choucheng": 1,

    "son_len": [

    {

    "_id": "6458941828064a7587d97d7d",

    "fid": "64531129f5cf3a2df4e19148",

    "fpath": [

    "62600f60b76df10001c900bc",

    "64531129f5cf3a2df4e19148"

    ]

    }

    ],

    "uid": "64531129f5cf3a2df4e19148"

    }

    ]


    请问该怎么改写使用fpath得到相同结果

    2023-05-09 14:24

杨杨杨

杨杨杨 (作者)

const dbCmd = db.command  
const $ = dbCmd.aggregate  
db.collection('card-dadaili').aggregate().lookup({  
  from: 'uni-id-users',  
  let: {  
    uid: '$uid',  
  },  
  pipeline: $.pipeline()  
    .match(dbCmd.expr($.and([  
      $.in(['$fpath', ['$$uid']]),  
    ]))).group({  
            _id: null,  
            len: $.sum(1)  
        })  
    .done(),  
  as: 'son_len',  
}).end()

这种写法不对
如果不加[] 会报错

杨杨杨

杨杨杨 (作者)

const dbCmd = db.command  
const $ = dbCmd.aggregate  
db.collection('card-dadaili').aggregate().lookup({  
  from: 'uni-id-users',  
  let: {  
    uid: '$uid',  
  },  
  pipeline: $.pipeline()  
    .match(dbCmd.expr($.and([  
      $.in(['$fpath', '$$uid']),  
    ]))).group({  
            _id: null,  
            len: $.sum(1)  
        })  
    .done(),  
  as: 'son_len',  
}).end()

这样写会报错 麻烦指导一下 尝试了许多写法都不能得到正确结果

杨杨杨

杨杨杨 (作者)

实在是怎么改写都得不到正确的结果 帮着改写一下吧

const dbCmd = db.command    
const $ = dbCmd.aggregate    
db.collection('card-dadaili').aggregate().lookup({    
  from: 'uni-id-users',    
  let: {    
    uid: '$uid',    
  },    
  pipeline: $.pipeline()    
    .match(dbCmd.expr($.and([    
      $.in(['$fpath', '$$uid']),    
    ]))).group({    
            _id: null,    
            len: $.sum(1)    
        })    
    .done(),    
  as: 'son_len',    
}).end()
杨杨杨

杨杨杨 (作者)

$.in(['$$uid', '$fpath']) 这种写法会报错 说第二个参数必须为数组
$.in(['$$uid', ['$fpath']]), 这种定法不报错 但结果为空
还有什么写法啊 我崩溃了

  • DCloud_uniCloud_WYQ

    上面一种是对的,只是你的数据包含fpath不是数组的数据。额外加个判断即可,可以在文档搜一下 $.isArray,$.cond

    2023-05-09 15:46

  • DCloud_uniCloud_WYQ

    比如


    $.cond({  
    if: $.isArray('$fpath'),
    then: $.in(['$$uid', '$fpath']),
    else: false
    })

    2023-05-09 15:58

  • 杨杨杨 (作者)

    回复 DCloud_uniCloud_WYQ: 哦哦哦 原来是因为数据问题 非常非常感谢

    2023-05-09 17:02

要回复问题请先登录注册