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"));
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"));
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"));
and 和or的联合查询条件 (换成sql 这个查询条件就是 m=1 and (a=0 or a=1) and (b=1 or c=1))
public DBObject getWhereField(){
DBObject wheremap = new BasicDBObject();
wheremap.put("m", 1);
DBObject[] orDbj = new BasicDBObject[2];
orDbj[0] = new BasicDBObject("a", 0);
orDbj[1] = new BasicDBObject("a", 1);
DBObject[] orDbj1 = new BasicDBObject[2];
orDbj1[0] = new BasicDBObject("b", 1);
orDbj1[1] = new BasicDBObject("c", 1);
BasicDBList values = new BasicDBList();
values.add(new BasicDBObject("$or", orDbj));
values.add(new BasicDBObject("$or", orDbj1));
wheremap.put("$and", values);
return wheremap;
转载:AND:[html] view plain copy print?public void testAnd(){ //agender='female' AND age > 27 DBObject queryCondition = new BasicDBObject();
1 //条件 startsAt< curr and endsAt > curr
2 long curr = new Date().getTime()/1000;
3 DBObject query = new BasicDBObject();
4 query.put("start...
DBCollection dbcon = null;
DBObject query = new BasicDBObject();
BasicDBList values = new BasicDBList();
// or用法 --->> 查询薪水salary10000
values.add(new B
JAVA - mongodb OR查询import org.springframework.data.mongodb.core.query.Query;
Criteria criteria = new Criteria();
criteria.orOperator(Criteria.where("status").is(0),
Criteria.where("status").is(1));
//age条件(and条件)
BasicDBList condList = new BasicDBList();
BasicDBObject cond = new BasicDBObject();
cond.put("$gt",0);
cond.put("$lte",40);
在Java中使用MongoDB进行多条件OR查询可以使用MongoDB的$or操作符。$or操作符接受一个数组作为参数,数组中每个元素都是一个条件表达式,只要其中一个条件表达式成立,整个$or条件就成立。
以下是一个示例代码,演示如何在Java中使用MongoDB的$or操作符进行多条件OR查询:
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;
import java.util.ArrayList;
import java.util.List;
public class MongoOrQueryExample {
public static void main(String[] args) {
// 创建MongoDB客户端
MongoClient mongoClient = MongoClients.create("mongodb://localhost:27017");
// 连接到数据库
MongoDatabase database = mongoClient.getDatabase("testdb");
// 获取集合
MongoCollection<Document> collection = database.getCollection("testcollection");
// 创建查询条件列表
List<Document> conditions = new ArrayList<Document>();
// 添加条件表达式
conditions.add(new Document("name", "Alice"));
conditions.add(new Document("age", 25));
// 创建$or条件
Document orCondition = new Document("$or", conditions);
// 执行查询操作
MongoCursor<Document> cursor = collection.find(orCondition).iterator();
// 输出查询结果
while (cursor.hasNext()) {
System.out.println(cursor.next().toJson());
// 关闭游标和MongoDB客户端
cursor.close();
mongoClient.close();
上述代码中,我们首先创建了MongoDB客户端并连接到数据库。然后,我们获取了一个集合,并创建了一个条件列表,其中包含两个条件表达式。接下来,我们使用这个条件列表创建了一个$or条件,并将其作为查询条件传递给find()方法。最后,我们遍历查询结果并关闭游标和MongoDB客户端。
请注意,MongoDB的$or操作符可以与其他操作符一起使用,例如$and和$not。
springcloud 入门第一坑[org.springframework.beans.factory.BeanDefinitionStoreException: Failed to process]
53612
接触qt来第一个坑:Could not create directory "E:\oysl\QT\Error in " Util.asciify("build-untitle
只会抄袭:
接触qt来第一个坑:Could not create directory "E:\oysl\QT\Error in " Util.asciify("build-untitle
W001123456789:
这个问题太恶心了
springcloud 入门第一坑[org.springframework.beans.factory.BeanDefinitionStoreException: Failed to process]
qq_46472778: