我正在使用带有 Java 的 Mongo DB。
我试图找出在 Mongo DB 中是否存在具有给定字符串的符号,如下所示。这是有效的,但问题是它对 MOngo DB 进行了两次调用,这非常昂贵。有什么办法可以将它减少到一个电话并使其更加注重性能。
这是我的代码
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
}
}
}