2015-11-11 60 views
0

我們正在實施在我們的應用程序中進行搜索。我們必須顯示所有公開的項目或用戶所關注的所有私人項目。模式如下。使用and's和or的查找ObjectId的Mongoose

var ItemSchema = new Schema({ 
    . 
    . 
    . 
    followers: { 
     type: Array 
    } 
} 

追隨者被用戶的對象ID填充。
而我試圖找到它像這樣:

Item.find() 
    .and([ 
    { name: new RegExp(searchText, 'i') }, 
    { 
     $or: [ 
     { private: false }, 
     { 
      $and: [ 
      { private: true }, 
      { followers: user._id } 
      ] 
     } 
     ] 
    } 
    ]) 
    .exec(function(err, item) { 
    if(err) { 
     console.log(err); 
    } 
    done(null, user, Item); 
    } 

回答

0

您可以使用Query#or傭工$and一起。

Item.find() 
    .or([ 
      { "private": true }, 
      { $and: [{ private: true },{ followers: user._id } 
      ]} 
    ]) 
    .exec(function(err, item) { 
     ... 
     } 
相關問題