1.0.3 • Published 5 years ago

mongoose-findoneorcreate v1.0.3

Weekly downloads
31
License
MIT
Repository
github
Last release
5 years ago

findOneOrCreate

Promise-based plugin for Mongoose that defines a "findOneOrCreate" static method.

Useful for finding a document, with support for creating it if doesn't exist already.

Install

$ npm install --save mongoose-findoneorcreate

Usage

import * as mongoose from 'mongoose';
import findOneOrCreate from 'mongoose-findoneorcreate';
import BookSchema from './book_schema';

BookSchema.plugin ( findOneOrCreate );

const model = mongoose.model ( 'Book', BookSchema );

const book = await model.findOneOrCreate ( { title: 'My book title' }, {
  title: 'My book title',
  author: 'Me',
  category: 'fantasy'
});

Related

  • findAnyoneOrCreate - Promise-based plugin for Mongoose that defines a "findAnyoneOrCreate" static method.

License

MIT © Fabio Spampinato