首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >基于$type的mongodb聚合$type

基于$type的mongodb聚合$type
EN

Stack Overflow用户
提问于 2020-01-07 19:11:41
回答 2查看 658关注 0票数 2

我有一个收藏品:

代码语言:javascript
复制
{
  values: [null, null, 1, 2, 3, 4.6],
}

我想要接收一个属性,它告诉我这些值中是否有一个是数字。

我试过:

代码语言:javascript
复制
{
    $project: {
        hasNumber: {
            $in: [{ $eq: [{ $type: '$$CURRENT' }, 'number'] }, '$values'],
        },
    },
}

但是它不起作用,这样的事情有可能是聚合的吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-01-07 19:37:42

请试试这个:

代码语言:javascript
复制
db.yourCollectionName.aggregate([{
    $project: {
        values: 1, hasNumber: {
            $gt: [{
                $size: {
                    $filter:
                    {
                        input: "$values",
                        as: "value",
                        cond: { $eq: [{ $type: '$$value' }, 'int'] }
                        // To check & include double as well, replace above cond with this :
                       //cond: { $or :[ {$eq: [{ $type: '$$value' }, 'int']} , {$eq: [{ $type: '$$value' }, 'double']}] }
                    }
                }
            }, 0]
        }
    }
}])

收集数据:

代码语言:javascript
复制
/* 1 */
{
    "_id" : ObjectId("5e14d9dd627ef78236ea77e3"),
    "values" : [ 
        null, 
        null, 
        1, 
        2, 
        3, 
        4.6
    ]
}

/* 2 */
{
    "_id" : ObjectId("5e14d9e4627ef78236ea785f"),
    "values" : [ 
        null, 
        null
    ]
}

/* 3 */
{
    "_id" : ObjectId("5e14decc627ef78236eb12d3"),
    "values" : [ 
        "1", 
        4.6
    ]
}

结果:

代码语言:javascript
复制
/* 1 */
{
    "_id" : ObjectId("5e14d9dd627ef78236ea77e3"),
        "values" : [
            null,
            null,
            1,
            2,
            3,
            4.6
        ],
            "hasNumber" : true
}

/* 2 */
{
    "_id" : ObjectId("5e14d9e4627ef78236ea785f"),
        "values" : [
            null,
            null
        ],
            "hasNumber" : false
}
/* 3 */ // If we're checking for double this hasNumber will be true
{
    "_id" : ObjectId("5e14decc627ef78236eb12d3"),
        "values" : [
            "1",
            4.6
        ],
            "hasNumber" : false
}
票数 3
EN

Stack Overflow用户

发布于 2020-01-07 19:37:28

调试你的代码。

代码语言:javascript
复制
$in: [{ $eq: [{ $type: '$$CURRENT' }, 'number'] }, '$values'],

您正在检查false是否在$values中。

解释:

  • '$$CURRENT‘返回原始文档
  • { $type: '$$CURRENT' }返回'object'
  • $eq:['object', 'number']将始终返回false
  • $in:[ 'false', '$values']将是错误的

我用$convert算子解决了

代码语言:javascript
复制
db.collection.aggregate([
  {
    $project: {
      hasNumber: {
        $cond: [
          {
            $eq: [
              {
                $map: {
                  input: "$values",
                  in: {
                    $convert: {
                      input: "$$this",
                      to: "int",
                      onError: -999,
                      onNull: -999
                    }
                  }
                }
              },
              "$values"
            ]
          },
          true,
          false
        ]
      }
    }
  }
])    
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59634664

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档