【问题标题】:Why is the while loop in Mongodb inserting the same document several times instead of iterating through the documents?为什么MongoDB中的while循环多次插入同一个文档而不是遍历文档?
【发布时间】:2021-06-21 02:26:38
【问题描述】:

我正在重新设计一个 mongodb 数据库,以便产品的评论包含在产品文档中,而不是评论和产品的单独集合。我创建了一个循环遍历评论并将它们插入到相应的产品文档中。但是,当稍后查询数据库时,我发现如果一个产品有多个评论,而不是遍历评论并插入每一条评论,则第一次评论将重复存在的评论数量。

任何帮助将不胜感激。

更新前的产品示例和审核文件

// product document
{
    "_id" : ObjectId("5d0b6d1cd7367de7f58b4a77"),
    "p2507945_asin" : "B00005JHK9",
    "p2507945_salesRank" : {
        "Clothing" : 2416095
    },
    "p2507945_imUrl" : "http://ecx.images-amazon.com/images/I/413S4T19S8L._SY445_.jpg",
    "p2507945_categories" : [ 
        [ 
            "Clothing, Shoes & Jewelry", 
            "Boys"
        ], 
        [ 
            "Clothing, Shoes & Jewelry", 
            "Novelty, Costumes & More", 
            "Costumes & Accessories", 
            "Costumes", 
            "Kids & Baby", 
            "Boys"
        ]
    ],
    "p2507945_title" : "Gladiator Costume: Boy's Size 7-8"
}

// Review documents for product
{
    "_id" : ObjectId("5d0b70f2d7367de7f5fa1da3"),
    "p2507945_reviewerID" : "A2Y0II6CM888FO",
    "p2507945_asin" : "B00005JHK9",
    "p2507945_reviewerName" : "BAB \"BAB\"",
    "p2507945_helpful" : [ 
        3, 
        4
    ],
    "p2507945_reviewText" : "Looks cool but the plastic is very thin on the chest shield, leg and arm guards. The face plate velcroes on as does the shoulder pads which pop off too easily! Every move my son made they were coming apart! Too many pieces for an active boy to keep in place for any length of time! This costume is cheaply made for the price!We were very disappointed in it!!!",
    "p2507945_overall" : 1.0,
    "p2507945_summary" : "Gladiator Costume",
    "p2507945_unixReviewTime" : 1064966400,
    "p2507945_reviewTime" : "10 1, 2003"
}
/* 2 */
{
    "_id" : ObjectId("5d0b70f2d7367de7f5fa1da4"),
    "p2507945_reviewerID" : "A2PIV63VGDZBFX",
    "p2507945_asin" : "B00005JHK9",
    "p2507945_reviewerName" : "\"dauph08\"",
    "p2507945_helpful" : [ 
        12, 
        13
    ],
    "p2507945_reviewText" : "Overall, this is a neat costume for the money and my son thinks it's pretty cool. I think there are 2 things that could have been done differently though:1. The shoulder pads fall off easily as they need to make either more velcro to hold them on or else find a different way to attach the shoulder pads (which I'm in the process of doing).2. My son was pretty disappointed that the sword pictured didn't come with the costume. For their cost of an extra buck at the most, the company should really have the matching sword included.So really, these are minor things that can be fixed and the costume is really neat and also fun for kids to play with, but sometimes it's nice to buy something without having to \"fix\" it :)",
    "p2507945_overall" : 5.0,
    "p2507945_summary" : "Not bad for the money",
    "p2507945_unixReviewTime" : 1033603200,
    "p2507945_reviewTime" : "10 3, 2002"
}

要更新的代码

var RvColcursor =
db.p250794_reviews.find({}, {_id : 0});
while (RvColcursor.hasNext()) {
  var nextRVDoc = RvColcursor.next();
  var ProductID = nextRVDoc.p2507945_asin;
  var RvDoccursor = db.p2507945_reviews.find({p2507945_asin : ProductID}, {_id : 0, p2507945_asin : 0});
  var nextRVDocUp = RvDoccursor.next();
  db.p2507945_products.update({p2507945_asin : ProductID}, {$push : {Reviews : nextRVDocUp}});
}

返回的示例

{
    "_id" : ObjectId("5d0b6d1cd7367de7f58b4a77"),
    "p2507945_asin" : "B00005JHK9",
    "p2507945_salesRank" : {
        "Clothing" : 2416095
    },
    "p2507945_imUrl" : "http://ecx.images-amazon.com/images/I/413S4T19S8L._SY445_.jpg",
    "p2507945_categories" : [ 
        [ 
            "Clothing, Shoes & Jewelry", 
            "Boys"
        ], 
        [ 
            "Clothing, Shoes & Jewelry", 
            "Novelty, Costumes & More", 
            "Costumes & Accessories", 
            "Costumes", 
            "Kids & Baby", 
            "Boys"
        ]
    ],
    "p2507945_title" : "Gladiator Costume: Boy's Size 7-8",
    "Reviews" : [ 
        {
            "p2507945_reviewerID" : "A2Y0II6CM888FO",
            "p2507945_reviewerName" : "BAB \"BAB\"",
            "p2507945_helpful" : [ 
                3, 
                4
            ],
            "p2507945_reviewText" : "Looks cool but the plastic is very thin on the chest shield, leg and arm guards. The face plate velcroes on as does the shoulder pads which pop off too easily! Every move my son made they were coming apart! Too many pieces for an active boy to keep in place for any length of time! This costume is cheaply made for the price!We were very disappointed in it!!!",
            "p2507945_overall" : 1.0,
            "p2507945_summary" : "Gladiator Costume",
            "p2507945_unixReviewTime" : 1064966400,
            "p2507945_reviewTime" : "10 1, 2003"
        }, 
        {
            "p2507945_reviewerID" : "A2Y0II6CM888FO",
            "p2507945_reviewerName" : "BAB \"BAB\"",
            "p2507945_helpful" : [ 
                3, 
                4
            ],
            "p2507945_reviewText" : "Looks cool but the plastic is very thin on the chest shield, leg and arm guards. The face plate velcroes on as does the shoulder pads which pop off too easily! Every move my son made they were coming apart! Too many pieces for an active boy to keep in place for any length of time! This costume is cheaply made for the price!We were very disappointed in it!!!",
            "p2507945_overall" : 1.0,
            "p2507945_summary" : "Gladiator Costume",
            "p2507945_unixReviewTime" : 1064966400,
            "p2507945_reviewTime" : "10 1, 2003"
        }
    ]
}

【问题讨论】:

    标签: javascript mongodb while-loop


    【解决方案1】:

    这是代码中的逻辑错误。

    这使您可以将光标放在p250794_reviews 集合中的所有文档上

    var RvColcursor =
    db.p250794_reviews.find({}, {_id : 0});
    

    这会迭代光标,并从每个文档中选择productId

    while (RvColcursor.hasNext()) {
      var nextRVDoc = RvColcursor.next();
      var ProductID = nextRVDoc.p2507945_asin;
    

    这将重新查询 p250794_reviews 集合,并检索引用 ProductId 的第一个评论

      var RvDoccursor = db.p2507945_reviews.find({p2507945_asin : ProductID}, {_id : 0, p2507945_asin : 0});
      var nextRVDocUp = RvDoccursor.next();
    

    然后将其推送到评论数组中

      db.p2507945_products.update({p2507945_asin : ProductID}, {$push : {Reviews : nextRVDocUp}});
    }
    

    基本上,您完全忽略了光标返回的评论,并重复获取和推送每个文档的第一个评论。

    【讨论】:

      猜你喜欢
      • 2013-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-29
      相关资源
      最近更新 更多