简体   繁体   English

在node.js上使用猫鼬通过字符串键入多对多关系

[英]Many to many relationship keyed by string using mongoose on node.js

The best way I can describe what I'm after is by starting with a simplified demo page document: 我可以描述我追求的最好方法是从一个简化的演示page文档开始:

{
    name: "Page Name"
    blocks: {
        "top-content": 50f0ded99561a3b211000001,
        "bottom-content": 50f0ded99561a3b211000002
    }
}

Is there a way I can define this kind of many-to-many relation with mongoose so that I can look them up by string keys like top-content and bottom-content ? 有没有一种方法可以用猫鼬定义这种多对多关系,以便可以通过诸如top-contentbottom-content类的字符串键来查找它们? The relational equivalent to this would be something like including a string on the many to many join table. 与此相关的关系将类似于在多对多连接表上包括一个字符串。

Important: 重要:

  • I don't want to embed the blocks I'm referencing as they could be referenced by multiple pages. 我不想嵌入我要引用的块,因为它们可以被多个页面引用。

Mongoose的查询填充功能支持此功能,但是top-contentbottom-content将需要是ObjectId而不是字符串(无论如何效率更高)。

After some chatting in #node.js on Freenode, it seems like I can just set this up as an array and put arbitrary key/value pairs containing the references I want. 在Freenode上的#node.js中聊天之后,似乎我可以将其设置为数组并放入包含所需引用的任意键/值对。

At which point, I'll be dereferencing things myself. 在这一点上,我将自己取消引用。

{ //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;
  });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM