【问题标题】:Mongo Relationships/ReferencingMongo 关系/参考
【发布时间】:2021-05-28 03:32:47
【问题描述】:

我是 MongoDB 引用的新手。现在我有一个集合,我称之为users。它存储所有用户及其密钥。我有另一个集合,其中包含每个键的数据。

我只想使用他们的密钥作为 ID 来连接他们。因此,我将生成每个键,并且 keyData 在第一次创建时将为空,然后我将继续将对象添加到 keyData 数组中。这是我的计划,但我不知道如何创建与架构的关系。

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

//Create Schema 

const userKey = new Schema({
    _id : {
        type : String,
        required: true
    },
    key: {
        type : String,
        required: true
    },
    keyData: [key],
    date: {
        type: Date,
        default: Date.now
    }
});


module.exports = Key = mongoose.model('key', userKey);

这不起作用,因为在初始化之前我无法访问密钥。那么如何关联这两个集合呢?

【问题讨论】:

  • 您需要两个单独的模式示例 userModelkeyModel 来分别管理 userskeys 集合并像这里显示的那样链接它:mongoosejs.com/docs/populate.html
  • 我仍然对如何做这些感到很困惑。它如何知道要使用哪个 id?
  • 您将关键详细信息存储在哪个集合中?根据您的问题,您有一个存储用户详细信息的集合。
  • 我有两个架构,我在代码中显示的一个是保存所有用户密钥的那个。
  • 我想我现在明白你的问题了。你想在插入data 集合后自动将对象添加到keyData 数组中吗?如果是这样,请阅读有关 mongoose post hooks 的信息:mongoosejs.com/docs/middleware.html#post

标签: node.js mongodb mongoose


【解决方案1】:

架构 #1:用户数据

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

// Create User-data Schema 
const userData = new Schema({
  data: {
    type: Array,
    require: true
  }
},
{
  collection: 'data' // Mentioning collection name explicitly is good!
});

module.exports = mongoose.model('data', userData);

架构 #2:键

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

// Create User-Key Schema 
const userKey = new Schema({
  key: {
    type: String,
    required: true
  },
  keyData: { 
    type: Schema.Types.ObjectId, 
    ref: 'data' 
  },
  date: {
    type: Date,
    default: Date.now
  }
},
{
  collection: 'keys' // Mentioning collection name explicitly is good!
});

module.exports = mongoose.model('keys', userKey);

根据this link,不可能将字符串设置为refs。所以在你的keys 架构中使用ObjectId 作为ref

【讨论】:

  • 这让我更加困惑。你为同一件事制作了两个模式?用户和密钥是相同的。我们用他们的密钥来识别我们的用户。然后我想要另一个集合,它有一个包含数据的对象数组。每个键都有自己的数据。如果这有意义的话。
  • 我会将我所拥有的内容放在“回答你的问题”中,因为由于某种原因我无法编辑我当前的帖子,它对我来说是错误的。
  • 试试看,但你错过了第二点 _id 不能属于 string 类型。
  • 是的,我只是在我的代码中更改了它。我马上去测试。我是否只需初始化 userKey 架构,它会自动生成 useData 架构?
  • 这对我不起作用。对于 keydata 字段,它只是作为:Array/empty array
【解决方案2】:

这样的东西有用吗?

const userData = new Schema({
    _id : {
        type : String,
        required: true
    },
    data : {
        type : Array,
        require: true
    }
});

const userKey = new Schema({
    _id : {
        type : String,
        required: true
    },
    key: {
        type : String,
        required: true
    },
    keyData: [{ type: Schema.Types.ObjectId, ref: 'data' }],
    date: {
        type: Date,
        default: Date.now
    }
});




module.exports = KeyData = mongoose.model('data', userData);
module.exports = Key = mongoose.model('key', userKey);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-19
    • 1970-01-01
    • 1970-01-01
    • 2021-10-27
    • 1970-01-01
    相关资源
    最近更新 更多