– $in
db.Student.find({"_id":{"$in":[1001,1002,1]}})
– $or
db.Student.find({"$or":[{"_id":99},{"pageViews":2}]})
–and and OR
db.Student.find({"pageViews":2,"$or":[{"_id":99}]})
Cursor control
for(i=0;i<10;i++){ db.Student.insert({"_id":i});}var cursor=db.Student.find();cursor.forEach(function(x){ print(x);});
– Paged Query
db.Student.find().limit(3) db.Student.find().limit(3).skip(3)
– Sorting
db.Student.find().sort({"_id":-1}) --1为升序,-1为降序db.Student.find().sort({"_id":-1,"pageView":1}) --前面如果相同就按后面pageView的升序来排序
DBHelper:
PackageCom.yc.link;ImportJava.net.UnknownHostException;ImportCom.mongodb.DB;ImportCom.mongodb.DBCollection;ImportCom.mongodb.Mongo;ImportCom.mongodb.MongoException; Public class dbhelper { Private StaticMongo mongo=NULL;PrivateDB db=NULL;Static{Try{mongo=NewMongo (Mypro.getinstance (). GetProperty ("IP"), Integer.parseint (Mypro.getinstance (). GetProperty ("Port"))); }Catch(NumberFormatException e) {E.printstacktrace (); }Catch(Unknownhostexception e) {E.printstacktrace (); }Catch(Mongoexception e) {//TODO auto-generated catch blockE.printstacktrace (); } } Public Boolean Getdb(String dbname,string name,string pwd) {Mongo.getdb (dbName);if(name!=NULL&&!"". Equals (name) &&pwd!=NULL&&!"". Equals (PWD) {if(Db.authenticate (name, Pwd.tochararray ())) {return true; }Else{return false; } }Else{//See if a user name and password are given in the configuration fileString uname=mypro.getinstance (). GetProperty ("Uname"); String pwds=mypro.getinstance (). GetProperty ("Password");if(name!=NULL&&!"". Equals (name) &&pwd!=NULL&&!"". Equals (PWD) {if(Db.authenticate (name, Pwd.tochararray ())) {return true; }Else{return false; } } }return true;}/** * Close link * */ Public void CloseAll(Mongo Mongo) {if(mongo!=NULL) {mongo.close (); }}/** * Gets the specified collection * CollectionName the collection to link * dbname the database where the collection resides * The username of the linked data * The password of the linked database * */ PublicDbcollectiongetdbcollection(String collectionname,string DbName) {dbcollection dbcollection=NULL;if(Getdb (DbName,NULL,NULL)) {Db.requeststart ();if(collectionname==NULL){//If empty, the flush configuration file is loadedCollectionname=mypro.getinstance (). GetProperty (CollectionName); } dbcollection= db.getcollection (CollectionName); }Else{Throw NewRuntimeException ("Database connection failed ..."); }returnDbcollection;} PublicDbcollectiongetdbcollection(String collectionname,string dbname,string name,string password) {dbcollection dbcollection=NULL;if(Getdb (Dbname,name,password)) {Db.requeststart ();if(collectionname==NULL){//If empty, the flush configuration file is loadedCollectionname=mypro.getinstance (). GetProperty (CollectionName); } dbcollection= db.getcollection (CollectionName); }Else{Throw NewRuntimeException ("Database connection failed ..."); }returnDbcollection;}}
Mypro: Single case
PackageCom.yc.link;ImportJava.io.IOException;ImportJava.io.InputStream;ImportJava.util.Properties; Public class mypro extends Properties { Private StaticMypro instance=NewMypro ();Private Mypro() {InputStream Is=mypro.class.getresourceasstream ("/db.properties");Try{ This. Load (IS); }Catch(IOException e) {E.printstacktrace (); }finally{if(is!=NULL){Try{Is.close (); }Catch(IOException e) {E.printstacktrace (); } } } } Public StaticMyprogetinstance(){returnInstance }}
package com.yc.link;publicclass Test { publicstaticvoidmain(String[] args) { DBHelper db=new DBHelper(); System.out.println(db); }}
Db.properties
ip=127.0.0.1port=27017dbName=uname=password=collectionName=
DBHelper should be more in the encapsulation of some common methods, you can go to the internet slowly collect
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
MOGODB Connection Test