[英]How to query all documents where array field have at least one object-element with property equal 'X'?
I have some MongoDB documents like that:我有一些像这样的 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"
}]}
I'm working with Mongo in Mongoose and can't understand how to query all documents(like above) where field(array) "order" has at least one object within where field, for example, "login" is queal to "example".我在 Mongoose 中使用 Mongo 并且无法理解如何查询字段(数组)“订单”在其中字段中至少有一个 object 的所有文档(如上),例如,“登录”对“示例”。 How can I do this?
我怎样才能做到这一点?
I tried something like this:我试过这样的事情:
export async function getQueues(request: Request, response: Response) {
const returningQueues = await queues.find({order: [login: request.params.login]});
response.json(returningQueues);
But TypeScript error (56 missing properties(mostly internal Mongoose's) of type "User", which I store within an array) says that's I am wrong in my thoughts.但是 TypeScript 错误(56 个“用户”类型的缺失属性(主要是内部猫鼬),我存储在一个数组中)说我的想法是错误的。
If you just have one condition on the array field, you can simply do:如果您在数组字段上只有一个条件,您可以简单地执行以下操作:
queues.find({"order.login": request.params.login})
But if you have multiple conditions, you can use $elemMatch :但是如果你有多个条件,你可以使用$elemMatch :
queues.find({
order: { $elemMatch: { login: request.params.login, name: request.params.name } }
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.