I have a Mongo db structure that looks like this and I would like to retrieve all the entries where blood type length is bigger than 2.
{
"_id" : ObjectId("5dba8987b4a39c13bc104a23"),
"contact" : {
"firstName" : "Mark",
"lastName" : "Doe",
"parentsBloodType" : [
{
"type" : "AB+",
},
{
"type" : "A+",
},
],
},
"createdAt" : ISODate("2019-10-31T07:13:11.278Z"),
"updatedAt" : ISODate("2019-11-26T09:59:41.611Z")
}
{
"_id" : ObjectId("5dba898fdsw54c132c104a23"),
"contact" : {
"firstName" : "Helen",
"lastName" : "Fly",
"parentsBloodType" : [
{
"type" : "O-",
},
{
"type" : "AB-",
},
],
},
"createdAt" : ISODate("2019-10-31T08:13:11.278Z"),
"updatedAt" : ISODate("2019-11-26T09:59:41.611Z")
}
I tried the following query:
db.users.find({"contact.parentsBloodType.type" : {$exists:true}, "$expr": { "$gt": [ { "$strLenCP": "$contact.parentsBloodType.type" }, 2 ] } })
But I got this error:
Error: error: {
"ok" : 0,
"errmsg" : "$strLenCP requires a string argument, found: array",
"code" : 34471,
"codeName" : "Location34471"
}
How can I correctly do this?