简体   繁体   English

如何查询数组字段至少有一个属性等于'X'的对象元素的所有文档?

[英]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.

相关问题 Mongodb:查找所有至少一个数组元素不匹配的文档? - Mongodb: Find all documents where at least one array element does not matches? 获取数组的至少一个元素包含 substring 的文档 - Fetch documents where at least one element of an array contains a substring 如何从mongodb中选择一个字段不等于某个特定值的所有文档 - how to select all documents from mongodb where one field does not equal a certain value 查询对象数组不包含等于字符串值的属性的地方 - Query where array of object does not contain property equal to string value Mongodb:查找具有至少一个元素与ObjectID不匹配的数组的文档 - Mongodb: Find documents with array where at least one element does not match ObjectID Firestore 查询,其中结果有一个字段,该字段包含要传递给第二个查询以检索所有文档的文档引用 - Firestore query where the results have a field that contains a document ref to be passed to a second query to retrieve all the documents 有没有一种方法可以查询 mongoDB/mongoose 中的文档以匹配日期时间大于指定日期的数组属性中的所有对象 - Is there a way to query documents in mongoDB/mongoose to match ALL objects in an array property WHERE the datetime is greater than a specified date 如何从对象数组中的给定键查询包含字段的文档? - How to query the documents that contain field from given key in array of object? 如何根据 object 查询 mongodb 以获取包含一个或多个相等属性的文档(搜索) - how to query mongodb based on object to get documents that contains one or more equal properties (Search) 选择其中一个数组字段中的所有值都存在于另一个数组中的文档 - Select documents where all values in an array field exist in another array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM