javascriptjsonmongodbmongojs

How to remove _id when I print fields using printjson()?


db.events.find().forEach(function(doc)
{
    var iso_date = new Date(doc.starts_at);
    if(iso_date>= new Date() && iso_date<= new Date(new Date().setDate(new 
     Date().getDate()+4))){
        printjson(doc);
    }
})

I am unable to remove _id field using printjson() in MongoDB. I am printing fields based on a particular condition which I have handled using JavaScript. While printing using printjson(), I can't remove _id field. Is there a way to remove _id while printing using printjson()?


Solution

  • Simply use projection to avoid returning _id in db results :

    db.events.find(
      {starts_at: {$gte: Date.now(), $lte: new Date().setDate(4)}},
      {_id:0} 
    )
    .forEach(function(doc)
    {
        printjson(doc);
    });