【问题标题】:Mongodb find->insert and count has different resultsmongodb find->insert 和 count 有不同的结果
【发布时间】:2016-05-12 21:01:00
【问题描述】:

我正在尝试通过进行查询并将结果存储在较小的集合中来过滤数据集合。但是,使用 count() 找到的记录数量与集合中的数量非常不同(count() 要高得多)。我做错了吗?

这大约返回 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 中运行它,它给人的印象是一切都很好。现在我在 mongo shell 中尝试了这个,我得到了这个

2016-02-04T00:39:21.735+0800 错误:getMore:服务器上不存在游标,可能重新启动或超时?在 src/mongo/shell/query.js:116

【问题讨论】:

  • count() 没有projection 参数
  • @AlexBlex,你说的是第一个命令吗?有它会影响计数的结果吗?
  • 您可能在 .forEach 循环期间遇到错误,它最多只插入了 500 万条记录。试试不带 forEach 的 find(),看看返回的游标的 count() 给你什么。

标签: mongodb


【解决方案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)
})   

【讨论】:

    猜你喜欢
    • 2015-12-16
    • 1970-01-01
    • 1970-01-01
    • 2015-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多