Java Operations MongoDB (advanced query)

Source: Internet
Author: User
Tags mongodb

Go directly to code (dependency code check previous blogs):

public void query () {//$or (data with Query ID equal to 1 or ID equal to 2) basicdbobject queryobject = new Basicdbobject (). Append (Queryoper
		Ators.or, new basicdbobject[] {new basicdbobject ("id", 1), New Basicdbobject ("id", 2)});

		Find (Queryobject, "(data with Query ID equal to 1 or ID equal to 2)"); $and (data with Query ID equal to 10 and name equal to 10) Queryobject = new Basicdbobject (). Append (Queryoperators.and, new basicdbobject[] {NE
		W basicdbobject ("id"), new Basicdbobject ("name", "10")});

		Find (Queryobject (data with Query ID equal to 10 and name equals 10)); $GT (data with query ID greater than 10) Queryobject = new Basicdbobject (). Append ("id", new Basicdbobject (). Append (queryoperators.gt, 10)
		);
		Find (Queryobject, "(data with query ID greater than 10)"); $gte (data with query ID greater than or equal to 10) Queryobject = new Basicdbobject (). Append ("id", new Basicdbobject (). Append (Queryoperators.gte
		, 11));
		Find (Queryobject, "(data with query ID greater than 11)");
		$lt queryobject = new Basicdbobject (). Append ("id", new Basicdbobject (). Append (queryoperators.lt, 2));
		Find (Queryobject, "(data with query ID less than 2)"); //$lte Queryobject = new Basicdbobject (). Append ("id", new Basicdbobject (). Append (Queryoperators.lte, 2));

		Find (Queryobject, "(data with query ID less than 2)");
		$in queryobject = new Basicdbobject (). Append ("id", New Basicdbobject (queryoperators.in, new int[] {1, 2}));
		Find (Queryobject, "(Query ID 1 and 2 data)"); $nin queryobject = new Basicdbobject (). Append ("id", New Basicdbobject (Queryoperators.nin, new int[] {1, 2, 3, 4
		, 5, 6, 7, 8, 9});

		Find (Queryobject, "(data with Query ID not 1,2,3,4,5,6,7,8,9)"); There are many other advanced queries you can see Queryoperators class} 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 class source code:

Queryoperators.java/** * Copyright (C) 10gen Inc.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * You could not use this file, except in compliance with the License. * Obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * unless required 
 By applicable or agreed into writing, software * Distributed under the License is distributed on ' as is ' basis,
 * Without warranties or CONDITIONS of any KIND, either express or implied.
 * 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 is implemented in QueryBuilder) public static final String WHERE = "$where";
    public static final String NOR = "$nor";
    public static final String TYPE = "$type";

    The public static final String is 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 is implemented in QueryBuilder) public static final String max_distance = "$maxDistance";


    public static final String Unique_docs = "$uniqueDocs";
    Meta query operators (to is 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";
 }


Output results:

================ (data with Query ID equal to 1 or ID equal to 2) ==================
id:1,name:jinhui,address:beijing
id:2,name: Manman,address:beijing
================ (data with Query ID equal to 10 and name equal to 10) ==================
================ (data with Query ID greater than 10) ==================
id:11,name:11,address:11
id:12,name:12,address:12
================ (data with a query ID greater than or equal to 11) ==================
id:11,name:11,address:11
id:12,name:12,address:12
============== = = (data with query ID less than 2) ==================
id:1,name:jinhui,address:beijing
================ (data with query ID less than equal to 2) = = = ==============
id:1,name:jinhui,address:beijing
id:2,name:manman,address:beijing
============== = = (data for query ID 1 and 2) ==================
id:1,name:jinhui,address:beijing
id:2,name:manman,address:beijing
================ (data with Query ID not 1,2,3,4,5,6,7,8,9) ==================
id:11,name:11,address:11
id:12, Name:12,address:12

The database data looks like this:


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.