2017-10-04 115 views
0

我對Meteor並不陌生,但因爲在其他項目上工作而離開了幾個星期。Collection.findOne({_ id:「stringidsdfdsfdsfds」})返回undefined

我現在正在使用React處理Meteor項目。

當我這樣做Collection.find({}).fetch()它返回:

[ 
    { 
     "_id": { "_str": "59d3b91d80f4f5eeb0162634" }, 
     "title": "My first Post", 
     "content": "This is the body of the pst" 
    } 
] 

唯一奇怪的是_id場。

但是,當我做Collection.findOne({_id: "59d3b91d80f4f5eeb0162634" })時,它返回undefined

如何使用_id字符串作爲查詢參數來執行.findOne()

回答

2

,你看到的是_id的值不是一個JSON對象,但蒙戈的ObjectID類型的字符串表示,這就是爲什麼你.findOne()沒有找到它。

你應該尋找這樣的:

const _id = new Meteor.Collection.ObjectID('59d3b91d80f4f5eeb0162634'); 
Collection.findOne({ _id }); // same as { _id: _id } 

默認情況下,流星uses STRING method of _id generation,如此看來,這個特殊的文件已經被插入到收集另一種方式。

+0

嗨,謝謝你。我實際上是從「流星蒙哥」控制檯插入的。 – Kenshinman

+0

@Kenshinman我建議你使用'meteor shell'控制檯來做這樣的事情。 – Styx