Mongodb高级查询
编程语言
2018-05-11 15:05:33
阅读次数: 1
public class MongoDbFind { |
002 |
public static final int port = 27017 ; |
003 |
public static final String host = "127.0.0.1" ; |
009 |
private MongoDatabase getDb() { |
010 |
MongoClient mongo = new MongoClient(host, port); |
011 |
MongoDatabase db = mongo.getDatabase( "new_db" ); |
019 |
private MongoCollection getCollection() { |
020 |
MongoDatabase db = getDb(); |
021 |
MongoCollection<Document> table = db.getCollection( "user" ); |
028 |
public void findOne() { |
029 |
MongoCollection table = getCollection(); |
030 |
BasicDBObject dbObject = new BasicDBObject(); |
032 |
dbObject.put( "age" ,- 1 ); |
033 |
FindIterable iterate = table.find().sort(dbObject).limit( 1 ); |
034 |
System.out.println(iterate.iterator().tryNext()); |
039 |
* @param pageIndex 当前页码 |
041 |
private void findPage( int pageIndex) { |
043 |
MongoCollection table = getCollection(); |
045 |
MongoCursor cursor = table.find().limit(pageSize).skip((pageIndex - 1 ) * pageSize).iterator(); |
046 |
System.out.println( "当前页:" + pageIndex); |
047 |
while (cursor.hasNext()) { |
048 |
System.out.println(cursor.next()); |
052 |
public void findPage() { |
060 |
public void comparison() { |
061 |
MongoCollection table = getCollection(); |
062 |
BasicDBObject dbObject = new BasicDBObject(); |
063 |
dbObject.put( "age" , 20 ); |
065 |
MongoCursor cursor = table.find(dbObject).iterator(); |
066 |
while (cursor.hasNext()) { |
067 |
System.out.println(cursor.next()); |
074 |
public void findIn() { |
076 |
List<Integer> list = new ArrayList<>( 4 ); |
081 |
MongoCollection table = getCollection(); |
082 |
BasicDBObject dbObject = new BasicDBObject(); |
084 |
dbObject.put( "age" , new BasicDBObject( "$in" , list)); |
085 |
MongoCursor cursor = table.find(dbObject).iterator(); |
086 |
while (cursor.hasNext()) { |
087 |
System.out.println(cursor.next()); |
094 |
public void findGtLt() |
096 |
MongoCollection table = getCollection(); |
097 |
BasicDBObject dbObject = new BasicDBObject(); |
099 |
dbObject.put( "age" , new BasicDBObject( "$gt" , 15 ).append( "$lt" , 25 )); |
100 |
MongoCursor cursor = table.find(dbObject).iterator(); |
101 |
while (cursor.hasNext()) { |
102 |
System.out.println(cursor.next()); |
111 |
MongoCollection table = getCollection(); |
112 |
BasicDBObject dbObject = new BasicDBObject(); |
113 |
dbObject.put( "age" , new BasicDBObject( "$ne" , 17 )); |
114 |
MongoCursor cursor = table.find(dbObject).iterator(); |
115 |
while (cursor.hasNext()) { |
116 |
System.out.println(cursor.next()); |
123 |
public void findMulti() |
125 |
MongoCollection table = getCollection(); |
127 |
List<BasicDBObject> objects = new ArrayList<BasicDBObject>(); |
128 |
objects.add( new BasicDBObject( "age" , new BasicDBObject( "$ne" , 17 ))); |
129 |
objects.add( new BasicDBObject( "name" , "zicheng-3" )); |
130 |
BasicDBObject query= new BasicDBObject(); |
131 |
query.put( "$and" ,objects); |
132 |
MongoCursor cursor = table.find(query).iterator(); |
133 |
while (cursor.hasNext()) { |
134 |
System.out.println(cursor.next()); |
141 |
public void findRegex() |
143 |
MongoCollection table = getCollection(); |
144 |
BasicDBObject regexQuery = new BasicDBObject(); |
145 |
regexQuery.put( "name" , new BasicDBObject( "$regex" , "自成.*-[1-8]" )); |
148 |
MongoCursor cursor = table.find(regexQuery).iterator(); |
149 |
while (cursor.hasNext()) { |
150 |
System.out.println(cursor.next()); |
转载自tkggft.iteye.com/blog/2231842