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.
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 ] }})
I get the error: https://gyazo.com/d2849406d36364de94d6ea89eff1c2b6
I tried not only such options.
UPD mongo version 3.4.3
"classes"is actually an array, therefore any field path expression like"$content.classes.number"also produces an array of the matching values. Your question only shows a single array member, so it's unclear if you use of an array in the structure is incorrect and it should be singular, or whether you actually mean this to be an array and there can indeed be multiple members.$exprshould be"$expr": { "$anyElementTrue": { "$map": { "input": "$content.classes.number", "in": { "$gt": [ { "$strLenCP": "$$this" }, 16 ] } } } }. Which is also basically the same as"$where": "this.content.classes.some(e => e.number.length > 16)"as the alternate$whereexpression.$exprshould process faster than$wherethough the syntax is more terse."$expr": { "$anyElementTrue": { "$map": { "input": "$content.classes.number", "in": { "$gt": [ { "$strLenCP": "$$this" }, 16 ] } } } }. It came up for version 3.6.9. But it turned out that it is necessary to install on version 3.4.3 and there gyazo.com/8031da7305191d3bdfd02ca87abafdd5