2016-04-03 51 views
0

我設定座標地理位置陣列:

Crowds.insert({ 
    location: {"type": "MultiPoint","coordinates": 
     [[1, 1]] 
    } 
}); 
Crowds._ensureIndex({ location: "2dsphere" }); 

然後我嘗試添加值。要做到這一點我做一個§push在增加新的價值「座標」陣:

Crowds.update(
    { _id: crowd[0]._id }, 
    { $push: { location: { "coordinates": [ 2, 2 ] 
    }}}  
); 

我得到folling錯誤:

Exception in Mongo write: TypeError: object is not a function 

看來,我不更新座標陣列的正確方法......我嘗試了各種組合,但無法找到如何嵌套數組中添加值...

請幫助;)感謝

回答

0

是否有錯字嗎?

crowd[0]._id 

「人羣」在那裏是奇異的,但你打電話給[0]就好像它是一個數組。

它應該是:

crowds[0]._id 
+0

crowd [0] ._ id是一個簡單的變量,我不認爲它會導致錯誤,因爲沒有$推沒有錯誤。 – Krem

+0

@Krem,請給出架構示例,它對我有用。 –

0

我想我找到了問題,實際上我沒有問這個問題很好...對不起

下面的代碼返回錯誤:

Crowds.update(
    { _id: '123' }, 
    { $inc: { people: 1 } }, 
    { $push: { "location.coordinates": [ Meteor.user().profile.location.coords.longitude, Meteor.user().profile.location.coords.latitude ], 
    { $set: { modified: new Date().valueOf() } }, 
      function(error){ 
      return "Update error: " + error; 
     }      
); 

但它的工作原理:

Crowds.update(
    { _id: '123' }, 
    { $inc: { people: 1 } }, 
    { $set: { modified: new Date().valueOf() } }, 
    function(error){ 
     return "Update error: " + error; 
    }     
); 

Crowds.update(
    { _id: crowd[0]._id }, 
    { $push: { "location.coordinates": [ Meteor.user().profile.location.coords.longitude, Meteor.user().profile.location.coords.latitude ] 
    }}, 
    function(error){ 
     return "Update error: " + error; 
    }     
); 

看來推$需要單獨使用,告訴我,如果我在診斷錯誤;)