Mongoose use of .select() method

前端 未结 9 1630
孤城傲影
孤城傲影 2020-12-14 14:16

I\'m pretty confused with the use of the select method. This is how I use it, and it\'s wrong:

Transaction.find({username : user.username}).sele         


        
相关标签:
9条回答
  • 2020-12-14 14:56

    Now there is a shorter way of doing this (not using .select and not using an array), just passing the fields separate by spaces as the second argument

    User.find({}, 'first last', function (err, usr) {
        //Got the result, saved a few bytes of code
    });
    

    The Docs

    0 讨论(0)
  • 2020-12-14 14:59

    This was pretty helpful: How to protect the password field in Mongoose/MongoDB so it won't return in a query when I populate collections?

    Looks like you have a few options.

    1) Use select('-_id'). 2) Use find({whatever: values}, '-_id', callback...}. I can't verify this method, but if it works with select(), I don't see why it wouldn't work here.

    0 讨论(0)
  • 2020-12-14 15:02

    this is another way: queries in mongoose

    Transaction.find({username : user.username})
    .select('uniqueId confirmation_link item_name timeout username')
    .exec(function(err, txs) {
            console.log(txs);
    });
    
    0 讨论(0)
提交回复
热议问题