Springdata integration MongoDB Some methods including Or,and,regex and so on "need to be updated"

Source: Internet
Author: User



These days of contact with MongoDB and Springdata, their own English compared to stamp, so finishing these methods spend a little more time, but also my first time in the foreign site finishing technology



Not much to say, directly on the code, here just give some operation method only, if there is need source code, please q I 206314068, if reproduced please indicate the source


1 package mongodbProject1;
  2 
  3 import java.util.List;
  4
  5 import mg.pojo.User;
  6 import mg.pojo.UserList;
  7 import mg.service.UserService;
  8 
  9 
 10 import org.springframework.context.ApplicationContext;
 11 import org.springframework.context.support.ClassPathXmlApplicationContext;
 12 import org.springframework.data.mongodb.core.MongoTemplate;
 13 import org.springframework.data.mongodb.core.query.Criteria;
 14 import org.springframework.data.mongodb.core.query.CriteriaDefinition;
 15 import org.springframework.data.mongodb.core.query.Query;
 16
 17 import com.mongodb.CommandResult;
 18 import com.mongodb.DBObject;
 19
 20 public class Test {
 21 static ApplicationContext context = null;
 22 static MongoTemplate mongoTemplate = null;
 23 static {
 24 context = new ClassPathXmlApplicationContext ("applicationContext.xml");
 25 mongoTemplate = context.getBean (MongoTemplate.class);
 26}
 27 / **
 28 * Query UserName equal to 123
 29 * where (String n) is (String s)
 30 * /
 31 @ org.junit.Test
 32 public void TestFind () {
 33
 34 Query query = Query.query (
 35 Criteria.where ("UserName"). Is ("123")); // is equivalent to = in the sql statement
 36 DBObject obj = query.getFieldsObject ();
 37 try {
 38 List <User> userlist = mongoTemplate.find (query, User.class);
 39 System.out.println (userlist);
 40} catch (Exception e) {e.printStackTrace ();}
 41
 42}
 43 / **
 44 * all () method is equivalent to and, the function is to query all the records that contain "00" and "lzh" in a field whose type is array or list
 45 * http://docs.mongodb.org/manual/reference/operator/query/all/
 46 * Test data:
 47 * {
 48
 49 Password: "xyz",
 50 UserName: ["school", "book", "bag", "headphone", "appliance"],
 51}
 52 * /
 53 @ org.junit.Test
 54 public void testAll () {
 55 Query query = Query.query (Criteria.where ("UserName"). All ("00", "lzh"));
 56 try {
 57 List <UserList> userlist = mongoTemplate.find (query, UserList.class); System.out.println (userlist);
 58} catch (Exception e) {e.printStackTrace ();}
 59}
 60 / **
 61 * elemMatch () method is used, the database format is as follows
 62 * The query is whether the object properties under the object array match the corresponding values
 63 * The data format is as follows:
 64 * db.inventory.find ({
 65 qty: {$ all: [
 66 {"$ elemMatch": {size: "M", num: {$ gt: 50}}},
 67 {"$ elemMatch": {num: 100, color: "green"}}
 68]}
 69})
 70 * /
 71 @ org.junit.Test
 72 public void testelemMatch () {
 73 Criteria c = new Criteria ();
 74 Query qm = new Query ();
 75 qm.addCriteria (c.elemMatch (Criteria.where ("UserName"). Is ("lzh1"). And ("Password"). Is (100))); // The string in parentheses is the name of the data field
 76 DBObject s = qm.getQueryObject (); // Convert to DBObject to get the string command for more convenient
 77 String n = s.toString ();
 78 Query query = Query.query (Criteria.where ("user"). All (s));
 79 try {
 80 List <UserList> userlist = mongoTemplate.find (query, UserList.class); System.out.println ("list size" + userlist.size () + "\ n" + userlist);
 81} catch (Exception e) {e.printStackTrace ();}
 82}
 83 / **
 84 * and operation, equivalent to and in sql statement
 85 * /
 86 @ org.junit.Test
 87 public void testAnd () {
 88 Query query = Query.query (Criteria.where ("UserName"). Is ("00"). And ("Password"). Is ("123"));
 89 try {
 90
 91 List <User> userlist = mongoTemplate.find (query, User.class); System.out.println ("list size" + userlist.size () + "\ n" + userlist);
 92} catch (Exception e) {e.printStackTrace ();}
 93}
 94 / **
 95 * This method uses the regex () (regular expression) method and or (or) operation to query data
 96 * Equivalent to db.user.find ({"UserName": "00", "$ or": [{"Password": / lz /}]});
 97 * /
 98 @ org.junit.Test
 99 public void testor () {
100
101 try {
102 Criteria c = Criteria.where ("Password"). Regex ("lz"); // The regular expression here is / lzh /
103 Query query = Query.query (Criteria.where ("UserName"). Is ("00"). OrOperator (c));
104
105 List <User> userlist = mongoTemplate.find (query, User.class);
106 System.out.println ("list size" + userlist.size () + "\ n" + userlist);
107} catch (Exception e) {e.printStackTrace ();}
108}
109 / ** Use regular expression query
110 * Criteria.where ("Password"). Regex (re, options); where re and option are all strings,
111 * option can be selected as: i, m, x, s i means not case sensitive, m means that regular expressions such as ^ and $ can be used to identify the start character and character of each line in the database using \ n line feed.
112 * x
113 * Specific original introduction http://docs.mongodb.org/manual/reference/operator/query/regex/
114 * /
115 @ org.junit.Test
116 public void testRegex () {
117
118 try {
119 Criteria c = Criteria.where ("Password"). Regex ("lz", "i"); // The regular expression here is / lzh /
120
121 Query query = Query.query (c);
122
123 List <User> userlist = mongoTemplate.find (query, User.class);
124 System.out.println ("list size" + userlist.size () + "\ n" + userlist);
125} catch (Exception e) {e.printStackTrace ();}
126}
127} 




Springdata Some of the ways to integrate MongoDB include Or,and,regex and so on "pending update"


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.