簡體   English   中英

mongodb - 如何從此查詢中隱​​藏 _id?

[英]mongodb - How to hide _id from this query ?

如何從此查詢中隱​​藏 _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。

您可以嘗試使用投影( 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. 直接從 mongo 使用排除
  2. 對結果使用.map()函數

Mongo Exclusion與您所做的類似,但您需要正確聲明變量,如果集合是動態的(例如每個文檔中的“預設”、“過去”等更改),這可能會很痛苦。

您需要以嵌套屬性方式使用 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);
    });
});

但是,由於文檔的現在、過去等分配,這可能需要大量重復。 現在讓我們嘗試:

在響應之前使用 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
}

將 Moods 保存在每個集合的數組中將簡化迭代,例如不在.map(...)函數上使用if (mood === 'verbName'){...}測試

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM