【问题标题】:mongodb - How to hide _id from this query ?mongodb - 如何从这个查询中隐藏 _id ?
【发布时间】:2019-03-12 05:19:45
【问题描述】:

如何从这个查询中隐藏 _id,我使用 express node.js ?

我有这个查询,我创建了一个 API,但我想隐藏 _id。

这是查询:

router.get("/", (req, res) => {
  VerbsDE.find({}, { _id: 0 })
    .limit(1)
    .then(verbs => {
      res.send(verbs);
    });
});

/////////////////////////////////////// /// 这是收藏:

[
      {
        Indicative: {
          Present: [
            {
              _id: "5bb9009249efde355376ad29",
              pron: "xxx",
              verb: "xxx xxx xxx"
            },
            {
              _id: "5bb9009249efde355376ad29",
              pron: "xxx",
              verb: "xxx xxx xxx"
            },
            {
              _id: "5bb9009249efde355376ad29",
              pron: "xxx",
              verb: "xxx xxx xxx"
            }
          ],
          Perfect: [
            {
              _id: "5bb9009249efde355376ad29",
              pron: "xxx",
              verb: "xxx xxx xxx"
            },
            {
              _id: "5bb9009249efde355376ad29",
              pron: "xxx",
              verb: "xxx xxx xxx"
            },
            {
              _id: "5bb9009249efde355376ad29",
              pron: "xxx",
              verb: "xxx xxx xxx"
            }
          ],
          Past: [
            {
              _id: "5bb9009249efde355376ad29",
              pron: "xxx",
              verb: "xxx xxx xxx"
            },
            {
              _id: "5bb9009249efde355376ad29",
              pron: "xxx",
              verb: "xxx xxx xxx"
            },
            {
              _id: "5bb9009249efde355376ad29",
              pron: "xxx",
              verb: "xxx xxx xxx"
            }
          ],
          Pluperfect: [
            {
              _id: "5bb9009249efde355376ad29",
              pron: "xxx",
              verb: "xxx xxx xxx"
            },
            {
              _id: "5bb9009249efde355376ad29",
              pron: "xxx",
              verb: "xxx xxx xxx"
            },
            {
              _id: "5bb9009249efde355376ad29",
              pron: "xxx",
              verb: "xxx xxx xxx"
            }
          ],
          Future_I: [
            {
              _id: "5bb9009249efde355376ad29",
              pron: "xxx",
              verb: "xxx xxx xxx"
            },
            {
              _id: "5bb9009249efde355376ad29",
              pron: "xxx",
              verb: "xxx xxx xxx"
            },
            {
              _id: "5bb9009249efde355376ad29",
              pron: "xxx",
              verb: "xxx xxx xxx"
            }
          ],
          Future_II: [
            {
              _id: "5bb9009249efde355376ad29",
              pron: "xxx",
              verb: "xxx xxx xxx"
            },
            {
              _id: "5bb9009249efde355376ad29",
              pron: "xxx",
              verb: "xxx xxx xxx"
            },
            {
              _id: "5bb9009249efde355376ad29",
              pron: "xxx",
              verb: "xxx xxx xxx"
            }
          ]
        },
        Imperative: {
          Worte: [
            {
              _id: "5bb9009249efde355376ad29",
              pron: "xxx",
              verb: "xxx xxx xxx"
            },
            {
              _id: "5bb9009249efde355376ad29",
              pron: "xxx",
              verb: "xxx xxx xxx"
            },
            {
              _id: "5bb9009249efde355376ad29",
              pron: "xxx",
              verb: "xxx xxx xxx"
            }
          ]
        },
        _id: "5bb9009249efde355376ad23",
        verbName: "abbilden",
        __v: 0
      }
    ];

我试图隐藏 _id ,但每次我出错时,我都想获取数据而不是 Id。

【问题讨论】:

标签: javascript node.js mongodb mongoose


【解决方案1】:

有两种主要方法可以做到这一点:

  1. 使用排除直接来自 mongo
  2. 对结果使用 .map() 函数

Mongo 排除 与您所做的类似,但您需要正确声明变量,如果集合是动态的(例如每个文档中的“预设”、“过去”等发生变化),这可能会很痛苦。

您需要以嵌套属性的方式使用 fields 选项,只需更改以下内容:

router.get("/", (req, res) => {
  VerbsDE.find({}, { _id: 0 })
    .limit(1)
    .then(verbs => {
      res.send(verbs);
    });
});

到这里:

router.get("/", (req, res) => {
  VerbsDE.find({}, { _id: 0, __v: 0, 'Indicative.Present._id': 0 })
    .limit(1)
    .then(verbs => {
      res.send(verbs);
    });
});

但是,由于文档的 Present、Past 等 分配,这可能需要大量重复。现在让我们试试:

在响应前使用地图

现在你有:

router.get("/", (req, res) => {
      VerbsDE.find({}, { _id: 0, __v: 0 })
        .limit(1)
        .then(verbs => {
          // We'll use map before sending the response
          res.send(verbs);
        });
    });

所以,map函数如下:

function cleanVerbs(verbs) {
    return verbs.map(doc => {
        // For each doc, make a newDoc
        const newDoc = {};
        for (const mood in doc) {
            // mood will be 'Imperative' 'Indicative', etc.
            if (!newDoc[mood]) {
                // If out newDoc object does not have 'Imperative', etc. property, assign it as object.
                newDoc[mood] = {};
            }
            if (mood === 'verbName') {
                // You have verbName as root property, treat it differently
                newDoc[mood] = doc[mood];
                break; // Avoid further execution on this cycle
            }
            for (const time in doc[mood]) {
                console.log('MOOD & TIME: ', [mood, time]);
                const entries = doc[mood][time];
                const newTimeEntries = entries.map(e => {
                    delete e._id;
                    return e;
                });
                // This will set the newTimeEntries for this Mood's time.
                newDoc[mood][time] = newTimeEntries;
            }
        }
        return newDoc;
    });
}

那么新的get 方法将是这样的:

router.get("/", (req, res) => {
      VerbsDE.find({}, { _id: 0, __v: 0 })
        .limit(1)
        .then(verbs => {
          // We'll use map before sending the response
          res.send(cleanVerbs(verbs));
          // Try res.json(cleanVerbs(verbs)) instead :)
        });
    });

只记得在编写此路由时声明cleanVerbs 函数并将其置于作用域(可在同一文件中访问)。

注意: 我强烈建议从以下位置更改 Mongo 集合架构:

你必须做什么:

{
    _id: "5bb9009249efde355376ad23",
    verbName: "abbilden",
    grammar: [
      Indicative: {
            Present: [...],
            Past: [...],
      },...
    ],
    __v: 0
}

将 Moods 保存在每个集合的数组中将简化迭代,例如不对 .map(...) 函数使用 if (mood === 'verbName'){...} 测试

【讨论】:

    【解决方案2】:

    您可以尝试使用投影 (https://docs.mongodb.com/manual/reference/method/db.collection.find/):

    {projection: { _id: 0 }}
    

    如:

    router.get("/", (req, res) => {
      VerbsDE.find({}, {projection: { _id: 0 }})
        .limit(1)
        .then(verbs => {
          res.send(verbs);
        });
    });
    

    【讨论】:

      猜你喜欢
      • 2023-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-14
      • 1970-01-01
      • 2018-02-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多