mongodb Mongoose.model vs Connection.model vs Model.model

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12806559/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 15:56:47  来源:igfitidea点击:

Mongoose.model vs Connection.model vs Model.model

node.jsmongodbmongooseodm

提问by Prashant Bhate

I am bit confused with usage of models in mongoosejs

我对mongoosejs中模型的使用有点困惑

Models can be created using mongoose in these ways

可以通过以下方式使用猫鼬创建模型

Using Mongoose

使用猫鼬

var mongoose = require('mongoose');
var Actor = mongoose.model('Actor', new Schema({ name: String }));

Using Connection

使用连接

var mongoose = require('mongoose');
var db = mongoose.createConnection(..);
db.model('Venue', new Schema(..));
var Ticket = db.model('Ticket', new Schema(..));
var Venue = db.model('Venue');

Using existing Model instance

使用现有的模型实例

var doc = new Tank;
doc.model('User').findById(id, callback);

Now what is the difference between model returned by Mongoose.model, Connection.modeland Model.model. and when to use what , what is the recommended way to create/fetch model ?

现在由Mongoose.model,Connection.model和返回的模型之间有什么区别Model.model。以及何时使用什么,推荐的创建/获取模型的方法是什么?

回答by JohnnyHK

  1. mongoose.modelties the defined model to the default connection that was created by calling mongoose.connect.
  2. db.modelties the model to the connection that was created by calling var db = mongoose.createConnection.
  3. doc.modellooks up another model by name using the connection that doc's model is tied to.
  1. mongoose.model将定义的模型绑定到通过调用创建的默认连接mongoose.connect
  2. db.model将模型与通过调用创建的连接联系起来var db = mongoose.createConnection
  3. doc.model使用模型所关联的连接按名称查找另一个模型doc

All three can be sensibly used in the same program; which one to use just depends on the situation.

所有这三个都可以在同一个程序中明智地使用;使用哪个取决于具体情况。

回答by Prashant Bhate

ok here is what I found

好的,这是我发现的

Important! If you opened a separate connection using mongoose.createConnection() but attempt to access the model through mongoose.model('ModelName') it will not work as expected since it is not hooked up to an active db connection. In this case access your model through the connection you created:

重要的!如果您使用 mongoose.createConnection() 打开了一个单独的连接,但尝试通过 mongoose.model('ModelName') 访问模型,它将无法按预期工作,因为它没有连接到活动的数据库连接。在这种情况下,通过您创建的连接访问您的模型:

var conn = mongoose.createConnection('your connection string');
var MyModel = conn.model('ModelName', schema);
var m = new MyModel;
m.save() // works

vs

对比

var conn = mongoose.createConnection('your connection string');
var MyModel = mongoose.model('ModelName', schema);
var m = new MyModel;
m.save() // does not work b/c the default connection object was never connected

回答by lixun

mongoose.connect is for you connect to same database,although your database is balance or replicaSet

mongoose.connect 是为你连接到同一个数据库,虽然你的数据库是 balance 或 replicaSet

db.model is for multiple connections open to Mongo, each with different read/write settings

db.model 用于对 Mongo 开放的多个连接,每个连接具有不同的读/写设置