简体   繁体   中英

To require or not to require { model, Schema } for mongoose?

Is there any difference between requiring twice as opposed to just destructuring model and Schema from the existing variable mongoose already created?

Here are the two different examples. In a Node app is there a difference between the two following code examples outside of basic syntax and readability.

const mongoose = require('mongoose');
const { model, Schema } = mongoose;
const mongoose = require('mongoose')
const { model, Schema } = require('mongoose')

It seems they both work fine. I assume it's a very simple question to answer.

If you need the mongoose object later, then go with the first one. If you want to use only model and Schema then go simply with:

const { model, Schema } = require('mongoose')

You don't want to load the module twice, there is a performance difference.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM