【问题标题】:How to formulate the following Mongoose query?如何制定以下 Mongoose 查询?
【发布时间】:2013-08-17 17:44:56
【问题描述】:

这是我的架构:

var A = new Schema({
    active: Boolean
  , containers: [{
         b: { type: ObjectId, ref: 'B' }
    }]
})

var B = new Schema({
    c: { type: ObjectId, ref: 'C' }
  , d: { type: ObjectId, ref: 'D' }
})

var C = new Schema({ })

var D = new Schema({ })

基本上,A 有一个容器数组,这些容器引用了 BB 引用了 CD

现在我有一个C 的ID,我需要一组Dactive As 使用。这甚至可能吗?我应该以某种方式更改架构吗?

编辑:这里是真正的架构

 //Mashup has a number of Containers (containerSchema is a sub-doc)
    //so that the same Component could belong to two different Containers
    var containerSchema = new Schema({
          pos: { top: Number, left: Number }
        , size: { width: Number, height: Number }
        , component: { type: ObjectId, ref: 'Component' }
    })

    var mashupSchema = new Schema({
          name: String
        , desc: String
        , size: { width: Number, height: Number }
        , active: Boolean
        , containers: [containerSchema]
    })

    //I am using 'mongoose-schema-extend' to inherit from componentSchema (waiting for the new PR)
    var componentSchema = new Schema({
          name: String
        , desc: String
    }, { collection : 'components', discriminatorKey : '_type' })

    //now the various components
    var imageComponentSchema = componentSchema.extend({
          url: String
    })

    var textComponentSchema = componentSchema.extend({
          text: String
    })


    var htmlComponentSchema = componentSchema.extend({
          html: String
    })

    //this particular component needs a page and a selector
    //(which could live outside it and belong to multiple components)
    var webComponentSchema = componentSchema.extend({
          page: { type: ObjectId, ref: 'Page' }
        , selector: { type: ObjectId, ref: 'Selector' }
    })

    var pageSchema = new Schema({
          name: String
        , desc: String
        , url: String
        , active: { type: Boolean, default: false }
    })

    var selectorSchema = new Schema({
          desc: String
        , url: String
        , cssPath: String
    })

    ///MODELS
    var Mashup = mongoose.model("Mashup", mashupSchema)
    var Component = mongoose.model("Component", componentSchema)
    var ImageComponent = mongoose.model("ImageComponent", imageComponentSchema)
    var TextComponent = mongoose.model("TextComponent", textComponentSchema)
    var HtmlComponent = mongoose.model("HtmlComponent", htmlComponentSchema)
    var WebComponent = mongoose.model("WebComponent", webComponentSchema)
    var Page = mongoose.model("Page", pageSchema)
    var Selector = mongoose.model("Selector", selectorSchema)

【问题讨论】:

    标签: mongodb mongoose mongoose-populate


    【解决方案1】:

    你想得太有关系了!现在的你,我认为你不能高效地做到这一点,因为你必须这样做:

    • 找到所有有c: {id}的B
    • 查找所有具有b 的 A,它是查询 1 的结果集并且处于活动状态
    • 找出哪些 B 属于您在上一个查询中找到的 ID
    • 找出哪些 D 属于您找到的 B

    我认为您绝对应该在这里非规范化您的架构。例如,您可以将以上所有内容放在一个文档中:

    {
        Active: true,
        containers: [ // B's
            { c: [
                { _id: X, field1: foo },
                { _id: X, field1: foo },
            ] },
            { d: [
                { _id: X, field1: foo },
                { _id: X, field1: foo },
            ] }
        ]
    }
    

    然后你可以用一个查询来完成:

    db.collection.find(
        { "container.c._id" : your_id, Active: true }, // query
        { "container.d" : 1 } // projection
    );
    

    【讨论】:

    • 但是我仍然需要B..我希望能够查询所有B。客户端需要能够创建一个新A,然后从列表中选择一个B并将其添加到A. 我想我可以有一个 B 而不是推动它,提取 C 和 D 并推动它们.. Mhh
    • 你是 100% 正确的,对不起那个例子。 Here我贴了一个真实的例子。谢谢:)
    【解决方案2】:

    最终完全更改了数据库架构。结束。

    【讨论】:

      猜你喜欢
      • 2021-11-19
      • 1970-01-01
      • 1970-01-01
      • 2020-04-08
      • 1970-01-01
      • 2019-03-06
      • 1970-01-01
      • 2015-07-22
      • 1970-01-01
      相关资源
      最近更新 更多