2016-09-08 50 views
0

Foo是我的MongoDBcollection。 在那裏,我只有一個document流星。 MongoDB upsert錯誤:TypeError:keypath.split不是函數。 (in'keypath.split('。')','keypath.split'is undefined)

{ 
    0: {code: "basic", caption: "basic", points: 100}, 
    1: {code: "gold", caption: "gold", points: 200}, 
    2: {code: "platinum", caption: "platinum", points: 300}, 
    3: {code: "diamond", caption: "diamond", points: 400}, 
    id: "PnpbhFi8m7NqZXRr6" 
} 

當我試圖upsert它,我收到以下錯誤:

TypeError: keypath.split is not a function. (In 'keypath.split('.')', 'keypath.split' is undefined) 

這是我的代碼:

const data = [ 
    { 
    "code": "basic", 
    "caption": "basic", 
    "points": 100 
    }, 
    { 
    "code": "gold", 
    "caption": "gold", 
    "points": 200 
    }, 
    { 
    "code": "platinum", 
    "caption": "platinum", 
    "points": 300 
    }, 
    { 
    "code": "diamond", 
    "caption": "diamond", 
    "points": 400 
    } 
]; 

const doc = Foo.findOne(); 
Foo.upsert(doc._id, { $set: data }); 

哪裏是我的錯誤?

+0

是您的意圖有'data'是對象的數組或數字鍵嵌套對象? –

回答

1

您使用$set錯誤。你必須通過對象$set

Foo.update(doc._id, { 
    $set: { 
    4: data[0], 
    5: data[1], 
    6: data[2], 
    7: data[3], 
    } 
}); 

你也不必使用upsert。從meteor docsupsert

Modify one or more documents in the collection, or insert one if no matching documents were found

但你的文件存在,所以沒有必要upsert,使用update

+0

謝謝!你幫了我很多 – Edgar