2016-02-03 30 views
3

我想通过查询并将结果存储在一个较小的集合中筛选数据收集。但是,使用count()和集合中的数字发现的记录数非常不同(count()要高得多)。难道我做错了什么?Mongodb查找 - >插入和计数有不同的结果

这将返回约1.1亿。

db.getCollection('ex').count({ 
    'data.points': {$exists: true}, 
    'data.points.points': {$exists: false}, 
}, { 
    'data.id': 1, 
    'data.author.id': 1 
}) 

然后我执行此操作。

db.getCollection('ex').find({ 
    'data.points': {$exists: true}, 
    'data.points.points': {$exists: false}, 
}, { 
    'data.id': 1, 
    'data.author.id': 1 
}) 
.forEach(function (doc) { 
    db.s_uid_wid.insert(doc) 
}) 

但是,这只能提供约500万条记录。他们应该完全一样。到底是怎么回事?

db.getCollection('s_uid_wid').count({}) 

编辑

  • 以前我在robomongo GUI正在运行这一点,它给人的印象是一切的一致好评。现在,我在蒙戈外壳尝试这样做,我得到这个

2016-02-04T00:39:21.735+0800 Error: getMore: cursor didn't exist on server, possible restart or timeout? at src/mongo/shell/query.js:116

+0

'count()'没有'投影'参数 –

+0

@AlexBlex,你说的是第一个命令吗?会不会影响计数的结果? –

+1

在.forEach循环中您可能遇到错误,并且它只插入了500万条记录。尝试不带forEach的find(),看看返回的游标的count()是什么给你的。 –

回答

1

下解决该问题。大约需要一天才能完成插入。

db.getCollection('ex').find({ 
    'data.points': {$exists: true}, 
    'data.points.points': {$exists: false}, 
}, { 
    'data.id': 1, 
    'data.author.id': 1 
}).addOption(DBQuery.Option.noTimeout) 
.forEach(function (doc) { 
    db.s_uid_wid.insert(doc) 
})