【问题标题】:Get Object(s) from MongoDB wihtout _id in node.js [duplicate]在node.js中从没有_id的MongoDB获取对象)[重复]
【发布时间】:2020-01-31 18:00:14
【问题描述】:

我的目标是在列表中呈现对象的值。为此,我想从我从数据库获得的对象中排除 _id。 我得到了具有这些功能的集合:

function getCollection(collectionName) {
  if (db === null) {
    throw new Error("Database is not initialized. Call initDB first");
  }
  return db.collection(collectionName);
}

这就是我如何尝试排除 _id:

const { getCollection } = require("./database");

function getAttendeeCollection() {
  return getCollection("attendees").find({}, { _id: 0 });
}

这就是我在 MongoDB (Link) 的文档页面和 stackoverflow (Link) 上的这个问题中找到它的方式

我显然做错了什么,因为它在我的情况下不起作用。这就是对象的样子:

[
    {
        "_id": "5e2efb0e06c1c35b619f6a9f",
        "name": "Test",
        "firstName": "Vorname",
        "address": "Musterstraße",
        "location": "Musterort, 12345",
        "email": "test@mail.de",
        "phone": "12345677898",
        "highschool": "test",
        "gender": "test",
        "nutrition": "test",
        "else": "test"
    },
    {
        "_id": "5e2efbc51c9d44000092fc4e",
        "name": "Test",
        "firstName": "Vorname",
        "address": "Musterstraße",
        "location": "Musterort, 12345",
        "email": "test@mail.de",
        "phone": "12345677898",
        "highschool": "test",
        "gender": "test",
        "nutrition": "test",
        "else": "test"
    }, ...
]

也许有人对我有提示? 提前致谢!

【问题讨论】:

  • 感谢您的快速回答。 getCollection("attendees").find().select('-_id') 导致TypeError: collection.find(...).select is not a function
  • 您正在查看 shell 文档。请改用node.js driver documentation
  • 哦,是的,伙计!非常感谢:)

标签: node.js mongodb


【解决方案1】:

如果我没记错的话,试试这样的:

getCollection("attendees").find().select('-_id')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-19
    • 1970-01-01
    • 2015-11-09
    • 1970-01-01
    • 2019-07-28
    • 2012-06-09
    • 2021-04-15
    • 2013-06-25
    相关资源
    最近更新 更多