我有一些MongoDB文档,比如:
{
"group": "P32666",
"order": [{
"_id": {
"$oid": "5e8e9b40e7999f6b90fd88bf"
},
"name": "Dmitriy A",
"login": "example",
"password": "example",
"email": "example",
"level": "user",
"uuid": "b6a19744-bb20-4d39-9e1e-0ca5b464f890"
}, {
"_id": {
"$oid": "5e8ea03f5a21c26b90983de4"
},
"name": "Dmitriy B",
"login": "example",
"password": "example",
"email": "example",
"level": "user",
"uuid": "556924c3-605c-44cc-8a26-d32f58222e89"
}, {
"_id": {
"$oid": "5e8ea0645a21c26b90983de5"
},
"name": "Dmitriy C",
"login": "example",
"password": "example",
"email": "example",
"level": "user",
"uuid": "aef00707-ef00-4ce9-918b-5cef17e7280b"
}]}
我在Mongoose中使用Mongo,无法理解如何查询所有文档(如上所述(,其中字段(数组("order"中至少有一个对象,例如,"login"与"example"的关系。我该怎么做?
我试过这样的东西:
export async function getQueues(request: Request, response: Response) {
const returningQueues = await queues.find({order: [login: request.params.login]});
response.json(returningQueues);
但是TypeScript错误(56个"User"类型的属性缺失(大部分是Mongoose内部的(,我将其存储在一个数组中(表明我的想法是错误的。
如果您在数组字段上只有一个条件,您可以简单地执行:
queues.find({"order.login": request.params.login})
但如果你有多种情况,你可以使用$elemMatch:
queues.find({
order: { $elemMatch: { login: request.params.login, name: request.params.name } }
})