mongoDB中数组中的字符串字段值长度 [英] String field value length in array in mongoDB

查看:588
本文介绍了mongoDB中数组中的字符串字段值长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从嵌套在另一个字段中的字段中获取字符长度?它在一个数组中.例如:

How can I get the length of characters from a field that is nested in another field? and it is in an array. eg:

{
    "_id" : ObjectId("687e1db"),
    "content" : {
        "ods" : "1102223000241",
        "startDate" : ISODate("2017-05-11T12:00:00Z"),
        "classes" : [
            {
              "driveNumber" : "9999078900007091",
              "number" : "00107605829357",
              "sId" : "0000000005009593"
            }
        ],
         "user" : "SoftLogic",
    },
    "level" : 2
}

,我想在content.classes.number中获得一个示例,该字段中的字符超过16个.

and I want to get a sample in the content.classes.number, where there are more than 16 characters in the field.

在发出请求时,我受到 mongoDB中字符串字段值长度的指导

when making a request I was guided by String field value length in mongoDB

此请求不适合这种情况,或者我做错了事.

Either this request is not suitable for this situation, or I am doing something wrong.

db.Basis.find({
    "content.classes.number": { "$exists": true }, 
    "$expr": { "$gt": [ { "$strLenCP": "$content.classes.number" }, 16 ] }})

我收到错误: https://gyazo.com/d2849406d36364de94d6ea89eff1c2b6

我不仅尝试了这些选项.

I tried not only such options.

UPD mongo版本3.4.3

UPD mongo version 3.4.3

推荐答案

content.classes.number是一个数组字段,而不是字符串,这就是为什么您的查询无法工作的原因.

content.classes.number is an array field not a string and that's why your query could not work.

这里的一种解决方案是使用 $arrayElemAt 运算符,但不知道要检查哪个元素.但是例如,我在这里使用0th元素.

One solution here is to use $arrayElemAt operator but don't know which element you want to check with. But for instance, I am taking the 0th element here.

db.Basis.find({
  "content.classes.number": { "$exists": true }, 
  "$expr": { "$gt": [{ "$strLenCP": { "$arrayElemAt": ["$content.classes.number", 0] }}, 16 ] }
})

这篇关于mongoDB中数组中的字符串字段值长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆