`

Java实现对MongoDB的AND、OR和IN操作

 
阅读更多

很全的JAVA操作mongodb:

http://www.blogjava.net/xiaomage234/archive/2012/08/06/384904.html

 

转的:

 

AND:

public void testAnd(){  
    //agender='female' AND age > 27    
    DBObject queryCondition = new BasicDBObject();  
    queryCondition.put("agender", "female");  
    queryCondition.put("age", new BasicDBObject("$gt", 27));  
    DBCursor dbCursor = coll.find(queryCondition);  
    assertEquals(1, dbCursor.size());  
    assertEquals("Jane", dbCursor.next().get("username"));  
}  

 

OR:

public void testOrSingleField(){  
    DBObject queryCondition = new BasicDBObject();        
    //age<15 OR age>27  
    queryCondition = new BasicDBObject();  
    BasicDBList values = new BasicDBList();  
    values.add(new BasicDBObject("age", new BasicDBObject("$gt", 27)));  
    values.add(new BasicDBObject("age", new BasicDBObject("$lt", 15)));  
    queryCondition.put("$or", values);  
      
    DBCursor dbCursor = coll.find(queryCondition);  
    assertEquals(3, dbCursor.size());  
    assertEquals("tom", dbCursor.next().get("username"));  
}  

 

OR:

public void testOrMultiFields(){  
    DBObject queryCondition = new BasicDBObject();        
    //agender=female OR age<=23  
    queryCondition = new BasicDBObject();  
    BasicDBList values = new BasicDBList();  
    values.add(new BasicDBObject("agender", "female"));  
    values.add(new BasicDBObject("age", new BasicDBObject("$lte", 23)));  
    queryCondition.put("$or", values);  
      
    DBCursor dbCursor = coll.find(queryCondition);  
    assertEquals(4, dbCursor.size());  
    assertEquals("Jim", dbCursor.next().get("username"));  
}

 

IN:

public void testIn(){  
    DBObject queryCondition = new BasicDBObject();        
    //age in [13, 47]  
    queryCondition = new BasicDBObject();  
    BasicDBList values = new BasicDBList();  
    values.add(13);  
    values.add(47);  
    queryCondition.put("age", new BasicDBObject("$in", values));  
      
    DBCursor dbCursor = coll.find(queryCondition);  
    assertEquals(2, dbCursor.size());  
    assertEquals("tom", dbCursor.next().get("username"));  
}  

 

 

 

分享到:
评论

相关推荐

    MongoDB Cookbook 2nd 2016第2版 无水印pdf 0分

    You do not need any prior knowledge of MongoDB, but it would be helpful if you have some programming experience in either Java or Python. What You Will Learn Install, configure, and administer ...

    MongoDB.Cookbook.2nd.Edition.1785289

    You do not need any prior knowledge of MongoDB, but it would be helpful if you have some programming experience in either Java or Python. What You Will Learn Install, configure, and administer ...

    Pentaho Analytics for MongoDB Cookbook(PACKT,2015)

    The variant features in Pentaho for MongoDB are designed to empower organizations to be more agile and scalable and also enables applications to have better flexibility, faster performance, and lower...

    Seven Databases in Seven Weeks

    You’ll need a *nix shell (Mac OSX or Linux preferred, Windows users will need Cygwin), and Java 6 (or greater) and Ruby 1.8.7 (or greater). Each chapter will list the downloads required for that ...

    Pro Couchbase Development(Apress,2015)

    This book is for big data developers who use Couchbase NoSQL database or want to use Couchbase for their web applications as well as for those migrating from other NoSQL databases like MongoDB and ...

    test-driven-java-development-2nd2018

    provided by Java since version 8 and create readable and meaningful tests. Chapter 8, BDD – Working Together with the Whole Team, discusses developing a book store application by using the BDD ...

    Beginning Hibernate: For Hibernate 5

    Get started with the Hibernate 5 persistence layer and gain a clear introduction to the current standard for object-relational persistence in Java. This updated edition includes the new Hibernate 5.0 ...

    mongo2SQL:Mongodb 查询到 sql 查询转换器

    Mongodb 查询到 sql 查询转换器。 示例:在:db.user.find({name: 'julio'})... 翻译器支持以下 mongodb 操作符: $or $and (记住 $and 和对象上的逗号分隔值是相同的) $lt $lte $gt $gte $ne $in 输入文件:input_

    Beginning Spring 5: From Novice to Professional

    You’ll see how Spring has drastically and positively affected the way we program and design applications in Java. Beginning Spring 5 discusses how you can build apps with the Spring mindset and ...

    Beginning.Hibernate.For.Hibernate.5.4th.Edition

    Get started with the Hibernate 5 persistence layer and gain a clear introduction to the current standard for object-relational persistence in Java. This updated edition includes the new Hibernate 5.0 ...

    Learning.Node.js.for..NET.Developers.epub

    This short guide will help you develop applications using JavaScript and Node.js, leverage your existing programming skills from .NET or Java, and make the most of these other platforms through ...

    Beginning Hibernate, 3rd Edition

    Third Edition is ideal if you’re experienced in Java with databases (the traditional, or «connected,» approach), but new to open-source, lightweight Hibernate, a leading object-relational mapping ...

    spring-boot-reference.pdf

    23.8. Using the ApplicationRunner or CommandLineRunner 23.9. Application Exit 23.10. Admin Features 24. Externalized Configuration 24.1. Configuring Random Values 24.2. Accessing Command Line ...

    跨数据库的ORM框架JugglingDB.zip

    JugglingDB 是一个跨数据库的 ORM 框架,提供了访问大多数数据库的通用接口,支持包括:mysql, mongodb, redis, neo4j and js-memory-storage. 你可扩展其他数据库的适配器,支持回调和钩子。 示例代码: var ...

    graylog2使用说明(docker)

    `("ssh login" AND (source:example.org OR source:another.example.org)) OR _exists_:always_find_me` - NOT ``` "ssh login" AND NOT source:example.org NOT example.org ``` 注意: AND, OR, and NOT 只能...

Global site tag (gtag.js) - Google Analytics