Javascript 在MongoDB中查找光标大小
是否可以在每个循环中不使用计数器的情况下获取光标的大小?找到了一个合理的解决方案:Javascript 在MongoDB中查找光标大小,javascript,mongodb,Javascript,Mongodb,是否可以在每个循环中不使用计数器的情况下获取光标的大小?找到了一个合理的解决方案: 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)); } }) })
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));
}
})
})
使用toArray和数组的.length属性查找其大小 找到了一个合理的解决方案:
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));
}
})
})
使用toArray和数组的.length属性查找其大小 如果要检查重复项,您可能希望尝试附加到集合的count()方法。例如:
collection.find({description : data.story.description}).toArray(function(err,items){
console.log("Items:" + items.length);
})
collection.count({description:data.story.description},函数(err,count){
如果(计数>1)console.log(“找到重复项!”);
});
Cursor.toArray()只是在后端使用Cursor.each()。我的示例假设您正在检查post-insert(),但您可以使用(count>0)检查pre,如果(count==0)调用insert()。如果您正在检查重复项,您可能希望尝试附加到集合的count()方法。例如:
collection.find({description : data.story.description}).toArray(function(err,items){
console.log("Items:" + items.length);
})
collection.count({description:data.story.description},函数(err,count){
如果(计数>1)console.log(“找到重复项!”);
});
Cursor.toArray()只是在后端使用Cursor.each()。我的示例假设您正在检查post-insert(),但您可以使用(count>0)检查pre,如果(count==0)调用insert()。这将尝试将所有项目加载到内存中,这可能会耗尽可用资源。如果您只需要计数,那么使用另一个答案中的方法。这将尝试将所有项目加载到内存中,这可能会耗尽您的可用资源。如果您只需要计数,那么使用另一个答案中的方法。