繁体   English   中英

在nodejs中,如何在嵌入的mongodb文档之间遍历以获取其键的值

[英]In nodejs, how do I traverse between embedded mongodb documents to get the values of their keys

所以,我有一个不同的 mongodb 文档相互关联。 我希望能够访问从父文档一直到孙关系文档的值的不同键及其值。 这是我的设置。

const itemSchema = new mongoose.Schema({
    name: String,
    amount: mongoose.Decimal128
});
const Item = new mongoose.model("Item", itemSchema);

const sectionSchema = new mongoose.Schema({
    name: String,
    items: [itemSchema],
    currentAmount: mongoose.Decimal128,
    limitAmount: mongoose.Decimal128,
    sectionStatus: Boolean
});
const Section = new mongoose.model("Section", sectionSchema);

const userSchema = new mongoose.Schema({
    username: {
        type: String,
        required: true,
        unique:true
    },
    email: {
        type: String,
        lowercase: true,
        trim:true,
        required: true,
        unique: true
    },
    password: {
        type: String,
        required: true
    },
    sections: [sectionSchema]
    const User = new mongoose.model("User", userSchema);

我还创建了虚拟文档来填充数据库,这样我就可以将它们发送到 EJS 并设置网页。

我试过了

    User.find({}, function(err,foundUser){
        let rUsername = foundUser.username;
        let rSections = foundUser.sections;
        // let rItems = rSections.items
        // let rItems = foundUser.sections.items;
        console.log(foundUser.sections);
        console.log(foundUser.username);

    });

我似乎无法记录任何内容,因为它只是说undefined 我已经确保我的语法是正确的,因为它的格式与我所学的完全一致,并且在我的其他类似项目中它工作得很好。 只是我以前从未做过“三重”嵌入。 我还确保一切都正确连接:我的依赖项(express、body-parser、mongoose)、MongoDB 数据库充满了它们各自的集合并连接。 我似乎无法在文档或谷歌或 stackoverflow 上的任何地方找到答案。 哈普请 x(

您可以存储对内部模式的引用并populate它们:

const itemSchema = new mongoose.Schema({
  name: String,
  amount: mongoose.Decimal128,
});
const Item = new mongoose.model('Item', itemSchema);

const sectionSchema = new mongoose.Schema({
  name: String,
  items: [{
      type: mongoose.Types.ObjectId, 
      ref: 'Item'
  }],
  currentAmount: mongoose.Decimal128,
  limitAmount: mongoose.Decimal128,
  sectionStatus: Boolean,
});
const Section = new mongoose.model('Section', sectionSchema);

const userSchema = new mongoose.Schema({
  username: {
    type: String,
    required: true,
    unique: true,
  },
  email: {
    type: String,
    lowercase: true,
    trim: true,
    required: true,
    unique: true,
  },
  password: {
    type: String,
    required: true,
  },
  sections: [{
    type: mongoose.Types.ObjectId, 
    ref: 'Section'
  }],
});

const User = new mongoose.model('User', userSchema);

要检索值:

.(async (req, res) => {
    const user = await User.find({})
        .populate({
            path: 'sections',
            populate: { path: 'items', }
        }).exec();
        
    if (!user) { // User not found }
    let rUsername = user[0].username;
    let rSections = user[0].sections;
    console.log(foundUser.sections)
    console.log(foundUser.username)
})

有关嵌套查询人口的更多信息,请参阅官方文档

暂无
暂无

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

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