Mongo Find() function won't exclude _id

后端 未结 2 1631
Happy的楠姐
Happy的楠姐 2020-12-11 19:44

Hello I can\'t seem to get the exclude _id to work, here is the code

const findLabels = (db, cb) => {
  // Get the documents collection
  const collection         


        
相关标签:
2条回答
  • 2020-12-11 20:13

    I think the correct way to specify a projection is to use the "fields" or "projection" property, depends on the version of your driver.

    collection.find({}, {projection:{ _id: 0 }})
    

    Read documentation here.

    0 讨论(0)
  • 2020-12-11 20:17

    if you use findOne,

    you can try

        const posts : Collection= db.collection('posts')
        let document = await posts.findOne({
                path: path
            }, {
                fields: {
                    _id: 0
            }
        })
    

    In example, I query path(used as _id)

    0 讨论(0)
提交回复
热议问题