Second parameter to limit fields in find() not working in Mongodb

前端 未结 3 739
遇见更好的自我
遇见更好的自我 2020-12-20 06:42

Mongodb Database view

I want to omit the _id field in my result

MongoClient.connect(\"mongodb://localhost:27017/\",function(err,client)         


        
3条回答
  •  轮回少年
    2020-12-20 07:41

    In version 3.0 of the mongodb driver, the second parameter to find() is the options object, not the projection document. See the documentation here. To send a projection document, set the projection property of the options document. E.g.

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

    Alternatively, use the project method:

    db.collection("customerName").find({}).project({ _id: 0 })
    

    In version 2.2 of the mongodb driver, the second argument of find was indeed the projection document. This has changed in the latest version (3.0). So some blog posts might not be up to date. See the relevant section of the 3.0 changelog here.

提交回复
热议问题