最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

MongoDB $ unwind并保留数组字段和每个未解卷的文档以供参考

IT培训 admin 5浏览 0评论

MongoDB $ unwind并保留数组字段和每个未解卷的文档以供参考

我想保留每个解开文档的原始数组字段。

示例:在展开band的成员数组时,我想保留原始成员数组和每个doc,以便我可以引用其他成员

{ 'name': 'the crazies', 'member': 'Tom', 'othermembers': [ 'Tom', 'Mike', 'Sally' ] }

{ 'name': 'the crazies', 'member': 'Mike', 'othermembers': [ 'Tom', 'Mike', 'Sally' ] }

{ 'name': 'the crazies', 'member': 'Sally', 'othermembers': [ 'Tom', 'Mike', 'Sally' ] }

最终,成员数组不应该包含已经在“名称”字段中的成员的名称,但如果有人有任何想法,我会采取我可以得到的。

一种有效的方法是通过乐队ID对自己进行$查找,但看起来有点笨拙。

band.aggregate()
  .match( 'whatever criteria' )
  .lookup({ from: 'bands', localField: '_id', foreignField: '_id', as       'othermembers')
  .unwind({ path: 'members')
  .project({
    'name': 1
    'member': '$members.name',
    'othermembers.members.name': 1;
})

思考???

回答如下:

在展开members数组之前,将项目成员数组作为两个新字段,然后展开两个新字段中的任何一个。例如

{
"_id" : ObjectId("5c5ca7184ef14365b786e11f"),
"a" : [ 
    10, 
    20, 
    30
],
"b" : "hello"

}

对于上述数据,我使用以下查询

db.testColl.aggregate([
{$project: {old_arr: "$a", new_arr: "$a"}},
{$unwind: {path: "$old_arr"}}
])

我得到的结果是

    {
    "_id" : ObjectId("5c5ca7184ef14365b786e11f"),
    "old_arr" : 10,
    "new_arr" : [ 
        10, 
        20, 
        30
    ]
}

{
    "_id" : ObjectId("5c5ca7184ef14365b786e11f"),
    "old_arr" : 20,
    "new_arr" : [ 
        10, 
        20, 
        30
    ]
}

{
    "_id" : ObjectId("5c5ca7184ef14365b786e11f"),
    "old_arr" : 30,
    "new_arr" : [ 
        10, 
        20, 
        30
    ]
}

因此old_arr字段是unwinded,但对于每个文档,您将拥有包含所有值的new_arr字段。

MongoDB $ unwind并保留数组字段和每个未解卷的文档以供参考

我想保留每个解开文档的原始数组字段。

示例:在展开band的成员数组时,我想保留原始成员数组和每个doc,以便我可以引用其他成员

{ 'name': 'the crazies', 'member': 'Tom', 'othermembers': [ 'Tom', 'Mike', 'Sally' ] }

{ 'name': 'the crazies', 'member': 'Mike', 'othermembers': [ 'Tom', 'Mike', 'Sally' ] }

{ 'name': 'the crazies', 'member': 'Sally', 'othermembers': [ 'Tom', 'Mike', 'Sally' ] }

最终,成员数组不应该包含已经在“名称”字段中的成员的名称,但如果有人有任何想法,我会采取我可以得到的。

一种有效的方法是通过乐队ID对自己进行$查找,但看起来有点笨拙。

band.aggregate()
  .match( 'whatever criteria' )
  .lookup({ from: 'bands', localField: '_id', foreignField: '_id', as       'othermembers')
  .unwind({ path: 'members')
  .project({
    'name': 1
    'member': '$members.name',
    'othermembers.members.name': 1;
})

思考???

回答如下:

在展开members数组之前,将项目成员数组作为两个新字段,然后展开两个新字段中的任何一个。例如

{
"_id" : ObjectId("5c5ca7184ef14365b786e11f"),
"a" : [ 
    10, 
    20, 
    30
],
"b" : "hello"

}

对于上述数据,我使用以下查询

db.testColl.aggregate([
{$project: {old_arr: "$a", new_arr: "$a"}},
{$unwind: {path: "$old_arr"}}
])

我得到的结果是

    {
    "_id" : ObjectId("5c5ca7184ef14365b786e11f"),
    "old_arr" : 10,
    "new_arr" : [ 
        10, 
        20, 
        30
    ]
}

{
    "_id" : ObjectId("5c5ca7184ef14365b786e11f"),
    "old_arr" : 20,
    "new_arr" : [ 
        10, 
        20, 
        30
    ]
}

{
    "_id" : ObjectId("5c5ca7184ef14365b786e11f"),
    "old_arr" : 30,
    "new_arr" : [ 
        10, 
        20, 
        30
    ]
}

因此old_arr字段是unwinded,但对于每个文档,您将拥有包含所有值的new_arr字段。

发布评论

评论列表 (0)

  1. 暂无评论