0

我有一个名为的模板qa,在其中我使用了一个名为 的助手question,它将text根据_id属性获取我的文档的字段。

Template.qa.helpers({
    question: function () {
        return Questions.find({_id: 24}).fetch()[0].text;
    }
});

这对所有_id值都适用,除非我使用_id: 0,然后什么都不返回。我db.questions.find({_id: 0})在 minimongo 控制台上运行,它确实返回了一个文档。

{ "_id" : 0, "author" : 0, "create" : "2014-12-22T13:26:23.038Z", "liked" : [ ], "range" : "", "text" : "I want to get this text", "update" : "2014-12-22T13:26:23.038Z", "version" : 1 }

我不能_id: 0collection.find()流星中使用吗?

澄清:我不想返回除 之外的所有字段_id,我想根据_id值查找/选择。_id但如果为 0则不起作用。

4

1 回答 1

1

我认为您想避免虚假的_id 值。Meteor 用一个随机数替换一个虚假的 _id 以防止它进入数据库。代码上的注释解释了它:

// protect against dangerous selectors.  falsey and {_id: falsey} are both
// likely programmer error, and not what you want, particularly for destructive
// operations.  JS regexps don't serialize over DDP but can be trivially
// replaced by $regex.
于 2014-12-27T11:33:47.933 回答