2016-04-03 52 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; 
    }     
); 

看来推$需要单独使用,告诉我,如果我在诊断错误;)