繁体   English   中英

使用mongoose查询只获取一个子文档

[英]Use mongoose query to get only a subdocument

我有一个类似的数据结构:

var GrandGrandChild = mongoose.Schema({
    attribute: String,
    id: Number
});

var GrandChild = mongoose.Schema({
    children: [GrandGrandChild],
    id: Number,
    irrelevantAttribute: String
});

var Child = mongoose.Schema({
    children: [GrandChild],
    id: Number,
    irrelevantAttribute2: String
});

var Parent = mongoose.Schema({
    children: [Child],
    id: Number,
    irrelevantAttribute3: String
});

var GrandParent = mongoose.Schema({
    children: [Parent],
    id: Number,
    irrelevantAttribute4: String
});

这些是包含子文档的很多集合。 请注意,ID对于其兄弟姐妹是唯一的,但对于具有相同模式的所有元素而言并非唯一。

因此,一个祖父母可以拥有身份0的父母,而另一个祖父母也可以拥有身份0的父母,但是一个祖父母不能拥有2个身份为0的父母。

保存的唯一模式是GrandParent模式,而mongoose / mongodb是这个祖父母的所有数据的一个很好的单个大文档。 (正是我要找的)

所以这是我的问题:我有一个GrandParent ID,Parent ID,Child ID,GrandChildID和GrandGrandChild ID,我想以某种方式获得所有这些ID所指向的GrandGrandChild对象。

丑陋的方式是,但目前我唯一可以开始工作的方法是创建一个获取GrandParent这个大文档的查询,并手动循环遍历所有数组以找到正确的Parent,然后再循环找到正确的孩子,然后再循环找到合适的孙子,然后再循环,找到我需要的grandgrandchild。

我的问题是,如何在mongoose中编写一个查询,该查询只返回grandgrandchild文档,或者只包含子属性的祖父文档,并且在子类属性中只包含引用的子对象所包含的父对象引用grandgrandchild对象的孙子对象,允许以下结果:

GRANDPARENT  PARENT      CHILD      GRANDCHILD   GRANDGRANDCHILD
grandparent.children[0].children[0].children[0].children[0].attribute;

我希望有人可以帮我解决这个问题,我得到的是:

GrandParentModel.findOne(
    {
        "id" : 0,
        "children.id" : 0,
        "children.children.id" : 0,
        "children.children.children.id" : 0,
        "children.children.children.children.id" : 0
    },
    {"children.children.children.children.$" : 1}, callback);

这个问题的问题在于,不合情理的兄弟姐妹没有被修剪掉。

我希望有人可以帮助我。

Hylke Bron

自从我提出这个问题以来已经有一段时间了,但我认为我找到了一种相当优雅的方式来处理这些结构。

在这种情况下,我将展示它如何仅适用于GrandParent,Parent和Child。

我没有在每个文档(GrandParent.children,Parent.children)中存储子文档列表,而是创建了以下结构的唯一标识符:

Child.referenceId = {
    grandparent: "some key to the grandparent of the parent",
    parent: "some key to the parent",
    child: "key of this child"
};

Parent.referenceId = {
    grandparent: "some key to its grandparent",
    parent: "key of this parent"
}

GrandParent.referenceId = {
    grandparent: "key of this parent"
}

这将创建GrandParent> Parent> Child的层次结构。

模型将类似于以下内容:

var idStructure = {
    grandparent: { type: String, required: true },
    parent: { type: String, required: false },
    child: { type: String, required: false }
};

var GrandParent = mongoose.Schema({
    id: idStructure,
    irrelevantAttribute: String
});

var Parent = mongoose.Schema({
    id: idSructure,
    irrelevantAttribute: String
});

var Child = mongoose.Schema({
    id: idStructure,
    irrelevantAttribute: String
});

请注意,父不直接知道它的父节点,因为它们不存储为子文档。 然而,通过referenceId,父母和孩子之间仍然存在联系。

在搜索GrandParent的整个familytree时,只需执行3个查询,然后正确连接它们:

// First find all children which belong to the grandparent
Child.find({"id.grandparent" : "some key to the grandparent"})
.exec(function(err, children)
{
     if(err)
         return;

     Parent.find({"id.grandparent" : "some key to the grandparent"})
     .exec(function(err, parents)
     {
         if(err)
             return;

         // Loop through the parents and children to connect them before returning to a client
         for(var i = 0; i < parents.length; i++)
         {
             var parent = parents[i];
             parent.children = [];
             // loop through the children to check if they belong to the current parent
             for(var j = 0; j < children.length; j++)
             {
                 var child = children[j];
                 if(parent.id.parent == child.id.parent)
                     parent.children.push(child);
             }
         }

         // After filling the children into the parents, get the grandparents and do the same for the parents and grandparents as done for the children and parents.
        GrandParent.find({"id.grandparent" : "some key to the grandparent"})
       .exec(function(err, grandparents)
       {
           // TODO: the same as done above (two loops, one loops the grandparents, other loops the parents
           // Once this is finished, we have a filled grandparent
       });

     });
});

上面的代码只会导致一个祖父母,父母充满了孩子。

不再找到祖父母的原因是因为祖父母的id应该是唯一的,因为祖父母的referenceId只有祖父母属性。

我希望我明确指出,因为通过这种方法,人们可以轻松地搜索一个特定的孩子,通过引用ID轻松获取其父级,并通过引用ID轻松获取其祖父母。

它可能有点复杂,但是一旦你为自己想出了这个方法,它就会直截了当。

Hylke

很难让这种东西以干净的方式运作。

我没有找到关于这个主题的干净解决方案,但也许我可以帮助你解决循环问题。 您可以使用以下命令来避免循环:var doc = parent.children.id(id); 查找子文档

我希望这对你有帮助。 此致,塞巴斯蒂安。

这对我有用

      model.find({_id:args.id},{ commentList: { $elemMatch: { _id: todo.commentList[todo.commentList.length-1] } } },(err, todos) => {
         if (err) reject(err)
         else resolve(todos)
         console.log(todos);
      })

$ elemMatch(投影)

暂无
暂无

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

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