mongodb:3.6.5
node:9.9.0
koa:2.5.0
npm-mongodb:3.0.5
遇到的问题:
用findOne/find带projection参数查询
在node里 返回结果仍然带有被剔除的字段
router.post('/manage', async(ctx, next) => {
var users = db.collection("user");
var doc = await users.findOne({userName:'yunf'},{_id:0});
var doc1 = await users.find({userName:'yunf'},{_id:0}).toArray();
console.log(doc);
console.log(doc1 );
});
// 返回的结果仍然带有被剔除的字段
// {
// _id: 5ac1cfe24a814acb143e3084,
// userName: 'yunf',
// authority: '00000000010000',
// cerationTime: '',
// updateTime: 2018-04-06T06:18:55.188Z,
// userPwd: '$2a$10$IA7/C9fplPoxY6ilTbadyeBJjAxHRUIgP.ezOQqYMCv7vHXPhx39u'
// }...
但同样的代码 shell正常返回
查过mongodb 3.6的文档find()查询返回的是游标 可以用project指定返回的字段
var cursor3 = db.collection('user').find({}).project({_id: 0 }).toArray();
// { userName: 'yunf'}]
但是为什么用findOne/find带projection参数查询 会不起作用
是版本问题???求大佬指点!!!!!!!!!!!!!!!!
你所使用的
mongodb
是nodejs的一个package
,而不是原生的查询语句,那是package生成的最终的结果。Node.js MongoDB Driver API
文档里写很清楚
find(query, options)
,options
的定义里是没有_id
的,里面有projection
应该就是你想要的。