【问题标题】:Add user id in my profile collection using aggregation使用聚合在我的个人资料集合中添加用户 ID
【发布时间】:2017-04-27 00:02:10
【问题描述】:

下面是我的用户收集数据

用户集合

{
    "_id" : ObjectId("584bc9ba420a6b189c510af6"),
    "old_user_id" :1,
    "name" :"aaa"
},
{
    "_id" : ObjectId("9ba420a584bc6b189c59ba42"),
    "old_user_id" : 2,
     "name" :"bbb"
},
{
    "_id" : ObjectId("59ba4284bc0a6b189c3323w23"),
    "old_user_id" : 3,
    "name" :"ccc"
}

我的个人资料收藏

{        
    "old_user_id" :1,
    "name" :"aaa",
    "number":"123456789"
},
{
    "old_user_id" : 2,
     "name" :"bbb",
     "number":"678912345"
},
{
    "old_user_id" : 3,
    "name" :"ccc",
    "number":"673458912"    
},
{

    "old_user_id" : 2,
    "name" : "bbb",
    "adress" : "afsfdidhddk"
}   

我的期望:

我需要匹配两个集合中的 old_user_id 并更新我的 profile 集合中的 user 集合“_id”

{
    "userid":"584bc9ba420a6b189c510af6",
    "old_user_id" :1,
    "name" :"aaa",
    "number":"123456789"
},
{
    "userid":"9ba420a584bc6b189c59ba42",
    "old_user_id" : 2,
     "name" :"bbb",
     "number":"678912345"
},
{
    "userid":"59ba4284bc0a6b189c3323w23",
    "old_user_id" : 3,
    "name" :"ccc",
    "number":"673458912"
},
    {

    "old_user_id" : 2,
    "name" : "bbb",
    "adress" : "afsfdidhddk"
    "userid" : "9ba420a584bc6b189c59ba42"
}

【问题讨论】:

    标签: mongodb mongodb-query aggregation-framework robo3t


    【解决方案1】:

    您可以使用 mongo 聚合。使用 $lookup、$project 和 $unwind 会有所帮助。 我已经制定了查询,希望这会有所帮助:

    db.myprofile.aggregate([
        {
          $lookup:
            {
              from: "user",
              localField: "old_user_id",
              foreignField: "old_user_id",
              as: "inventory_docs"
            }
       },
       { $project : { _id : "$inventory_docs._id",
                      old_user_id:"$old_user_id",
                     "name":"$name",
                     "number":"$number" } },
       {$unwind: "$_id"}
    ])
    
    【解决方案2】:

    更新:

    db.user.find().forEach(function (user) {
    
        var cursor = db.myprofile.find({"old_user_id":  user.old_user_id});
    
        cursor.forEach(function(myprofile) {
            myprofile.userid = user._id.str;
            db.myprofile.save(myprofile);
        });
    });
    

    结果:

    db.myprofile.find().pretty()
    
       {
        "_id" : ObjectId("584e7294678ae15db4fab039"),
        "old_user_id" : 1,
        "name" : "aaa",
        "number" : "123456789",
        "userid" : "584e7294678ae15db4fab035"
    }
    {
        "_id" : ObjectId("584e7294678ae15db4fab03a"),
        "old_user_id" : 2,
        "name" : "bbb",
        "number" : "678912345",
        "userid" : "584e7294678ae15db4fab036"
    }
    {
        "_id" : ObjectId("584e7294678ae15db4fab03b"),
        "old_user_id" : 3,
        "name" : "ccc",
        "number" : "673458912",
        "userid" : "584e7294678ae15db4fab037"
    }
    {
        "_id" : ObjectId("584e7294678ae15db4fab03c"),
        "old_user_id" : 2,
        "name" : "bbb",
        "adress" : "afsfdidhddk",
        "userid" : "584e7294678ae15db4fab036"
    }
    

    注意:_id 字段仍会出现在生成的文档中,但您应该可以接受。是 MongoDB 的默认行为。您可以创建一个没有索引_id 字段的集合like in here: db.createCollection("user", { autoIndexId: false })

    【讨论】:

    • 检查我的问题,如果我有“old_user_id”:2 多次它不起作用
    • 确实,已更正。现在它应该适用于更多具有相同old_user_id 的文档。
    • 如果与此问题无关,请发布其他问题。
    • 您的下一个问题是什么?请发布另一个问题,如果我能提供帮助,我会的。强悍的 JS 不是我的专长。
    猜你喜欢
    • 2011-05-10
    • 1970-01-01
    • 1970-01-01
    • 2011-03-02
    • 1970-01-01
    • 2019-09-14
    • 2021-02-14
    • 2015-12-23
    • 1970-01-01
    相关资源
    最近更新 更多