我正在使用Mongo DB和Java。从Mongo DB光标获取信息
我想知道在Mongo DB中是否存在符号如下所示 这是行得通的,但问题在于它会对MOngo DB进行两次调用,这非常昂贵。 有没有什么办法可以将它减少到一个呼叫,并使其更注重性能。
这就是你为什么要使用count
我所有的代码
public class Test
{
public static void main(String args[])
{
DBCursor cursor = null;
DBCollection coll = null;
BasicDBObject query = new BasicDBObject();
String symbol = args[0];
query.put("symbol", "" + symbol);
cursor = coll.find(query);
int count = coll.find(query).count();
/* Here is want to avoid the count call , is there anyway by which
the cursor the obtained cursor tells , that there exists the symbol
in Mongo DB */
if(count>=1)
{
// If found then do
if (cursor != null) {
}
}
else
{
// If Not found then do
}
}
}
Thanks Rohit,所以你的意思是说cursor.count();不会进行另一个数据库调用?我对吗 ?? – Pawan
@PreethiJain ..是的你是对的。它不会再打电话。游标就像JDBC中的ResultSet一样。您可以通过向前移动光标来访问每条记录,从而耗尽查询返回的整个结果。 –
非常感谢Rohit。 – Pawan