2014-11-13 7 views
8

मैं किसी विशेष लेखक और उसकी संबंधित पुस्तकों को ढूंढने और आउटपुट करने के लिए मीन-पर्यावरण में निम्न मोंगोस क्वेरी का उपयोग करता हूं।मोंगोस/मोंगोडब: जनसंख्या वाले क्वेरी डेटा से फ़ील्ड बहिष्कृत करें

Author 
.findOne({personcode: code}) 
.select('-_id') 
.select('-__v') 
.populate('bookids') //referencing to book documents in another collection (->array of bookids) 
.select('-_id') //this doens't affect the data coming from the bookids-documents 
.select('-__v') //this doens't affect the data coming from the bookids-documents 
.exec(function (err, data) { 
    //foo 
}); 

मैं भी "_ id" और बाहरी दस्तावेजों से आ रही आबादी वाले डेटा से "__v" फील्ड छोड़ करना चाहते हैं। यह कैसे प्राप्त किया जा सकता है?

उत्तर

22

populate का दूसरा पैरामीटर, एक क्षेत्र चयन स्ट्रिंग है ताकि आप के रूप में यह कर सकते हैं:

Author 
    .findOne({personcode: code}) 
    .select('-_id -__v') 
    .populate('bookids', '-_id -__v') 
    .exec(function (err, data) { 
    //foo 
}); 

ध्यान दें कि आप एक ही स्ट्रिंग में अपने क्षेत्र चयन संयोजित करने होंगे।

+0

महान! बहुत बहुत धन्यवाद। बस ठीक काम करता है =) –

0

धन्यवाद JohnnyHK, और वस्तु पैरामीटर के लिए यह काम करता है:

Entity.populate({ 
    path: 'bookids', 

    // some other properties 
    match: { 
     active: true 
    }, 
    // some other properties 

    select: '-_id -__v' // <-- this is the way 
}).then(...) // etc 
संबंधित मुद्दे