Connect to MongoDB:
public class Genericmga{private DB Db;public db Getdb () {return this.db;} public void Setdb (db db) {this.db = db;}}
Dbcollection coll = null;coll=getdb (). GetCollection ("Collname");//name equivalent to MySQL table name when saving
To create a query condition:
Basicdbobject query = new Basicdbobject (); Query.append ("type", type);//"type" queries all data that meet the Type=type criteria for the fields in the table// Query the time between Searchstarttime and the data query.append ("Time", New Basicdbobject ("$gte", Searchstarttime). Append ("$lte", Searchendtime) Query.append ("role", "{$exists: true}");//Query the data that exists for the role field dbcursor cursor = coll.find (query); while ( Cursor.hasnext ()) {Cursor.next ();}
If you only want to query a field or a few fields, you can create one query as another criteria:
Basicdbobject Query1 = new Basicdbobject () query1.append ("User", 1);//indicates that only the user field is queried (the default _id also exists, and if not, Can be set as follows) Query1.append ("_id", 0);//indicates that the _id field is not displayed dbcursor cursor = Coll.find (Query,query1), while (Cursor.hasnext ()) { Cursor.next ();}
Queries the document:db that exist for the role field. Deviceinfo.find ({"role": {$exists: true}})
This article is from the "Java into God's Road" blog, please be sure to keep this source http://yao012.blog.51cto.com/12765256/1921744
Querying data from MongoDB