【问题标题】:Nested subschema in mongoose猫鼬中的嵌套子模式
【发布时间】:2015-09-21 22:25:00
【问题描述】:

我在 mongodb 中创建了一个非常嵌套的模式。看起来像这样

{
    "_id": ObjectID("559690ec34c506cea4be1775"),
    "cities": [
        {
            "val": "something",
            "pincode": [
                {
                    "val": "something",
                    "people": [
                        {
                            "val": "something",
                            "frnds": [
                                {
                                    "val": "something1",
                                    "frndaddress": [
                                        {
                                            "val": "something2"
                                        }
                                    ]
                                }
                            ]
                        }
                    ]
                }
            ]
        }
    ]
}

此文档已正确插入到 mongodb 中,但我不知道如何在 mongoose 上转换它

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

// create a all city list, without id
var allCitySchema = new Schema({
    cities: [{
        val:{ type: String },
        pincode:[{
                val:{ type: String },
                people:[{
                      val:{ type: String },  
                      frnds:[{
                              val:{ type: String },
                              frndaddress:[{
                                 val:{ type: String },     
                              }]
                      }]  
                }]
        }]
    }]
}, {collection: 'allcities'});

var allcities = mongoose.model('allcities', allCitySchema);
module.exports = allcities;

我是节点和 mongodb 的新手,我在上面创建的架构我什至不知道它是否正确。

【问题讨论】:

  • its seems its not working你能详细说明一下吗?
  • 当我尝试插入数据时,它的显示验证失败

标签: node.js mongodb


【解决方案1】:

我建议您尝试扁平化您的数据结构。这实际上取决于所有这些字段是如何连接的,但是,根据我的经验,一个好的经验法则是通过引用数组链接 mongoDB 中的对象是保持关系的好方法,但仍然允许高度解耦信息。这是一个示例,说明如何为上述设置模式但仍保留所需的关系。 首先是每个城市的架构:

// A Schema for city information
var mongoose = require('mongoose');
var People = require('./people.js');

var City = new mongoose.Schema({
//Pincode for this city object
pincode: { type: String },
// An array of references to the people objects for those who live in this city
people: [{ type: mongoose.Schema.Types.ObjectId, ref: 'People' }]
});

module.exports = mongoose.model('City', City);

然后在一个单独的文件中,一个人的模式:

// A Schema for person information
var mongoose = require('mongoose');
var Friends = require('./people.js');

var Person = new mongoose.Schema({
    //The address of this Person object
    address: { type: String },
    // An array of references to the other people objects who are friends of this person
    friends: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Friends' }]
    });

    module.exports = mongoose.model('People', People);

请注意,朋友也是 People 对象。这种数据结构有几个优点。 1) 所有字段在数据库中只有一个位置,因此使用的磁盘空间最少,因为冗余非常少(仅引用 ID)。 2) 使用 mongoose 通过mongoose populationdeep population,通过 DB 查询从多个 Schema 中检索信息相当容易。

【讨论】:

    【解决方案2】:

    通常,有两种方法可以创建relationship 或创建sub docs

    【讨论】:

      猜你喜欢
      • 2019-03-22
      • 2014-06-22
      • 2019-01-16
      • 2017-01-28
      • 1970-01-01
      • 2021-06-07
      • 1970-01-01
      • 2019-07-20
      • 2017-06-12
      相关资源
      最近更新 更多