2011-10-03 56 views
0
collection.find({description : data.story.description}, function(err, cursor){ 
    cursor.each(function(err, item){ 
    if(item != null){ 
     console.log("duplicate found\n"+util.inspect(item)); 
    } 
    }) 
}) 

是否可以在每個循環中不使用計數器的情況下獲取遊標大小?在MongoDB中查找遊標大小

回答

2

如果您正在檢查重複項,您可能希望嘗試附加到收集的count()方法。例如:

collection.count({description: data.story.description}, function(err, count) { 
    if (count > 1) console.log("Duplicates found!"); 
});

Cursor.toArray()只是在後端使用cursor.each()。我的例子假定你正在檢查post-insert(),但是你可以檢查pre(count> 0)並且調用insert()if(count == 0)。

+0

謝謝,我會試一試! – Jack

1

找到一個合理的解決方案:

collection.find({description : data.story.description}).toArray(function(err,items){ 
    console.log("Items:" + items.length); 
}) 

使用指定者和陣列的。長度屬性來查找它的大小!