Search code examples
mongodbmeteorfindmongodb-queryminimongo

Meteor Mongo find does not work for _id: 0


I have a template named qa and inside it I am using a helper named question, which will get the text field of my document, based on the _id property.

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

This works fine for all _id values unless I use _id: 0, then nothing is returned. I run db.questions.find({_id: 0}) on the minimongo console, and it does return a document.

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

Can I not use _id: 0 in collection.find() for Meteor?

Clarification: I do not want to return all fields except _id, I want to find/select based on the _id value. But it does not work if the _id is 0.


Solution

  • I think you want to avoid falsy _id values. Meteor replaces a falsy _id with a random number to prevent it ever going to the database. The comments on the code explain it:

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