无法通过部分匹配从 Mongo(使用 mongoose)检索文档
Unable to retrieve a document from Mongo (using mongoose) with a partial match
我在 NodeJS (v16.14.0) 中有一个项目,我想在其中过滤文档列表。大多数属性都是字符串,因此它们很容易访问,但使用 specialty
(obj) 属性我无法获得我想要的信息:
{
"_id": {
"$oid": "--"
},
"name": "string",
"email": "string",
"password": "string",
"phoneNumber": "number",
"city": "string",
"myDescription": "string",
"specialty": {
"certified": ["plomero","electricista"],
"inProgress": ["albañil"],
"nonCertified": ["mecanico","veterinario"]
},
"image": {
"profile": [
"string",
"string"
],
"myJobs": [
"string",
"string"
]
},
"socialSecurity": {
"eps": "string",
"arl": "string"
},
"availability": {
"schedule": "string",
"fullAvailability": false
}
}
我想使用字符串和 return 包含 certified
属性中给定字符串的文档列表,例如:
db.collection.find({ specialty: { certified: { $in: ['plomero'] } } })
// return the document shown above
但我总是一无所获。我试过使用 $in
和 $elemMatch
但没有结果。我只有复制整个对象才能取回文档:
db.collection.find({ specialty: { certified: ['plomero', 'electricista'], inProgress: ['albañil'], nonCertified: ['mecanico', 'veterinario'] } })
我不知道如何继续,我一直在阅读 MongoDB 文档,但找不到与此类似的示例...而且我对 Mongo 很陌生。
感谢您的指导!
使用查找
db.collection.find({
"specialty.certified": "plomero"
})
我在 NodeJS (v16.14.0) 中有一个项目,我想在其中过滤文档列表。大多数属性都是字符串,因此它们很容易访问,但使用 specialty
(obj) 属性我无法获得我想要的信息:
{
"_id": {
"$oid": "--"
},
"name": "string",
"email": "string",
"password": "string",
"phoneNumber": "number",
"city": "string",
"myDescription": "string",
"specialty": {
"certified": ["plomero","electricista"],
"inProgress": ["albañil"],
"nonCertified": ["mecanico","veterinario"]
},
"image": {
"profile": [
"string",
"string"
],
"myJobs": [
"string",
"string"
]
},
"socialSecurity": {
"eps": "string",
"arl": "string"
},
"availability": {
"schedule": "string",
"fullAvailability": false
}
}
我想使用字符串和 return 包含 certified
属性中给定字符串的文档列表,例如:
db.collection.find({ specialty: { certified: { $in: ['plomero'] } } })
// return the document shown above
但我总是一无所获。我试过使用 $in
和 $elemMatch
但没有结果。我只有复制整个对象才能取回文档:
db.collection.find({ specialty: { certified: ['plomero', 'electricista'], inProgress: ['albañil'], nonCertified: ['mecanico', 'veterinario'] } })
我不知道如何继续,我一直在阅读 MongoDB 文档,但找不到与此类似的示例...而且我对 Mongo 很陌生。
感谢您的指导!
使用查找
db.collection.find({
"specialty.certified": "plomero"
})