Javascript mongodb-如何在此查询中隐藏\u id?

Javascript mongodb-如何在此查询中隐藏\u id?,javascript,node.js,mongodb,mongoose,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); }); }); ///////////////////////////////////////////

如何从这个查询中隐藏_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。

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

  • 直接从mongo使用排除
  • 对结果使用
    .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);
        });
    });
    
    但是,由于文档的当前、过去等分配,这可能需要大量重复。现在让我们试试:

    在响应之前使用Map

    现在你有:

    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
    }
    

    将情绪保持在每个集合的数组中将简化迭代,例如不在
    .map(…)
    函数上使用
    if(情绪==='verbName'){…}
    测试有两种主要方法:

  • 直接从mongo使用排除
  • 对结果使用
    .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);
        });
    });
    
    但是,由于文档的当前、过去等分配,这可能需要大量重复。现在让我们试试:

    在响应之前使用Map

    现在你有:

    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(情绪==='verbName'){…}
    测试
    .map(…)
    函数

    您可以尝试使用投影():

    例如:

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

    您可以尝试使用投影():

    例如:

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