[英]String field value length in array in 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
}
and I want to get a sample in the content.classes.number
, where there are more than 16 characters in the field. 我想在
content.classes.number
获取一个示例,该字段中的字符数超过16个。
when making a request I was guided by String field value length in mongoDB 在发出请求时,我在mongoDB中受String字段值长度的引导
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 ] }})
I get the error: https://gyazo.com/d2849406d36364de94d6ea89eff1c2b6 我收到错误: https : //gyazo.com/d2849406d36364de94d6ea89eff1c2b6
I tried not only such options. 我不仅尝试了这样的选择。
UPD mongo version 3.4.3 UPD mongo版本3.4.3
content.classes.number
is an array field not a string and that's why your query could not work. content.classes.number
是一个数组字段而不是字符串,这就是你的查询无法工作的原因。
One solution here is to use $arrayElemAt
operator but don't know which element you want to check with. 这里的一个解决方案是使用
$arrayElemAt
运算符,但不知道要检查哪个元素。 But for instance, I am taking the 0th
element here. 但是,例如,我在这里采取第
0th
元素。
db.Basis.find({
"content.classes.number": { "$exists": true },
"$expr": { "$gt": [{ "$strLenCP": { "$arrayElemAt": ["$content.classes.number", 0] }}, 16 ] }
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.