Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Query data from mongodb

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Connect to the 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"); / / the save name is equivalent to the table name of MySQL

Create query criteria:

BasicDBObject query = new BasicDBObject (); query.append ("type", type); / / "type" queries all data that meets the type=type criteria for the fields in the table / / queries the data query.append ("time", new BasicDBObject ("$gte", searchStartTime) .append ("$lte", searchEndTime)) between time and the fields in the table ("time", new BasicDBObject ("$gte", searchStartTime) .append ("$lte", searchEndTime); query.append ("role", "{$exists:true}"); / / queries the data DBCursor cursor = coll.find (query) that exists in the role field While (cursor.hasNext ()) {cursor.next ();}

If you only want to query a field or fields, you can create a query as another query condition:

BasicDBObject query1 = new BasicDBObject (); query1.append ("user", 1); / / means only user is queried (default _ id also exists. If you don't want it, you can set it as follows) query1.append ("_ id", 0); / / does not show the _ id field DBCursor cursor = coll.find (query,query1); while (cursor.hasNext ()) {cursor.next ();}

Query the document: db.DeviceInfo.find that exists in the role field ({"role": {$exists: true}})

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report