Javascript 如何从猫鼬的集合中排除一个特定字段?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14559200/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to exclude one particular field from a collection in Mongoose?
提问by dany
I have a NodeJS application with Mongoose ODM(Mongoose 3.3.1). I want to retrieve all fields except 1 from my collection.For Example: I have a collection Product Which have 6 fields,I want to select all except a field "Image" . I used "exclude" method, but got error.. This was my code.
我有一个带有 Mongoose ODM(Mongoose 3.3.1)的 NodeJS 应用程序。我想从我的集合中检索除 1 之外的所有字段。例如:我有一个集合 Product 其中有 6 个字段,我想选择除字段 "Image" 之外的所有字段。我使用了“ exclude”方法,但出错了。这是我的代码。
var Query = models.Product.find();
Query.exclude('title Image');
if (req.params.id) {
Query.where('_id', req.params.id);
}
Query.exec(function (err, product) {
if (!err) {
return res.send({ 'statusCode': 200, 'statusText': 'OK', 'data': product });
} else {
return res.send(500);
}
});
But this returns error
但这会返回错误
Express
500 TypeError: Object #<Query> has no method 'exclude'.........
Also I tried, var Query = models.Product.find().exclude('title','Image');and var Query = models.Product.find({}).exclude('title','Image');But getting the same error. How to exclude one/(two) particular fields from a collection in Mongoose.
我也试过,var Query = models.Product.find().exclude('title','Image');和var Query = models.Product.find({}).exclude('title','Image');而得到同样的错误。如何从 Mongoose 的集合中排除一个/(两个)特定字段。
回答by JohnnyHK
Use query.selectfor field selection in the current (3.x) Mongoose builds.
使用query.select在当前(3.X)字段选择猫鼬的基础之上。
Prefix a field name you want to exclude with a -; so in your case:
使用-;在要排除的字段名称前加上; 所以在你的情况下:
Query.select('-Image');
Quick aside: in JavaScript, variables starting with a capital letter should be reserved for constructor functions. So consider renaming Queryas queryin your code.
顺便说一句:在 JavaScript 中,应为构造函数保留以大写字母开头的变量。所以考虑重新命名Query为query您的代码。
回答by Philipp
I don't know where you read about that .exclude function, because I can't find it in any documentation.
我不知道你在哪里读到的 .exclude 函数,因为我在任何文档中都找不到它。
But you can exclude fields by using the second parameter of the find method.
但是您可以使用 find 方法的第二个参数排除字段。
Here is an example from the official documentation:
以下是官方文档中的示例:
db.inventory.find( { type: 'food' }, { type:0 } )
This operation returns all documents where the value of the type field is food, but does not include the type field in the output.
此操作返回 type 字段值为 food 的所有文档,但不包括输出中的 type 字段。
回答by Danieldms
Model.findOne({ _id: Your Id}, { password: 0, name: 0 }, function(err, user){
// put your code
});
this code worked in my project. Thanks!! have a nice day.
这段代码在我的项目中有效。谢谢!!祝你今天过得愉快。
回答by Aditya
You could do this
你可以这样做
const products = await Product.find().select(['-image'])
回答by SaiSurya
In the updated version of Mongoose you can use it in this way as below to get selected fields.
在 Mongoose 的更新版本中,您可以按如下方式使用它来获取选定的字段。
user.findById({_id: req.body.id}, 'username phno address').then(response => {
res.status(200).json({
result: true,
details: response
});
}).catch(err => {
res.status(500).json({ result: false });
});
回答by Alfredo Izquierdo
I am use this with async await
我在异步等待中使用它
async (req, res) => {
try {
await User.findById(req.user,'name email',(err, user) => {
if(err || !user){
return res.status(404)
} else {
return res.status(200).json({
user,
});
}
});
} catch (error) {
console.log(error);
}

