1. 程式人生 > >mongodb的java driver執行“原生查詢”語句

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被返回

DBdb = 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"}]}