【问题标题】:Meteor Mongo find does not work for _id: 0Meteor Mongo 查找不适用于 _id: 0
【发布时间】:2014-12-27 06:57:13
【问题描述】:

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

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

这适用于所有_id 值,除非我使用_id: 0,否则不会返回任何内容。我在 minimongo 控制台上运行db.questions.find({_id: 0}),它确实返回了一个文档。

{ "_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 }

我不能在 collection.find() 中将 _id: 0 用于 Meteor 吗?

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

【问题讨论】:

  • 您是否尝试过var intId = parseInt(0); return Questions.find({_id: intId}),以便实际传递一个整数值而不是一个假值?

标签: mongodb meteor find mongodb-query minimongo


【解决方案1】:

我认为您想避免使用 falsy _id 值。 Meteor 用一个随机数替换一个虚假的 _id 以防止它进入数据库。 the code上的cmets解释一下:

// 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.

【讨论】:

    猜你喜欢
    • 2019-03-23
    • 1970-01-01
    • 2014-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-14
    • 1970-01-01
    相关资源
    最近更新 更多