首页 > 解决方案 > 如何从带有 mongo_dart 的集合中的数组中仅返回一个对象

问题描述

我目前在下面有这个集合,我需要一种方法来从内部数组中只返回一个项目(来自“pontosGastronomicos”数组的一个项目),只有对象,我对 mongodb 没有太多经验。我正在开发一个应用程序和 AngularDart 以及 Angel 中的后端,应用程序已经在工作,我只需要一种优化的方式来做到这一点。

// 1
{
    "_id": ObjectId("6025b13eab1951094272d007"),
    "id": "03a091e2-fa67-4132-9237-f5b9ed3dbb39",
    "dataCadastro": "2021-02-11 19:35:41.998",
    "link": null,
    "icon": "icon-pizza-1",
    "ativo": true,
    "order": NumberInt("0"),
    ...
    "pontosGastronomicos": [
        {
            "id": "208a3f93-9fcb-4db7-ac44-bb11b86a2d31",
            "infos": [
                {
                    "id": "925ed160-9276-4046-87a2-d1f37547f7cb",
                    "title": "BAR DA BOCA",
                    "lang": "pt",
                    "content": "Ótima experiência com  o  melhor chope de Rio das Ostras"
                },
                {
                    "id": "abc2ef60-4807-4b16-8d3e-95223abe0524",
                    "title": "BAR DA BOCA",
                    "lang": "en",
                    "content": "Great experience with the best draft beer in Rio das Ostras"
                }
            ],
            "midias": [
                {
                    "id": "d54435d2-2fe3-4ea1-aa04-0ccdfade4c9f",
                    "title": "experimente-praias-1",
                    "description": "experimente-praias-1.jpg",
                    "dataCadastro": "2021-02-10 14:23:23.224",
                    "fisicalFilename": "acd13d50-15af-45fd-98f9-0903d1bb3ea5.jpg",
                    "originalFilename": "experimente-praias-1.jpg",
                    "link": "http://localhost:4002/storage/turismo/midias/acd13d50-15af-45fd-98f9-0903d1bb3ea5.jpg",
                    "mimeType": "image/jpeg",
                    "tipo": null
                }
            ],
            "ativo": true,
            "dataCadastro": "2021-02-11 19:51:26.702",
            "order": NumberInt("0"),
            "link": null,
            "email": "bardaboca@yahoo.com.br",
            "logradouro": "Rua Teresópolis",
            "bairro": "Boca da Barra",
            "numero": "69",
            "telefone1": "2227641671 ",
            "telefone2": "22998578550",
            "horarioFuncionamento": "10h as 20h",
            "latitude": null,
            "longitude": null,
            "categoria": null,
            "logo": null,
            "whatsapp": null,
            "tipoDeCozinha": null,
            "capacidade": null,
            "site": null,
            "facebook": null,
            "youtube": null,
            "instagram": null,
            "observacao": null,
            
        }
    ]
}

我需要提出一个请求,通过 id 给我带来一个“pontosGastronomicos”,我目前通过终端执行此操作:

db.pontosGastronomicos.find(
    {"pontosGastronomicos.id": "208a3f93-9fcb-4db7-ac44-bb11b86a2d31"}, 
    {_id: 0, pontosGastronomicos: {$elemMatch: {id: "208a3f93-9fcb-4db7-ac44-bb11b86a2d31"}}});

结果是这样的:

{
    "pontosGastronomicos": [
        {
            "id": "208a3f93-9fcb-4db7-ac44-bb11b86a2d31",
            "infos": [
                {
                    "id": "925ed160-9276-4046-87a2-d1f37547f7cb",
                    "title": "BAR DA BOCA",
                    "lang": "pt",
                    "content": "Ótima experiência com  o  melhor chope de Rio das Ostras"
                },
                {
                    "id": "abc2ef60-4807-4b16-8d3e-95223abe0524",
                    "title": "BAR DA BOCA",
                    "lang": "en",
                    "content": "Great experience with the best draft beer in Rio das Ostras"
                }
            ],
            "midias": [
                {
                    "id": "d54435d2-2fe3-4ea1-aa04-0ccdfade4c9f",
                    "title": "experimente-praias-1",
                    "description": "experimente-praias-1.jpg",
                    "dataCadastro": "2021-02-10 14:23:23.224",
                    "fisicalFilename": "acd13d50-15af-45fd-98f9-0903d1bb3ea5.jpg",
                    "originalFilename": "experimente-praias-1.jpg",
                    "link": "http://localhost:4002/storage/turismo/midias/acd13d50-15af-45fd-98f9-0903d1bb3ea5.jpg",
                    "mimeType": "image/jpeg",
                    "tipo": null
                }
            ],
            "ativo": true,
            "dataCadastro": "2021-02-11 19:51:26.702",
            "order": NumberInt("0"),
            "link": null,
            "email": "bardaboca@yahoo.com.br",
            "logradouro": "Rua Teresópolis",
            "bairro": "Boca da Barra",
            "numero": "69",
            "telefone1": "2227641671 ",
            "telefone2": "22998578550",
            "horarioFuncionamento": "10h as 20h",
            "latitude": null,
            "longitude": null,
            "categoria": null,
            "logo": null,
            "whatsapp": null,
            "tipoDeCozinha": null,
            "capacidade": null,
            "site": null,
            "facebook": null,
            "youtube": null,
            "instagram": null,
            "observacao": null,
            
        }
    ]
}

如何从带有 mongo_dart 的集合中的数组中仅返回一个对象

我希望结果是这样的


        {
            "id": "208a3f93-9fcb-4db7-ac44-bb11b86a2d31",
            "infos": [
                {
                    "id": "925ed160-9276-4046-87a2-d1f37547f7cb",
                    "title": "BAR DA BOCA",
                    "lang": "pt",
                    "content": "Ótima experiência com  o  melhor chope de Rio das Ostras"
                },
                {
                    "id": "abc2ef60-4807-4b16-8d3e-95223abe0524",
                    "title": "BAR DA BOCA",
                    "lang": "en",
                    "content": "Great experience with the best draft beer in Rio das Ostras"
                }
            ],
            "midias": [
                {
                    "id": "d54435d2-2fe3-4ea1-aa04-0ccdfade4c9f",
                    "title": "experimente-praias-1",
                    "description": "experimente-praias-1.jpg",
                    "dataCadastro": "2021-02-10 14:23:23.224",
                    "fisicalFilename": "acd13d50-15af-45fd-98f9-0903d1bb3ea5.jpg",
                    "originalFilename": "experimente-praias-1.jpg",
                    "link": "http://localhost:4002/storage/turismo/midias/acd13d50-15af-45fd-98f9-0903d1bb3ea5.jpg",
                    "mimeType": "image/jpeg",
                    "tipo": null
                }
            ],
            "ativo": true,
            "dataCadastro": "2021-02-11 19:51:26.702",
            "order": NumberInt("0"),
            "link": null,
            "email": "bardaboca@yahoo.com.br",
            "logradouro": "Rua Teresópolis",
            "bairro": "Boca da Barra",
            "numero": "69",
            "telefone1": "2227641671 ",
            "telefone2": "22998578550",
            "horarioFuncionamento": "10h as 20h",
            "latitude": null,
            "longitude": null,
            "categoria": null,
            "logo": null,
            "whatsapp": null,
            "tipoDeCozinha": null,
            "capacidade": null,
            "site": null,
            "facebook": null,
            "youtube": null,
            "instagram": null,
            "observacao": null,
            
        }


标签: mongodbflutterdartnosql

解决方案


我在 github 上得到了答案,对于其他有此困难的人,请遵循答案:

final cursor = db.collection(collection).modernAggregateCursor([
      {
        r'$replaceRoot': {
          'newRoot': {
            r'$arrayElemAt': [
              {
                r'$filter': {
                  'input': r'$pontosGastronomicos',
                  'as': 'pontosGastronomicos',
                  'cond': {
                    /* resolve to a boolean value and determine if an element should be included in the output array. */
                    r'$eq': [r'$$pontosGastronomicos.id', '208a3f93-9fcb-4db7-ac44-bb11b86a2d31']
                  }
                }
              },
              0 /* the element at the specified array index */
            ]
          }
        }
      }
    ]);
    result = await cursor.nextObject();
    await cursor.close();
    /*result = await db.collection(collection).modernFindOne(filter: {
      'pontosGastronomicos': {
        r'$elemMatch': {'id': '208a3f93-9fcb-4db7-ac44-bb11b86a2d31'}
      }
    }, projection: {
      '_id': 0,
      'pontosGastronomicos': {
        r'$elemMatch': {'id': '208a3f93-9fcb-4db7-ac44-bb11b86a2d31'}
      }
    });*/
    print(result);


推荐阅读