2012-02-13 103 views
1

我有一個查詢來返回帖子和人物內容類型。mongodb回覆人如果評論

var query = { 
    type: { 
    $in: ["post", "person"] 
    }, 
} 

但是我只想在個人有任何評論但是帖子總是返回的人。

var query = { 
    type: { 
    $in: ["post", "person"] 
    }, 
    // somehow make this only for person 
    // comments: {"$exists": true} 
} 

這是可能的還是應該使用地圖縮小?

謝謝。

回答

1
var query = { 
    $or: [ 
     { type: "post" }, 
     { type: "person", comments: { $exists: true }}, 
    ] 
} 
1

這應該工作,我只是測試它:

db.things.find({$or: [ { type: "post" }, 
         { type: "person", comments: { $exists: true }} ]}) 

我起初認爲這是不可能在查詢中使用相同的字段名稱兩次,但它只是一個問題與一些司機。 here你可以找到關於這個的討論。

也映射/減少通常不接受實時查詢,因爲它很慢。

+0

感謝您提供關於map/reduce和實時查詢的提示。 – Harry 2012-02-13 13:04:19

+0

@哈里:不客氣。 – 2012-02-13 13:23:18