B
B
bro-dev2018-02-13 05:56:06
JavaScript
bro-dev, 2018-02-13 05:56:06

How does the model know which mongo connection to use?

spoiler
var mongoose = require('mongoose');
mongoose.connect("mongodb://localhost/test");
var User = require(__dirname + '/models/User.js');
spoiler

User.js content
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var userSchema = new Schema({
    id: {
        type: Number,
        unique: true,
    },
});
module.exports = mongoose.model('User', userSchema);

After such code, I can use the model, select from the database and save, but how does she know that I need to use this particular connection from above, because these two variables and User and mongoose are not connected at all. Immediately the question is how can I, for example, then in one application take users from two different databases, for example, according to one model.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
levchak0910, 2018-02-13
@levchak0910

I'll try to feel myself in the role of Vanga. Between the 2nd and 3rd line of code somewhere should be something like this:

const UserSchema = new mongoose.Schema({...});
module.exports = mongoose.model("User", UserSchema);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question