Mogodb connection test: mogodb connection
-$ In
db.Student.find({"_id":{"$in":[1001,1002,1]}})
-$ Or
db.Student.find({"$or":[{"_id":99},{"pageViews":2}]})
-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);});
-Paging Query
db.Student.find().limit(3) db.Student.find().limit(3).skip(3)
-Sort
Db. student. find (). sort ({"_ id":-1}) -- 1 is in ascending order, and-1 is in descending order. student. find (). sort ({"_ id":-1, "pageView": 1}) -- sort by the ascending order of pageView
DBHelper:
Package com. yc. link; import java.net. unknownHostException; import com. mongodb. DB; import com. mongodb. DBCollection; import com. mongodb. mongo; import com. mongodb. unknown exception; public class DBHelper {private static Mongo mongo = null; private DB db = null; static {try {mongo = new Mongo (Mypro. getInstance (). getProperty ("ip"), Integer. parseInt (Mypro. getInstance (). getProperty ("port");} catch (NumberFormatExcep Tion e) {e. printStackTrace ();} catch (UnknownHostException e) {e. printStackTrace ();} catch (except exception e) {// TODO Auto-generated catch block e. 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 {// check whether the user name and password are specified in the configuration file String 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 ();}} /*** get the database * User Name of the linked data * password of the Linked database */public DBCollection getDBCollection (String collectionName, string dbName) {DBCollection dbCollection = null; if (getDB (dbName, null, null) {db. requestStart (); if (collectionName = null) {// if it is null, load collectionName = Mypro in the configuration file. getInstance (). getProperty (collectionName);} dbCollection = db. getCollection (collectionName);} else {throw new RuntimeException ("database connection failed ..... ");} return dbCollection;} public DBCollection getDBCollection (String collectionName, String dbName, String name, String password) {DBCollection dbCollection = null; if (getDB (dbName, name, password) {db. requestStart (); if (collectionName = null) {// if it is null, load collectionName = Mypro in the configuration file. getInstance (). getProperty (collectionName);} dbCollection = db. getCollection (collectionName);} else {throw new RuntimeException ("database connection failed ..... ") ;}return dbCollection ;}}
Mypro: Singleton
package com.yc.link;import java.io.IOException;import java.io.InputStream;import java.util.Properties;public class Mypro extends Properties { private static Mypro instance=new Mypro(); 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 static Mypro getInstance(){ return instance; }}
package com.yc.link;public class Test { public static void main(String[] args) { DBHelper db=new DBHelper(); System.out.println(db); }}
Db. properties
ip=127.0.0.1port=27017dbName=uname=password=collectionName=
DBHelper should encapsulate more common methods. You can collect them online.
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.