了解关系&猫鼬的外键 [英] Understanding Relationships & Foreign Keys in Mongoose
问题描述
我已经定义了 Todo
和 TodoList
模型,其中的关系是显而易见的。所以遵循文档 http://mongoosejs.com/docs/documents.html ,我已经定义的类如:
#Todo.coffee
mongoose = requiremongoose
todoSchema = mongoose.Schema
name:String
desc:String
dueOn:Date
completedOn:Date
module.exports = mongoose.modelTodo ,todoSchema
#TodoList.coffee
mongoose = requiremongoose
Todo = require./Todo
todoListSchema = mongoose.Schema
name:String
todos:[Todo.schema]
module.exports = mongoose.modelTodoList,todoListSchema
$ c
然后我试着测试类:
list = new TodoList
name:List 1
todos:[
{name:Todo 1,desc:Hello,dueOn:new Date(2012,10, 1),completedOn:new Date(2012,10,2)}
{name:Todo 2 }
{name:Todo 3,desc:Hello 2,dueOn:new Date(2012,10,6),completedOn:new Date(2012,10,2)}
{name :Todo 4}
]
#list.todos.push {name:Todo 5}
console.logList,list
list.save(err ) - >
if!err
TodoList.find {},(err,lists) - >
console.logTODOS
console.log lists.length,列出
done(err)
else
console.logERROR !!!
完成错误
当我尝试执行 Todo.find )
我什么也没得到,所以Model( Todo.coffee
)是多余的?它看起来像 Todo
存储在 TodoList
中,作为一个我可能不在意的用户,但是我想知道这会产生什么影响?例如。文件会变得太大吗? 1 TodoList有太多的Todos?那很重要吗?如果我允许嵌套的Todos(不是我想为了理解而去做),那么单独存储文档会更好吗?我如何做到这一点,如果我愿意,我什么时候做?
我看到另外一个方法,在Mongoose 2中实际上是不知道它是否可能在3中,像使用 ObjectId
嵌套的文档。也许这是分开存储的?
解决方案我还是Node,Mongoose和Mongo的新手,但是我想我至少可以解决一部分问题。 :)
你现在的方法和我一开始就是一样的。基本上,它最终存储它非常类似于此(用JS编写,因为我不知道CoffeeScript):
var todoListSchema = new mongoose.Schema({
name:String,
todos:[{
name:String,
desc:String,
dueOn:Date,
completedOn:Date
}]
});
后来我找到了这个方法,这就是我在找的东西,我想你的意图是什么:
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ t $
类型:mongoose.Schema.Types.ObjectId,
ref:'Todo'//编辑:我会把模式。傻傻的我
}]
});
存储一个ObjectIds数组,然后使用 Query#在Mongoose中填充
。
我不知道技术含义,但是如果我把它们分开,所以这就是我正在做的。 :)
编辑:以下是一些可能有用的官方文档: http://mongoosejs.com/docs/populate.html
In MongoDB/Mongoose, how do I define a relationship? I think there are a few ways I've seen, but I'm not sure I understand the differences or when do I use which. I am using Mongoose 3
I've defined Todo
and TodoList
model, where the relationship is obvious. So following the docs http://mongoosejs.com/docs/documents.html, I've defined classes like:
# Todo.coffee
mongoose = require "mongoose"
todoSchema = mongoose.Schema
name: String
desc: String
dueOn: Date
completedOn: Date
module.exports = mongoose.model "Todo", todoSchema
# TodoList.coffee
mongoose = require "mongoose"
Todo = require "./Todo"
todoListSchema = mongoose.Schema
name: String
todos: [Todo.schema]
module.exports = mongoose.model "TodoList", todoListSchema
Then I tried testing the classes:
list = new TodoList
name: "List 1"
todos: [
{ name: "Todo 1", desc: "Hello", dueOn: new Date(2012,10,1), completedOn: new Date(2012,10,2) }
{ name: "Todo 2" }
{ name: "Todo 3", desc: "Hello 2", dueOn: new Date(2012,10,6), completedOn: new Date(2012,10,2) }
{ name: "Todo 4" }
]
#list.todos.push { name: "Todo 5" }
console.log "List", list
list.save (err) ->
if !err
TodoList.find {}, (err, lists) ->
console.log "TODOS"
console.log lists.length, lists
done(err)
else
console.log "ERROR!!!"
done err
When I try to do Todo.find()
I get nothing, so the Model (Todo.coffee
) is kind of redundant? It looks like Todo
are stored in TodoList
, as a user I may not care, but I wonder what are the implications? Eg. will the document get too large? 1 TodoList with too many Todos? Does that matter? What if I allow nested Todos (not that I want to do itm just for understanding), is it better to store documents separately then? How do I do that, if I so desire, and when do I do it?
I saw another method, in Mongoose 2 actually, dunno if its possible in 3, something like using ObjectId
instead of nested docs. Maybe thats to store it separately?
I'm still new to Node, Mongoose, and Mongo, but I think I can address at least part of your question. :)
Your current method is the same as I tried doing at first. Basically, it ends up storing it very similarly to this (written in JS, since I don't know CoffeeScript):
var todoListSchema = new mongoose.Schema({
name: String,
todos: [{
name: String,
desc: String,
dueOn: Date,
completedOn: Date
}]
});
I later found this method, which is what I was looking for, and I think what you were intending:
var todoListSchema = new mongoose.Schema({
name: String,
todos: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'Todo' //Edit: I'd put the schema. Silly me.
}]
});
This stores an array of ObjectIds, which you can then load using Query#populate
in Mongoose.
I don't know of the technical implications, but it makes more sense in my brain if I keep them separate, so that's what I'm doing. :)
Edit: Here is a some official docs that might be useful: http://mongoosejs.com/docs/populate.html
这篇关于了解关系&猫鼬的外键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!