java操作mongodb(進階查詢)

來源:互聯網
上載者:User

直接上代碼(依賴代碼請查看之前的部落格):

public void query() {// $or (查詢id等於1或者id等於2的資料)BasicDBObject queryObject = new BasicDBObject().append(QueryOperators.OR,new BasicDBObject[] { new BasicDBObject("id", 1),new BasicDBObject("id", 2) });find(queryObject, "(查詢id等於1或者id等於2的資料)");// $and(查詢id等於10並且name等於10的資料)queryObject = new BasicDBObject().append(QueryOperators.AND,new BasicDBObject[] { new BasicDBObject("id", 10),new BasicDBObject("name", "10") });find(queryObject, "(查詢id等於10並且name等於10的資料)");// $gt(查詢id大於10的資料)queryObject = new BasicDBObject().append("id",new BasicDBObject().append(QueryOperators.GT, 10));find(queryObject, "(查詢id大於10的資料)");// $gte (查詢id大於等於10的資料)queryObject = new BasicDBObject().append("id",new BasicDBObject().append(QueryOperators.GTE, 11));find(queryObject, "(查詢id大於等於11的資料)");// $ltqueryObject = new BasicDBObject().append("id",new BasicDBObject().append(QueryOperators.LT, 2));find(queryObject, "(查詢id小於2的資料)");// $ltequeryObject = new BasicDBObject().append("id",new BasicDBObject().append(QueryOperators.LTE, 2));find(queryObject, "(查詢id小於等於2的資料)");// $inqueryObject = new BasicDBObject().append("id", new BasicDBObject(QueryOperators.IN, new int[] { 1, 2 }));find(queryObject, "(查詢id為1和2的資料)");// $ninqueryObject = new BasicDBObject().append("id", new BasicDBObject(QueryOperators.NIN, new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }));find(queryObject, "(查詢id不為1,2,3,4,5,6,7,8,9的資料)");// 還有很多其他的進階查詢方式可以參見QueryOperators類}public void find(BasicDBObject condition, String str) {System.out.println("================" + str + "==================");DB db = MongoManager.getDB(Constants.DB);DBCollection collection = db.getCollection(Constants.COLLECTION_USER);DBCursor find = collection.find(condition);while (find.hasNext()) {User user = new User();user.parse(find.next());System.out.println(user);}}public static void main(String[] args) {UserDao userDao = new UserDao();userDao.query();}


QueryOperators類源碼:

// QueryOperators.java/** *      Copyright (C) 2010 10gen Inc. * *   Licensed under the Apache License, Version 2.0 (the "License"); *   you may not use this file except in compliance with the License. *   You may obtain a copy of the License at * *      http://www.apache.org/licenses/LICENSE-2.0 * *   Unless required by applicable law or agreed to in writing, software *   distributed under the License is distributed on an "AS IS" BASIS, *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *   See the License for the specific language governing permissions and *   limitations under the License. */package com.mongodb;/** * MongoDB keywords for various query operations * * @author Julson Lim */public class QueryOperators {    public static final String OR = "$or";    public static final String AND = "$and";    public static final String GT = "$gt";    public static final String GTE = "$gte";    public static final String LT = "$lt";    public static final String LTE = "$lte";    public static final String NE = "$ne";    public static final String IN = "$in";    public static final String NIN = "$nin";    public static final String MOD = "$mod";    public static final String ALL = "$all";    public static final String SIZE = "$size";    public static final String EXISTS = "$exists";    public static final String ELEM_MATCH = "$elemMatch";    // (to be implemented in QueryBuilder)    public static final String WHERE = "$where";    public static final String NOR = "$nor";    public static final String TYPE = "$type";    public static final String NOT = "$not";    // geo operators    public static final String WITHIN = "$within";    public static final String NEAR = "$near";    public static final String NEAR_SPHERE = "$nearSphere";    public static final String BOX = "$box";    public static final String CENTER = "$center";    public static final String POLYGON = "$polygon";    public static final String CENTER_SPHERE = "$centerSphere";    // (to be implemented in QueryBuilder)    public static final String MAX_DISTANCE = "$maxDistance";    public static final String UNIQUE_DOCS = "$uniqueDocs";    // meta query operators (to be implemented in QueryBuilder)    public static final String RETURN_KEY = "$returnKey";    public static final String MAX_SCAN = "$maxScan";    public static final String ORDER_BY = "$orderby";    public static final String EXPLAIN =  "$explain";    public static final String SNAPSHOT = "$snapshot";    public static final String MIN = "$min";    public static final String MAX = "$max";    public static final String SHOW_DISK_LOC = "$showDiskLoc";    public static final String HINT = "$hint";    public static final String COMMENT = "$comment";}


 

輸出結果:

================(查詢id等於1或者id等於2的資料)==================id:1,name:jinhui,address:beijingid:2,name:manman,address:beijing================(查詢id等於10並且name等於10的資料)==================================(查詢id大於10的資料)==================id:11,name:11,address:11id:12,name:12,address:12================(查詢id大於等於11的資料)==================id:11,name:11,address:11id:12,name:12,address:12================(查詢id小於2的資料)==================id:1,name:jinhui,address:beijing================(查詢id小於等於2的資料)==================id:1,name:jinhui,address:beijingid:2,name:manman,address:beijing================(查詢id為1和2的資料)==================id:1,name:jinhui,address:beijingid:2,name:manman,address:beijing================(查詢id不為1,2,3,4,5,6,7,8,9的資料)==================id:11,name:11,address:11id:12,name:12,address:12

資料庫資料如下所示:


 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.