這裏是我的模型:我如何使用它的集合屬性(acts_as_tree)使用mongoid和mongo控制檯查詢mongo文檔?
class Person
acts_as_tree
end
我涉及多個對象爲一樹:
P1
|
---
| |
P1.1 P1.2
|
---
| |
P1.1.1 P1.1.2
這裏,如果我需要中檢索P1.1.1我需要編寫一個有效詢問查詢:
get me the Person with name P1.1.1 and path (given by acts_as_tree) [P1, P1.1].
僅僅通過名字查詢是不夠的,因爲我可以在多個路徑上具有相似的命名人員。
我該怎麼做?
> db.people.find({name: 'P1.1.1'})
上面的代碼片段會正確顯示路徑屬性,但我無法通過該路徑進行查詢。
> db.people.find({name: 'P1.1.1', path: [{name: 'P1'}, {name: 'P1.1'}]})
不起作用。同樣沒有:
> db.people.find({name: 'P1.1.1', path: [db.people.find({name: 'P1'}),
db.people.find({name: 'P1.1'})]})
但解釋了什麼是我想要做的事。
使用$ all的另一個問題是Mongo將返回結果,即使該集合的一個子集與您查詢的內容相匹配。爲了說清楚:在上面的例子中,如果路徑將是世界/家鄉/城鎮,那麼如果用家鄉/城鎮路徑搜索,太多$都會返回結果。但另一個帶有自己問題的選項看起來值得一試。我會回來的調查結果。 – vinayvinay