0

一个奇怪的问题:我使用 db.collectionName.count() 打印 dbcursor.count() ,它产生的数字与我从 mongo shell 得到的数字相同。

然而,当我这样做时,

DBCursor cursor = myCollection.find(someQuery);
int count = cursor.count();
int cnt = 0;
while (cursor.hasNext()) {
    cnt++;               
    DBObject dbObject = (DBObject) cursor.next();
} 

之后,count和cnt给出了冲突的结果:前者如我所料,而后者远不如前者。

然后我检查了 mongodb DBCursor API 文档。 http://api.mongodb.org/java/2.0/com/mongodb/DBCursor.html

而从 count() 和 hasNext() 的措辞

public int count()
    throws MongoException

Counts the number of elements in this cursor.  

Returns:
       the number of elements

public boolean hasNext()

Checks if there is another element.

Returns:
    if there is another element

这意味着 count 和 cnt 应该相同。并且只是为了记录,在这个过程中mongodb既没有插入也没有删除操作。(我在镜像数据库上测试只是为了测试)

任何的想法?

4

1 回答 1

0

正如@assylias 评论的那样,这可能是由于索引损坏。

于 2013-05-01T02:57:42.483 回答