我想要从结果中排除一些字段。 我有如下代码:
users = db.select('users');
users.find( {}, { sort: { points:1 }, privateKey:0, publicKey:0}, function(err,data){
res.send(data);
});
我想从结果中排除私钥和公钥。 使用monk可以实现吗?
你也可以像这样做:
users.find( {}, { sort: { points:1 }, fields : { privateKey:0, publicKey:0} },
function(err,data){
res.send(data);
}
);
{ field1: <boolean>, field2: <boolean> ... }
注意:即使未在投影参数中明确声明返回该字段,find()方法始终包括_id字段。 users.find({}, { privateKey: 0, publicKey: 0 }).sort({points: 1}).toArray(
function (err, data) {
res.send(data);
});
对于我来说,我需要使用.project()
方法:
const someFunction = async () => {
const result = await users
.find({}, { sort: { points: 1 })
.project({ privateKey: 0, publicKey: 0});
};
这是对我来说有效的方法,用于排除_id
字段。
const courseChapters = await db
.collection("users")
.find({}, { projection: { _id: 0 } })
.toArray();
所以问题中的示例看起来会像这样。
users.find(
{},
{ projection: { points: 1, privateKey: 0, publicKey: 0 } },
function (err, data) {
res.send(data);
}
);
请查看这个答案,它说根据您的驱动程序,您可能需要使用fields
字段而不是projection
。