为什么会这样db.eval - >为的Array.push某些记录执行两次? [英] Why would this db.eval -> array.push execute twice for certain records?

查看:269
本文介绍了为什么会这样db.eval - >为的Array.push某些记录执行两次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想添加一个对象元素匹配一个简单的选择记录阵列的,所以我跑这个查询:

I wanted to add an object element to arrays in records matching a simple selector, so I ran this query:

db.eval(function() { 
    db.companies.find({exchange: 'OTC'}).forEach(function(c) {
          c.links.push({
            url: 'http://www.otcmarkets.com/stock/' + c.symbol + '/profile',
            text: 'OTCMarkets.com'
          });
          db.companies.save(c);
    });
});

在对10000条记录匹配选择,约1100结束了的两个的链接推到数组的结尾。什么能可能会导致?关于1000有一个空的链接数组,但约100人之所以阵列中的一个或多个元素:

Out of about 10,000 records that matched the selector, about 1100 ended up with two links pushed at the end of the array. What can possibly cause that? About 1000 had an empty links array, but ~100 others had one or more elements in the array:

{
"_id": "...",
...
"exchange": "OTC",
"links": [
    {
        "text": "Website",
        "url": "..."
    },
    {
        "text": "OTCMarkets.com",
        "url": "http://www.otcmarkets.com/stock/GREN/profile"
    },
    {
        "text": "OTCMarkets.com",
        "url": "http://www.otcmarkets.com/stock/GREN/profile"
    }
],
"name": "GreenSmart Corp.",
...
}

大多数其他记录正确只拿到一个数组元素推动:

Most other records correctly got only one array element pushed:

{
"_id": "6WiXSoefPtqJdmzxa",
...
"exchange": "OTC",
...
"links": [
    {
        "text": "Website",
        "url": "..."
    },
    {
        "text": "Financial Information",
        "url": "..."
    },
    {
        "text": "Executives",
        "url": "..."
    },
    {
        "text": "OTCMarkets.com",
        "url": "http://www.otcmarkets.com/stock/SFEF/profile"
    }
],
"name": "Santa Fe Financial Corp.",
...
}

我已经从简洁备案省略字段,但我不能完全理解为什么有些会得到OTCMarkets link元素推一次,而其他人的两倍。

I have omitted fields from the record for brevity, but I can't fathom why some would get the OTCMarkets link element pushed once, while others twice.

蒙戈2.6.3。

推荐答案

您的问题得到劫持了一点边讨论,但我想在这里解决我的失误答案,以免误导你或其他人。在 $隔离运营商是不会帮你,因为它是你自己的更新,这是造成文件移动(因为它们改变大小),然后用收集扫描再次重新发现。作为Sammaye建议使用排序。

You question got hijacked for a bit of a side discussion, but I wanted to fix my mistakes here in an answer so as not to mislead you or anyone else. The $isolated operator is not going to help you as it is your own update which is causing documents to move (because they change size) and then be rediscovered again with the collection scan. Use a sort as Sammaye suggests.

另外,请不要使用 $隔离,除非你真的确定你需要一个孤立的更新;也不要忘记,一个 $隔离更新,而孤立的,不是原子的。

Also, please don't use $isolated unless you're really sure you need an isolated update; also don't forget that an $isolated update, while isolated, is not atomic.

最后,请不要使用db.eval。它有一堆的文档讨论限制的,其目的是由聚合框架来代替,做> 1调用数据库。

Finally, please don't use db.eval. It has a bunch of limitations discussed in its docs and is meant to be replaced by the aggregation framework and doing > 1 call to the database.

这篇关于为什么会这样db.eval - >为的Array.push某些记录执行两次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆