Querying data from MongoDB

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.