mongoose-lean-id-safe

0.3.0 • Public • Published

mongoose-lean-id

Attach id to the results of mongoose queries when using .lean()

(This fork prevents crash when _id is missing. Also using for loop for optimization.)

Build Status

Usage

const mongooseLeanId = require('mongoose-lean-id');

examples

It attaches .id to result of find, findOne, and findOneAndUpdate if lean

 
    const schema = new mongoose.Schema({
      name: String
    });
 
    schema.plugin(mongooseLeanId);
 
    const Model = mongoose.model('Test', schema);
 
    return Model.create({ name: 'test' }).
      then(() => Promise.all([
        Model.find().lean(),
        Model.findOne().lean(),
        Model.findOneAndUpdate({}, { name: 'test' }).lean()
      ])).
      then(results => {
        const [findRes, findOneRes, findOneAndUpdateRes] = results;
        assert.equal(findRes[0].id, findRes[0]._id.toHexString());
        assert.equal(findOneRes.id, findOneRes._id.toHexString());
        assert.equal(findOneAndUpdateRes.id,
          findOneAndUpdateRes._id.toHexString());
      });
  

/mongoose-lean-id-safe/

    Package Sidebar

    Install

    npm i mongoose-lean-id-safe

    Weekly Downloads

    89

    Version

    0.3.0

    License

    Apache 2.0

    Unpacked Size

    36.8 kB

    Total Files

    10

    Last publish

    Collaborators

    • qdouble