How to select document by id with Sails-mongo?

后端 未结 3 853
谎友^
谎友^ 2021-01-22 22:09
User.find({ _id: { \'!\': user.id } }, function foundFriends (err, friends) {
    if(err) return next(err);
        res.view({
            friends: friends
        });
}         


        
3条回答
  •  时光说笑
    2021-01-22 22:19

    Hi you can try this for find, update, or destroy by id with sails-mongo via select:

     //Schema
      module.exports = {  
       autoPK : false,
         attributes : {
           id : {
             type: 'string',
             primaryKey: true
           },
           name : {
             type : 'string'
           },
           email : {
             type : 'string'
           }
        }
     }
    
    
    
     // Find
     User.find({
       select : {id : user.id}
     }).exec((err,record)=> { 
       if(err) return console.log(err,"err");
       console.log(record,"record");
     })
    
     // Update
     User.update({
       select : {id : user.id}
     },{ 
      name : "Foo"
     }).exec((err,record)=> { 
       if(err) return console.log(err,"err");
       console.log(record,"record");
     })
    
     // Destroy
     User.destroy({
       select : {id : user.id}
     }).exec((err,record)=> { 
       if(err) return console.log(err,"err");
       console.log(record,"record");
     })
    

    Hope this can help to you.

提交回复
热议问题