2014-09-04 60 views
0

我想將mongoDB選擇器存儲在mongoDB集合中,以便進行動態數據分析。 但是,當我試圖存儲更多複雜的選擇,包括$ -prefixed表達式,如下面的(RAN從流星/ JavaScript的):如何在mongoDB中用流星存儲mongoDB選擇器

filterBy: 
    foo: 
    $ne: "bar" 

我得到這個錯誤:

key $ne must not start with '$' 

有一個正確方式來存儲mongoDB選擇器還是我必須建立一些邏輯來取代和反向替換對象中的所有$

+0

可能重複[如何存儲對象的MongoDB有一個以$開頭的密鑰](http://stackoverflow.com/questions/12739793/how-to-store-an-object-in-mongodb-that-has-a-key-that-starts-with ) – itsmatt 2014-09-04 16:48:03

+0

是的,你是對的。我現在標記了它;我的言辭太不一樣了,但它基本上是一樣的。 – 2014-09-04 16:56:02

回答

0

如上所述,該問題具有用於一般目的的been answered。我將在這裏概述一個流星專用解決方案。

使用Meteor Collection Hooks我加入後保存自動轉換:

FilterCollection.before.insert (userId, doc) -> 
    if doc.filterBy? 
    doc.filterBy = JSON.stringify doc.filterBy 

然後我用一個普通transform將其轉換回:

FilterCollection = new Meteor.Collection "filters", 
    transform: (document) -> 
    if document.filterBy? 
     document.filterBy = JSON.parse document.filterBy