简体   繁体   中英

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

So, I have a different mongodb documents in relationship with one another. I want to be able to get access to the the different keys and their values starting with the parent document all the way down to the values of the grandchild relational document. This is the setup I have.

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);

I've also created dummy documents to fill the database so I can then send them to EJS and set up the webpage.

I've tried

    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);

    });

I cant seem to get anything to log as it just says undefined . I've made sure my syntax is correct as this exactly formatted as I was taught to and in my other similiar projects it works just fine. It's just I've never made a 'tripple' embed before. I've also made sure everything is hooked up correctly: my dependencies (express, body-parser, mongoose), the MongoDB database is filled with their respective collections and connected. I cant seem to find the answer in the documentation or anywhere on google or stackoverflow. halp plz x(

You could store the reference to the inner schemas and populate them:

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);

To retrieve the values:

.(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)
})

For more information on nested query population see the official docs .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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