Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/manuel-di-iorio/find-or-create
Moongose find or create schema plugin
https://github.com/manuel-di-iorio/find-or-create
create document find mongoose nodejs query update
Last synced: 22 days ago
JSON representation
Moongose find or create schema plugin
- Host: GitHub
- URL: https://github.com/manuel-di-iorio/find-or-create
- Owner: manuel-di-iorio
- License: mit
- Created: 2016-06-27T09:48:56.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2022-04-17T06:57:20.000Z (almost 3 years ago)
- Last Synced: 2024-12-09T17:42:00.298Z (about 2 months ago)
- Topics: create, document, find, mongoose, nodejs, query, update
- Language: JavaScript
- Size: 14.6 KB
- Stars: 0
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# NOTICE: Unmaintained repository
# FindOrCreate v2.0 for Mongoose
### Extend the mongoose schemas with a findOrCreate() plugin. Essentially, if a document is not found, will be atomically created or (if specified) updated### Install it with:
npm i find-or-create
### **Examples**:
```javascript
yourSchema.statics.findOrCreate = require("find-or-create");
YourModel = mongoose.model("yourSchema", yourSchema);YourModel.findOrCreate({_id: myID}, {apples: 2}, (err, result) => {
if (err) return console.error(err);
console.log(result.doc); // the document itself
console.log(result.isNew); // if the document has just been created
});
```Example upserting the document and using the promise return:
```javascript
Model.findOrCreate({_id: myID, apples: 2}, {apples: 5}, {upsert: true})
.then((result) => {
console.log(result.doc);
console.log(result.isNew);
})
.catch(console.error);
```## Note:
As of Mongoose v5, to use this module you need to set the global option `useFindAndModify` to _false_, otherwise a warning will be logged.
Example:
```
mongoose.connect(uri, { useFindAndModify: false });
```## Upgrading to Mongoose 5:
The latest v2.0 of this plugin is compatible with Mongoose 5.x.
If you need retro-compatibility with Mongoose 4.x, please install the version 1.1 of this module.---
## API:```javascript
MongooseModel.findOrCreate(query, doc, [options, callback]);
```
If you don't specify a callback, it will be returned a promise.---
- **doc** is the document that will be inserted if the document based on your **query** is not found, otherwise the record will be updated with the new document (if upsert is enabled).
- **options** is an optional object that will be passed to the underlying mongoose 'findOrCreate' method.
You can find the possible options here: http://mongoosejs.com/docs/api.html#query_Query-findOneAndUpdate
Set `{upsert: true}` to update the document when it already exists, otherwhise it will be only inserted when not found
---
## Test with```bash
npm test
```---
## LicenseMIT