Mongoose使用.select()方法
Mongoose use of .select() method
我对select
方法的使用非常困惑。这就是我使用它的方式,它是错误的:
Transaction.find({username : user.username}).select('uniqueId', 'confirmation_link', 'item_name', 'timeout', 'username', function(err, txs){
callback(txs);
});
我试图实现的只是从数据库中的事务中选择具有该用户名的事务,我只想去掉select
方法中列出的字段。有人能指出我应该如何使用select
方法吗?谢谢
文档说你可以这样实现:
Mongoose v4.0
// Retrieving only certain fields
Model.find({}, 'first last', function (err, docs) {
});
陈旧过时的API
// Retrieving only certain fields
Model.find({}, ['first', 'last'], function (err, docs) {
// docs is an array of partially-`init`d documents
// defaults are still applied and will be "populated"
});
所以您可以在没有select()
的情况下完成此操作。
这是另一种方式:在mongoose 中查询
Transaction.find({username : user.username})
.select('uniqueId confirmation_link item_name timeout username')
.exec(function(err, txs) {
console.log(txs);
});
现在有一种更短的方法(不使用.select
,也不使用数组),只需将用空格分隔的字段作为第二个参数
User.find({}, 'first last', function (err, usr) {
//Got the result, saved a few bytes of code
});
文档
Select方法用于选择查询结果中要返回的字段,不包括Select意味着我们希望返回所有其他字段,这里是根据文档的简单用法。
// include a and b, exclude other fields
query.select('a b');
// exclude c and d, include other fields
query.select('-c -d');
此处提供更多信息,https://mongoosejs.com/docs/api.html#query_Query-选择
要在不检索"_id"的情况下检索某些字段,可以指定将其排除在之外
Model.find({}, {'Username':1, '_id':0}, function ( err, docs ){}.....
这很有帮助:如何保护Mongoose/MongoDB中的密码字段,使其获胜';填充集合时不会在查询中返回?
看来你有几个选择。
1) 使用select('-_id')
。2) 使用find({whatever: values}, '-_id', callback...}
。我无法验证这个方法,但如果它与select()
一起工作,我不明白为什么它在这里不工作。
要仅检索特定字段,请使用以下
Model.find({/*Your query*/},
{'firstName':1, 'lastname':1, '_id':0}, //Notice, this will omit _id!
function ( err, docs ){}.....
这将起作用,并且不会带来任何额外的id,例如_id。
选择&投影操作可以在nodejs中以这种方式轻松完成。试试这个
var Selection={
<some key of data model > : <target value for that key field>,
<some key of data model > : <target value for that key field>
//you can add many parameters here selection operation
};
var Projection = {
__v : false,
_id : false
//you can add many parameters here for projection
};
<DataModel>.find(Selection,Projection,function (err,data) {
if(err){
console.log(err);
}else{
console.log(data);
}
});
此语法也适用于
Transaction.find({username : user.username}).select(['uniqueId', 'confirmation_link', 'item_name', 'timeout', 'username']);
删除要选择的字段之间的逗号和引号:
Transaction.find({username : user.username}).select('uniqueId confirmation_link item_name timeout username', function(err, txs){
callback(txs);
});
为要在Schema
服务器请求中排除的字段设置select: false
。
示例
const userSchema = mongoose.Schema({
name: {
type: String,
select: false
},
password: {
type: Number,
select: false
}
})
这样,你想要进入的字段应该是没有select: false
的字段
如果出于某种原因,您需要获得密码(任何字段),但需要在模式中包含select: false
。使用select('+password etc')
.select()方法用于指定要在查询结果中包含或排除的字段。您可以使用括号中的字符串值来指定要使用的标记,在字符串值中使用-运算符可以用于排除,而在字符串中使用+运算符可以用于强制包含所述查询字符串我相信这是猫鼬的方法。
- select方法在Firefox和Chrome中失败
- 为什么方法.options对select无效
- select()方法中的函数
- 为什么dijit.form.Select有方法setStore(),而dijit.fform.FilteringSele
- 从jQuery select访问插件方法
- 有没有一种方法可以在Chrome中的Select Option元素上添加事件
- jQuery扩展方法,将select元素序列化为字典
- 如何基于URL对聚合物元素执行select()方法
- Mongoose使用.select()方法
- OpenCart 1.5.6.4处理checkout.tpl上的点击事件(带有输入select的新发货方法)
- focus()或select()方法javascript,在Chrome中性能缓慢
- Angular的ng-change for select不调用声明的方法
- 不确定使用Spring加载SELECT对象的正确方法
- 按select值分组众多变量的更简单的方法
- IE7 + Prototype -添加SELECT方法,后面跟着Element.addMethods() = no SE
- JavaScript的onchange事件won't触发,如果select选项从其他方法改变
- 有没有一种简单的方法可以知道select的起始值
- Ajax,方法load()用于select字段中的选项值
- 使用jQuery change方法从Javascript对象和html select元素更新内容
- Range.select() 等效方法,适用于非 IE 浏览器