mongodb-如何从此查询中隐​​藏_id?

时间:2018-10-07 09:02:55

标签: javascript node.js mongodb mongoose

如何使用此查询隐藏_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。

2 个答案:

答案 0 :(得分: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);
    });
});

答案 1 :(得分:1)

主要有两种方法:

  1. 直接从mongo使用排除功能
  2. 对结果使用.map()函数

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

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

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);
        });
    });

因此,地图功能如下:

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
}

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