MongoDB值是否可以是Hashmap,并且可以在Mongoose模式中对该哈希映射进行建模吗?

时间:2017-02-01 17:50:49

标签: mongodb hashmap mongoose-schema

我已经编写了以下MongooseJS架构。字段"喜欢"是一个嵌入式记录,目前是一个有两个键的POJO。嵌入式模式可以是每个" liker"的哈希映射。领域作为关键?

这里的哈希图应该是什么样的:

const userlikers = {
  {'100900800' : 'user'}
  {'100989455' : 'user'}
  {'109099985' : 'user'}
}

这是当前架构

const userlikers = Schema({
    users : {
        type: Schema.Types.Mixed
    }
})

const PicSchema = Schema ({
    url : {
        type : String, 
        required : true
    }, 
    desc : {
        type : String, 
        required : true
    },
    likers : [userlikers], 
    created : {
        by : {
            type : Schema.Types.ObjectId, 
            ref : 'User', 
            required : true
        }, 
        at : {
            type : Date, 
            default : Date.now
        }
    }
})

const Pic = mongoose.model('Pic', PicSchema);

module.exports = Pic;

1 个答案:

答案 0 :(得分:0)

我知道太晚了, 从猫鼬版本5.10.9起,您可以使用地图代替类型:Schema.Types.Mixed

const PicSchema = Schema ({
    ..............
    ........
    likers : { 
            type: Map,
            of: String
           }, 
   ........
   ........
})