mongoose-findoneorcreate
TypeScript icon, indicating that this package has built-in type declarations

1.0.3 • Public • Published

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

Readme

Keywords

none

Package Sidebar

Install

npm i mongoose-findoneorcreate

Weekly Downloads

21

Version

1.0.3

License

MIT

Unpacked Size

6.37 kB

Total Files

8

Last publish

Collaborators

  • fabiospampinato