[英]Many-to-many mapping with Mongoose
我的设计中有FlashcardSchemas和PackageSchemas。 一个闪存卡可以属于不同的包,一个包可以包含不同的闪存卡。
您可以在下面看到我的mongoose模式定义的精简版本:
// package-schema.js
var Schema = mongoose.Schema,
ObjectId = Schema.ObjectId;
var PackageSchema = new Schema({
id : ObjectId,
title : { type: String, required: true },
flashcards : [ FlashcardSchema ]
});
var exports = module.exports = mongoose.model('Package', PackageSchema);
// flashcard-schema.js
var Schema = mongoose.Schema,
ObjectId = Schema.ObjectId;
var FlashcardSchema = new Schema({
id : ObjectId,
type : { type: String, default: '' },
story : { type: String, default: '' },
packages : [ PackageSchema ]
});
var exports = module.exports = mongoose.model('Flashcard', FlashcardSchema);
从上面的注释中可以看出,这两个模式定义属于单独的文件并相互引用。
我得到一个例外,说明没有按预期定义PackageSchema。 如何映射与猫鼬的多对多关系?
我是node,mongoDB和mongoose的新手,但我认为这样做的正确方法是:
var PackageSchema = new Schema({
id: ObjectId,
title: { type: String, required: true },
flashcards: [ {type : mongoose.Schema.ObjectId, ref : 'Flashcard'} ]
});
var FlashcardSchema = new Schema({
id: ObjectId,
type: { type: String, default: '' },
story: { type: String, default: '' },
packages: [ {type : mongoose.Schema.ObjectId, ref : 'Package'} ]
});
这样,您只存储对象引用而不是嵌入对象。
您正在以正确的方式执行此操作,但问题是您必须在flashcard-schema.js中包含PackageSchema,反之亦然。 否则这些文件不知道你在引用什么
var Schema = mongoose.Schema,
ObjectId = Schema.ObjectId;
PackageSchema = require('./path/to/package-schema.js')
var FlashcardSchema = new Schema({
id : ObjectId,
type : { type: String, default: '' },
story : { type: String, default: '' },
packages : [ PackageSchema ]
});
您可以使用Schema.add()方法来避免前向引用问题。
此(未经测试的)解决方案将架构放在一个.js文件中
车型/ index.js
var Schema = mongoose.Schema,
ObjectId = Schema.ObjectId;
// avoid forward referencing
var PackageSchema = new Schema();
var FlashcardSchema = new Schema();
PackageSchema.add({
id : ObjectId,
title : { type: String, required: true },
flashcards : [ FlashcardSchema ]
});
FlashcardSchema.add({
id : ObjectId,
type : { type: String, default: '' },
story : { type: String, default: '' },
packages : [ PackageSchema ]
});
// Exports both types
module.exports = {
Package: mongoose.model('Package', PackageSchema),
Flashcard: mongoose.model('Flashcard', FlashcardSchema)
};
您认为这非常像关系数据存储。 如果这是你想要的,使用MySQL(或其他RDBMS)
如果失败了,那么是的,可以使用第三个模式,但不要忘记它仍然只是每个对象的id(没有连接,请记住)所以你仍然需要在单独的查询中检索每个其他项目。
https://www.npmjs.com/package/mongoose-relationship
##Many-To-Many with Multiple paths
var mongoose = require("mongoose"),
Schema = mongoose.Schema,
relationship = require("mongoose-relationship");
var ParentSchema = new Schema({
children:[{ type:Schema.ObjectId, ref:"Child" }]
});
var Parent = mongoose.models("Parent", ParentSchema);
var OtherParentSchema = new Schema({
children:[{ type:Schema.ObjectId, ref:"Child" }]
});
var OtherParent = mongoose.models("OtherParent", OtherParentSchema);
var ChildSchema = new Schema({
parents: [{ type:Schema.ObjectId, ref:"Parent", childPath:"children" }]
otherParents: [{ type:Schema.ObjectId, ref:"OtherParent", childPath:"children" }]
});
ChildSchema.plugin(relationship, { relationshipPathName:['parents', 'otherParents'] });
var Child = mongoose.models("Child", ChildSchema)
var parent = new Parent({});
parent.save();
var otherParent = new OtherParent({});
otherParent.save();
var child = new Child({});
child.parents.push(parent);
child.otherParents.push(otherParent);
child.save() //both parent and otherParent children property will now contain the child's id
child.remove()
这是循环/循环依赖的问题。 这就是你如何使它在nodejs中工作。 有关更多详细信息,请查看http://exploringjs.com/es6/ch_modules.html#sec_modules-in-javascript中的“CommonJS中的循环依赖项”
//------ a.js ------ var b = require('b'); function foo() { b.bar(); } exports.foo = foo; //------ b.js ------ var a = require('a'); // (i) function bar() { if (Math.random()) { a.foo(); // (ii) } } exports.bar = bar;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.