Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongodb find->insert and count has different results

Tags:

mongodb

I am trying to filter a data collection by making a query and storing the results in a smaller collection. However, the number of records that were found using count() and the number in the collection are very different(count() is much higher). Am I doing something wrong?

This returns about 110 million.

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

Then I execute this.

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)
})

But this only gives about 5 million records. They should be exactly the same. What is going on?

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

Edit

  • Previously I was running this in robomongo gui and it gave the impression that everything was well. Now I tried this in mongo shell and I got this

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

like image 966
Zaw Lin Avatar asked Nov 27 '25 04:11

Zaw Lin


1 Answers

The following fixes the problem. It takes about one day to complete the insertion.

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)
})   
like image 150
Zaw Lin Avatar answered Dec 08 '25 14:12

Zaw Lin