<bdo id='VApZH'></bdo><ul id='VApZH'></ul>

<small id='VApZH'></small><noframes id='VApZH'>

  • <legend id='VApZH'><style id='VApZH'><dir id='VApZH'><q id='VApZH'></q></dir></style></legend>

      <tfoot id='VApZH'></tfoot>

      <i id='VApZH'><tr id='VApZH'><dt id='VApZH'><q id='VApZH'><span id='VApZH'><b id='VApZH'><form id='VApZH'><ins id='VApZH'></ins><ul id='VApZH'></ul><sub id='VApZH'></sub></form><legend id='VApZH'></legend><bdo id='VApZH'><pre id='VApZH'><center id='VApZH'></center></pre></bdo></b><th id='VApZH'></th></span></q></dt></tr></i><div id='VApZH'><tfoot id='VApZH'></tfoot><dl id='VApZH'><fieldset id='VApZH'></fieldset></dl></div>

      1. 猫鼬填充嵌入式

        时间:2023-08-08

      2. <i id='3YxlY'><tr id='3YxlY'><dt id='3YxlY'><q id='3YxlY'><span id='3YxlY'><b id='3YxlY'><form id='3YxlY'><ins id='3YxlY'></ins><ul id='3YxlY'></ul><sub id='3YxlY'></sub></form><legend id='3YxlY'></legend><bdo id='3YxlY'><pre id='3YxlY'><center id='3YxlY'></center></pre></bdo></b><th id='3YxlY'></th></span></q></dt></tr></i><div id='3YxlY'><tfoot id='3YxlY'></tfoot><dl id='3YxlY'><fieldset id='3YxlY'></fieldset></dl></div>
        <tfoot id='3YxlY'></tfoot>
                <tbody id='3YxlY'></tbody>
              <legend id='3YxlY'><style id='3YxlY'><dir id='3YxlY'><q id='3YxlY'></q></dir></style></legend>

              <small id='3YxlY'></small><noframes id='3YxlY'>

                <bdo id='3YxlY'></bdo><ul id='3YxlY'></ul>

                  本文介绍了猫鼬填充嵌入式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我使用 Mongoose.js,无法解决 3 级层次文档的问题.

                  I use Mongoose.js and cannot solve problem with 3 level hierarchy document.

                  有两种方法.

                  第一 - 没有参考.

                  C = new Schema({
                      'title': String,
                  });
                  
                  B = new Schema({
                      'title': String,
                      'c': [C]
                  });
                  
                  A = new Schema({
                      'title': String,
                      'b': [B]
                  });
                  

                  我需要显示 C 记录.我如何填充/找到它,只知道 C 的 _id?

                  I need to show C record. How can i populate / find it, knowing only _id of C?

                  我正在尝试使用:

                  A.findOne({'b.c._id': req.params.c_id}, function(err, a){
                      console.log(a);
                  });
                  

                  但我不知道如何从 returnet 中获取我需要的仅 c 对象.

                  But i dont know how to get from returnet a object only c object that i need.

                  其次如果使用 refs:

                  C = new Schema({
                      'title': String,
                  });
                  
                  B = new Schema({
                      'title': String,
                      'c': [{ type: Schema.Types.ObjectId, ref: 'C' }]
                  });
                  
                  A = new Schema({
                      'title': String,
                      'b': [{ type: Schema.Types.ObjectId, ref: 'B' }]
                  });
                  

                  如何填充所有 B、C 记录以获得层次结构?

                  How to populate all B, C records to get hierarchy?

                  我试图使用这样的东西:

                  I was try to use something like this:

                  A
                  .find({})
                  .populate('b')
                  .populate('b.c')
                  .exec(function(err, a){
                      a.forEach(function(single_a){
                          console.log('- ' + single_a.title);
                          single_a.b.forEach(function(single_b){
                              console.log('-- ' + single_b.title);
                              single_b.c.forEach(function(single_c){
                                  console.log('--- ' + single_c.title);
                              });
                          });
                      });
                  });
                  

                  但它会为 single_c.title 返回 undefined.我有办法填充它吗?

                  But it will return undefined for single_c.title. I there way to populate it?

                  谢谢.

                  推荐答案

                  在 Mongoose 4 中,您可以跨多个级别填充文档:

                  In Mongoose 4 you can populate documents across multiple levels:

                  假设您有一个 User 架构来跟踪用户的朋友.

                  Say you have a User schema which keeps track of the user's friends.

                  var userSchema = new Schema({
                    name: String,
                    friends: [{ type: ObjectId, ref: 'User' }]
                  });
                  

                  首先populate() 让你得到一个用户好友列表.但是,如果您还想要用户的朋友的朋友怎么办?在这种情况下,您可以指定 populate 选项来告诉 mongoose 填充所有用户朋友的 friends 数组:

                  Firstly populate() lets you get a list of user friends. But what if you also wanted a user's friends of friends? In that case, you can specify a populate option to tell mongoose to populate the friends array of all the user's friends:

                  User.
                    findOne({ name: 'Val' }).
                    populate({
                      path: 'friends',
                      // Get friends of friends - populate the 'friends' array for every friend
                      populate: { path: 'friends' }
                    });
                  

                  取自:http://mongoosejs.com/docs/populate.html#deep-填充

                  这篇关于猫鼬填充嵌入式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何在 mongodb 的数组顶部添加一个值? 下一篇:如何在mongodb中只返回一个字段的值

                  相关文章

                  最新文章

                1. <tfoot id='2Nwka'></tfoot>
                  <legend id='2Nwka'><style id='2Nwka'><dir id='2Nwka'><q id='2Nwka'></q></dir></style></legend>

                2. <i id='2Nwka'><tr id='2Nwka'><dt id='2Nwka'><q id='2Nwka'><span id='2Nwka'><b id='2Nwka'><form id='2Nwka'><ins id='2Nwka'></ins><ul id='2Nwka'></ul><sub id='2Nwka'></sub></form><legend id='2Nwka'></legend><bdo id='2Nwka'><pre id='2Nwka'><center id='2Nwka'></center></pre></bdo></b><th id='2Nwka'></th></span></q></dt></tr></i><div id='2Nwka'><tfoot id='2Nwka'></tfoot><dl id='2Nwka'><fieldset id='2Nwka'></fieldset></dl></div>
                3. <small id='2Nwka'></small><noframes id='2Nwka'>

                      • <bdo id='2Nwka'></bdo><ul id='2Nwka'></ul>