javascript - mongodb - How to hide _id from this query ? - Stack Overflow

How to hide _id from this query, i use express node.js ? i have this query and i make an API but i want

How to hide _id from this query, i use express node.js ?

i have this query and i make an API but i want hide ths _id.

this is the query :

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

////////////////////////////////////////////////// this is the collection :

[
      {
        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
      }
    ];

I tried to hide the _id , but every time I made mistakes, I wanted to fetch the data but not with Id.

How to hide _id from this query, i use express node.js ?

i have this query and i make an API but i want hide ths _id.

this is the query :

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

////////////////////////////////////////////////// this is the collection :

[
      {
        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
      }
    ];

I tried to hide the _id , but every time I made mistakes, I wanted to fetch the data but not with Id.

Share Improve this question asked Oct 7, 2018 at 9:02 Jaber AlshamiJaber Alshami 4146 silver badges14 bronze badges 1
  • See this helps: stackoverflow./questions/9601219/remove-id-from-mongo-result – Rishabh Agarwal Commented Oct 7, 2018 at 9:59
Add a ment  | 

2 Answers 2

Reset to default 4

You could try using projection (https://docs.mongodb./manual/reference/method/db.collection.find/):

{projection: { _id: 0 }}

As in:

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

There are two main methods to do this:

  1. Directly from mongo using exclusion
  2. Using .map() function on results

Mongo Exclusion Similar to what you've done, but you need to declare the variables correctly, which might be a pain if the collection is dynamic (e.g. "Preset", "Past", etc. change in each document).

You need to use fields option in a nested property manner, simply, change this:

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

To this:

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

However, this might need a lot of repetition due to the document's Present, Past, etc. assignment. Now let's try:

Using Map before response

Right now you have:

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

So, the map function will be as follows:

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

Then the new get method will be this:

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

Just remember to declare the cleanVerbs function and have it on scope (accessible on the same file) when writing this route.

NOTE: I highly suggest changing the Mongo collection schema from:

what you have to:

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

Keeping the Moods inside an array on each collection will simplify iteration, such as not using the if (mood === 'verbName'){...} test on .map(...) function

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745399204a4626022.html

相关推荐

  • javascript - mongodb - How to hide _id from this query ? - Stack Overflow

    How to hide _id from this query, i use express node.js ? i have this query and i make an API but i want

    4小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信