How to get the _id of the last added document?


#1

I want to add a product in my Product database and open the product for sell after a certain time. Thus i’m using synced-cron package to update an attribute of the document to be published to the client.

ProductList.insert({
      name: productName,
      price: 0,
      winner: "Something",
      time: time,
      expire:time+60,
      started: false
  });
    SyncedCron.add({
        name: 'sample Scheduled job for a product',
        schedule: function(parser) {
            return   parser.cron(bidStartTime);

        },
        job: function() {
            ProductList.update( {name: productName},
                {$set :{ started:true} });
        }
    });

Few things to note here is that I want the synced-cron job name should contain the id of the added documents.Otherwise the cron job won’t be added , it’ll just overwrite the existing one. how do I get the Mongodb _id of this added document so that i can incorporate it with the cron job name.


#2

ProductList.insert(...) returns the _id of the inserted document :wink:


#3

in other words do

var latestId = ProductList.insert({ ...