node.js上使用mongoose的string的多对多关系
我可以用最简单的演示page
文件开始描述我的最佳方式:
{ name: "Page Name" blocks: { "top-content": 50f0ded99561a3b211000001, "bottom-content": 50f0ded99561a3b211000002 } }
有没有一种方法可以用mongoose来定义这种多对多的关系,这样我就可以用top-content
和bottom-content
类的string来查找它们了? 与此相关的关系就像在多对多的连接表中包含一个string一样。
重要:
- 我不想embedded我引用的块,因为它们可以被多个页面引用。
Mongoose的查询填充function支持这一点,但是top-content
和bottom-content
需要是ObjectId而不是string(无论如何都是更高效的)。
在Freenode上的#node.js中聊了一会之后,好像我可以将它设置为一个数组,并将包含我想要的引用的任意键/值对。
在这一点上,我将自己解除引用。
{ //PAGE 1 name:"Sticks", blocks:[ "top":[OID(5),OID(6)], "bottom":[OID(7),OID(8)] ] }; { //PAGE 2 name:"Poopsicles", blocks:[top:[OID(7)]] }; //BLOCKS { OID:5, name:"top" }; { OID:7, name:"bottom rocker" }; //QUERYING page.findOne({name:"Sticks"}, function(e,d) { var a = []; for(l in d.blocks) { a.append(d.blocks[l]); } blocks.find({oid:a},function (e,b) { //do your stuff with the blocks and page here; }); });