Mongoose使用.select()方法

Mongoose use of .select() method

本文关键字:方法 select 使用 Mongoose      更新时间:2023-09-26

我对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()方法用于指定要在查询结果中包含或排除的字段。您可以使用括号中的字符串值来指定要使用的标记,在字符串值中使用-运算符可以用于排除,而在字符串中使用+运算符可以用于强制包含所述查询字符串我相信这是猫鼬的方法。