https://github.com/mongoosejs/mongoose-lean-id
Attach `id` to the results of mongoose queries when using `.lean()`
https://github.com/mongoosejs/mongoose-lean-id
Last synced: 2 months ago
JSON representation
Attach `id` to the results of mongoose queries when using `.lean()`
- Host: GitHub
- URL: https://github.com/mongoosejs/mongoose-lean-id
- Owner: mongoosejs
- License: apache-2.0
- Created: 2017-01-07T23:17:45.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2023-12-01T02:15:44.000Z (over 1 year ago)
- Last Synced: 2025-04-01T16:07:21.895Z (3 months ago)
- Language: JavaScript
- Size: 26.4 KB
- Stars: 25
- Watchers: 3
- Forks: 8
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# mongoose-lean-id
Attach `id` to the results of mongoose queries when using `.lean()`
## Usage
```javascript
const mongooseLeanId = require('mongoose-lean-id');
```# examples
## It attaches `.id` to result of find, findOne, and findOneAndUpdate if lean
```javascript
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());
});
```