3

Been searching for an answer to, what I think, is a common question.

I'm using Mongoose and Step.js (for code control).

I'm iterating through a couple records, creating individual models and hoping to return the created IDs. This is for a RESTful service I'm implementing to bulk insert records. I didn't include all the parts that Step is doing.

Code looks roughly like this:

var group = this.group();

for (var i in records) {
    model.create(records[i], group());
}

The group function is Step, waiting for all the records to save.

The problem is once each record is saved, I'm hoping that Mongoose will return the _id associated with each create. It's only returning a result of the record itself without the proper _id value.

I've attempted creating the ObjectId and saving the model, but that failed and isn't recommended by a lot of people in a number of forums.

Thanks.

ddibiase
  • 1,412
  • 1
  • 20
  • 44
  • You're saying the document sent to group method doesn't hold the _id? – Guy Korland Feb 02 '13 at 08:50
  • What's the code handling the step look like? – WiredPrairie Feb 02 '13 at 12:47
  • Yes Guy, that's the problem. @WiredPrarie: It has three steps: 1) setup the Mongoose model, b) add attributes to the model based on my records array, c) retrieve the results as an array of objects. I don't think Step is the problem, and if it that's the least of my concerns, my major concern is why Mongoose isn't returning the id itself. I've attempted using a basic callback without step and it continues to return the same results. – ddibiase Feb 02 '13 at 12:59
  • 1
    The second parameter to the `model.create` callback is the created doc that includes the `_id` field that was assigned. – JohnnyHK Feb 02 '13 at 14:39
  • 1
    Thanks Johnny, I'm aware that it's *supposed* to be returned. In the above example I am certainly getting a response with the full saved record, except it doesn't actually have the _id. Everything else is working, the record is saved to the db. – ddibiase Feb 02 '13 at 16:30
  • 1
    Well that's pretty odd. Can you post more of your code? Have you updated Mongoose to the latest? – JohnnyHK Feb 02 '13 at 17:14
  • 2
    This is the related post created after from your question but you can refer that if you still have problem http://stackoverflow.com/questions/6854431/how-do-i-get-the-objectid-after-i-save-an-object-in-mongoose – Hüseyin BABAL Dec 25 '13 at 08:12

0 Answers0