get _id with mongoose

14,059

Solution 1

If you want to customize/edit record then you should use .lean() function.The .lean() function will turn it into a normal JavaScript object. If you don't use .lean() function then each record is still a mongoose document and _id behaves differently in that context. So can use like

MySchemaModel.find({}).lean().exec(function(error, records) {
  records.forEach(function(record) {
    console.log(record._id);
  });
});

N.B: when use .exec() then first parameter used for error and second one for success data.

Solution 2

Mongoose assigns each of your schemas an id virtual getter by default which returns the documents _id field cast to a string, or in the case of ObjectIds, its hexString. If you don't want an id getter added to your schema, you may disable it passing this option at schema construction time.

Source: Mongoose Docs

var schema = new Schema({ name: String }, { id: false });
var Page = mongoose.model('Page', schema);
var p = new Page({ name: 'mongodb.org' });
console.log(p.id); // '50341373e894ad16347efe01'

Solution 3

you can also use the .map() method :

MySchemaModel.find({}).exec(function(records) {
    console.log(records.map(record => record._id);
});

Solution 4

The problem is that each record is still a mongoose document and _id behaves differently in that context. The .lean() function will turn it into a normal JavaScript object.

MySchemaModel.find({}).lean().then(function(records) {
  records.forEach(function(record) {
    console.log(record._id);
  });
});

Solution 5

I guess the issue is with .then promise, I have never seen that.

MySchemaModel.find({}).then

So just try simple .exec call with callback.

MySchemaModel.find({}).exec(function(records) {
    records.forEach(function(record) {
    console.log(record._id);
  });
});
Share:
14,059
Gabriel Kunkel
Author by

Gabriel Kunkel

Updated on June 04, 2022

Comments

  • Gabriel Kunkel
    Gabriel Kunkel almost 2 years

    I was trying to console.log(record._id) all of records on my mongodb collection using Mongoose. I kept getting undefined for each of the _id values.

    I struggled until I bumped into this post. Then I used console.dir to find the location of the _id and used that in my console.log:

    MySchemaModel.find({}).then(function(records) {
    
      records.forEach(function(record) {
    
        console.log(record._doc._id); // <-- I added ._doc
      });
    
    });
    

    But, this looks down-right hacky. Is there a better way to do this?

    NOTE: This isn't just something that affects console.log. I'm just keeping the question narrow.

    • Shaishab Roy
      Shaishab Roy almost 8 years
      what is _doc or how generate this _doc?
    • Gabriel Kunkel
      Gabriel Kunkel almost 8 years
      After using console.dir(record) it showed me that the _id property was located in the _doc property. In part, I'm wondering why.
    • enRaiser
      enRaiser almost 8 years
      did you tried record._id;
  • Gabriel Kunkel
    Gabriel Kunkel almost 8 years
    I still had the same problem when just adding exec function. It wouldn't output the _ids, but once I added the lean function, then it worked for me. Still feels like this should be more straightforward. Weird.
  • Gabriel Kunkel
    Gabriel Kunkel almost 8 years
    I switched out the .then for exec and it turns out using the "promise" syntax is just fine, so this is not the problem. using lean() as shaishab roy mentions is used to strip out any of mongoose's extra stuff, so _id then can get logged.