mongodb 了解 Mongoose 中的关系和外键

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11912127/
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-09 12:47:22  来源:igfitidea点击:

Understanding Relationships & Foreign Keys in Mongoose

mongodbjoinforeign-keysmongoose

提问by Jiew Meng

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

在 MongoDB/Mongoose 中,如何定义关系?我想有几种方法我见过,但我不确定我是否理解这些差异,或者我什么时候使用哪个。我正在使用猫鼬 3

I've defined Todoand TodoListmodel, where the relationship is obvious. So following the docs http://mongoosejs.com/docs/documents.html, I've defined classes like:

我已经定义TodoTodoList建模,其中的关系很明显。因此,按照文档http://mongoosejs.com/docs/documents.html,我定义了如下类:

# 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 Todoare 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?

当我尝试做Todo.find()我一无所获时,所以模型 ( Todo.coffee) 有点多余?看起来Todo存储在 中TodoList,作为用户我可能不在乎,但我想知道有什么含义?例如。文档会不会太大?1 TodoList 有太多 Todos?这有关系吗?如果我允许嵌套的 Todos(不是我想这样做只是为了理解),那么单独存储文档会更好吗?如果我愿意,我该怎么做,什么时候做?

I saw another method, in Mongoose 2 actually, dunno if its possible in 3, something like using ObjectIdinstead of nested docs. Maybe thats to store it separately?

我看到了另一种方法,实际上在 Mongoose 2 中,不知道是否可以在 3 中使用,例如使用ObjectId而不是嵌套文档。也许那是分开存放?

回答by ZachRabbit

I'm still new to Node, Mongoose, and Mongo, but I think I can address at least part of your question. :)

我还是 Node、Mongoose 和 Mongo 的新手,但我想我至少可以解决您的部分问题。:)

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):

您当前的方法与我最初尝试的方法相同。基本上,它最终的存储方式与此非常相似(用 JS 编写,因为我不知道 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#populatein Mongoose.

这存储了一个 ObjectIds 数组,然后您可以Query#populate在 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

编辑:这是一些可能有用的官方文档:http: //mongoosejs.com/docs/populate.html