mongodb的java driver执行“原生查询”语句

 DBObject o=(DBObject) JSON.parse("{ groups.groupName : 'group1' }");

              DBObject o2=(DBObject) JSON.parse("{ groups.size : 0 }");     

//这个格外的部分是 filed selection,为0你看结果里面就没有这个filed,如果为1则是只有这个filed被返回

DB db = new Mongo().getDB("test");
DBCollection coll = db.getCollection("UserInfo");
DBCursor cur = coll.find(o,o2).slaveOk();   //slaveOk是说在 replica模式下,访问secondary(扶手)需要slaveOk     while(cur.hasNext()){
System.out.println(cur.next());

}

==================result======================================================

{ "_id" : { "$oid" : "4e82e5fe6c7de47b217684a8"} , "className" : "vo.UserInfo" , "email" : "[email protected]" , "groups" : [ { "groupName" : "group1"} , { "groupName" : "group2"}]}
{ "_id" : { "$oid" : "4e82e6566c7d169e3e4873d7"} , "className" : "vo.UserInfo" , "email" : "[email protected]" , "groups" : [ { "groupName" : "group1"} , { "groupName" : "group2"}]}
{ "_id" : { "$oid" : "4e82e6c26c7d1f4ace2dfe29"} , "className" : "vo.UserInfo" , "email" : "[email protected]" , "groups" : [ { "groupName" : "group1"} , { "groupName" : "group2"}]}
{ "_id" : { "$oid" : "4e82e6e96c7d1fb39cf66eb4"} , "className" : "vo.UserInfo" , "email" : "[email protected]" , "groups" : [ { "groupName" : "group1"} , { "groupName" : "group2"}]}
{ "_id" : { "$oid" : "4e82e6fb6c7da5fa87fb8b13"} , "className" : "vo.UserInfo" , "email" : "[email protected]" , "groups" : [ { "groupName" : "group1"} , { "groupName" : "group2"}]}
{ "_id" : { "$oid" : "4e82e74c6c7d653edb297c32"} , "className" : "vo.UserInfo" , "email" : "[email protected]" , "groups" : [ { "groupName" : "group1"} , { "groupName" : "group2"}]}
{ "_id" : { "$oid" : "4e82ea436c7da72043cf2e4c"} , "className" : "vo.UserInfo" , "email" : "[email protected]" , "groups" : [ { "groupName" : "group1"} , { "groupName" : "group2"}]}



你可能感兴趣的:(java,c,mongodb,email)