1. 關於mongoDB的$or 怎麼用java實現

在mongodb中有$or 操作符的,官網中給出的內例子如容下
Simple:
db.foo.find( { $or : [ { a : 1 } , { b : 2 } ] } )
With another field

db.foo.find( { name : "bob" , $or : [ { a : 1 } , { b : 2 } ] } )

2. java solr怎麼使用查詢mongodb

1- When inserting an object in MongoDB, I then add it to Solr
SolrServer server = getServer();
SolrInputDocument document = new SolrInputDocument();
document.addField("id", documentId);
...
server.add(document);
server.commit();
2- When updating a property of the object, since Solr cannot update just one field, first I retrieve the object from MongoDB then I update the Solr index with all properties from object and new ones and do something like
StreamingUpdateSolrServer update = new StreamingUpdateSolrServer(url, 1, 0);
SolrInputDocument document = new SolrInputDocument();
document.addField("id", documentId);
...
update.add(document);
update.commit();
3- When querying, first I query Solr and then when retrieving the list of documents SolrDocumentList I go through each document and:
get the id of the document
get the object from MongoDB having the same id to be able to retrieve the properties from there

4- When deleting
server.deleteById("1");
OR:
List<String> ids = new ArrayList<String>();
ids.add("2");
ids.add("3");
server.deleteById(ids);
OR:
server.deleteByQuery("id:4 id:6");

then you can delete the object from MongoDB

3. 怎麼使用java操作mongodb更新整個文檔

上篇博客介紹了java操作mongoDB進行對文件的處理。現在來介紹一下對文檔的處理。和對文件的處理一樣,也是通過java驅動中提供的幾個類相互作用完成的。這幾個類分別是:
DBCollection類:指定資料庫中指定集合的實例,提供了增刪改查等一系列操作。在關系型資料庫中,對數據的增刪改查操作是建立在表的基礎上的,在mongodb中是建立在集合的基礎上進行的。
DBObject介面:DBObject是鍵值的映射,因此,可以將DBObject的實現類作為查詢的返回結果,也可以作為查詢條件
DBCursor:游標,返回結果的集合。
下面是部分實例:

[java] view plain
Mongo mongo = new Mongo();
DB db = mongo.getDB("myMongoDB");
DBCollection course = db.getCollection("course");//對myMongoDB資料庫中course集合進行操作

//添加操作
//下面分別是創建文檔的幾種方式:1. .append() 2. .put() 3. 通過map 4. 將json轉換成DBObject對象
DBObject english = new BasicDBObject().append("name","english").append("score", 5).append("id",1);
course.insert(english);

DBObject math = new BasicDBObject();
math.put("id", 2);
math.put("name", "math");
math.put("score", 10);
course.insert(math);

Map<String,Object> map = new HashMap<String,Object>();
map.put("name","physics" );
map.put("score", 10);
map.put("id", 3);
DBObject physics= new BasicDBObject(map);
course.insert(physics);

String json ="{'name':'chemistry','score':10,'id':4}";
DBObject chemistry =(DBObject)JSON.parse(json);
course.insert(chemistry);

List<DBObject> courseList = new ArrayList<DBObject>();
DBObject chinese = new BasicDBObject().append("name","chinese").append("score", 10).append("id", 5);
DBObject history = new BasicDBObject().append("name", "history").append("score", 10).append("id", 6);
courseList.add(chinese);
courseList.add(history);
course.insert(courseList);

//添加內嵌文檔
String json2 =" {'name':'english','score':10,'teacher':[{'name':'柳松','id':'1'},{'name':'柳鬆鬆','id':2}]}";
DBObject english2= (DBObject)JSON.parse(json);
course.insert(english2);

List<DBObject> list = new ArrayList<DBObject>();
list.add(new BasicDBObject("name","柳松").append("id",1));
list.add(new BasicDBObject("name","柳鬆鬆").append("id",2));
DBObject english3= new BasicDBObject().append("name","english").append("score",10).append("teacher",list);

//查詢
//查詢所有、查詢一個文檔、條件查詢
DBCursor cur = course.find();
while(cur.hasNext()){
DBObject document = cur.next();
System.out.println(document.get("name"));
}

DBObject document = course.findOne();
String name=(String)document.get("name");
System.out.println(name);

//查詢學分=5的
DBObject query1 = new BasicDBObject("score",5);
DBObject query2 = new BasicDBObject("score",new BasicDBObject("$gte",5));
DBCursor cur2 = course.find(query2);
//條件表達式:$ge(>) $get(>=) $lt(<) $lte(<=) $ne(<>) $in $nin $all $exists $or $nor $where $type等等

//查找並修改
DBObject newDocument = course.findAndModify(new BasicDBObject("score",5), new BasicDBObject("score",15));

//更新操作
//q:更新條件 o:更新後的對象
course.update(new BasicDBObject("score",10), new BasicDBObject("test",15));
course.update(new BasicDBObject("score",15), new BasicDBObject("$set",new BasicDBObject("isRequired",true)));
//兩個的區別是,第一個更新是將{"test":15}這個文檔替換原來的文檔,
//第二個更新添加了條件表達式$set,是在原來文檔的基礎上添加"isRequired"這個鍵
//條件表達式:$set $unset $push $inc $push $push $addToSet $pull $pullAll $pop等等

//當_id相同時,執行save方法相當於更新操作
course.save(new BasicDBObject("name","math").append("_id", 1));
course.save(new BasicDBObject("name","數學").append("_id", 1));

//刪除符合條件的文檔
course.remove(new BasicDBObject("score",15));

//刪除集合及所有文檔
course.drop();<span style="font-family:Arial, Helvetica, sans-serif;"><span style="white-space: normal;">
</span></span>

上面只是介紹了一些簡單的操作,具體復雜的查詢更新可以根據需求再去查找文檔資料。其實,不管操作簡單還是復雜,其核心都是對DBObject和DBCollection的操作,主要掌握DBObject如何構造鍵值對,以及一些條件表達式。

4. 關於mongoDB的$or 怎麼用java實現

public static void selectAll() throws Exception{ //第一:實例化mongo對象,連接mongodb伺服器 包含所有的資料庫 //默認構造方法,默認是連接本機,埠號,默認是27017 //相當於Mongo mongo =new Mongo("localhost",27017) Mongo mongo =new Mongo(); //第二:連接具體的資料庫 //其中參數是具體資料庫的名稱,若伺服器中不存在,會自動創建 DB db=mongo.getDB("myMongo"); //第三:操作具體的表 //在mongodb中沒有表的概念,而是指集合 //其中參數是資料庫中表,若不存在,會自動創建 DBCollection collection=db.getCollection("user"); BasicDBList condList = new BasicDBList(); BasicDBObject cond = null; String ageStr = "function (){return parseFloat(this.id) > 2 && parseFloat(this.id) <= 4};"; cond = new BasicDBObject(); cond.put("$where",ageStr); Pattern pattern = Pattern.compile("^.*明.*$", Pattern.CASE_INSENSITIVE); BasicDBObject query =new BasicDBObject(); query.put("name", pattern); condList.add(query); condList.add(cond); BasicDBObject searchCond = new BasicDBObject(); searchCond.put("$or", condList); //查詢操作 DBCursor cursor=collection.find(searchCond); System.out.println("mongodb中的user表結果如下:"); while(cursor.hasNext()){ System.out.println(cursor.next()); } }

5. 關於mongoDB的$or 怎麼用java實現

coll.find(new BasicDBObject("age", new BasicDBObject(QueryOperators.OR, new int[] { 25, 26, 27 }))).toArray();

6. 關於mongoDB的$or 怎麼用java實現

public static void selectAll() throws Exception{
//第一:實例化mongo對象,連接mongodb伺服器 包含所有的資料庫

//默認構造方法,默認是連接本機,埠號,默認是27017
//相當於 mongo =new Mongo("localhost",27017)
Mongo mongo =new Mongo();

//第二:連接具體的資料庫
//其中參數是具體資料庫的名稱,若伺服器中不存在,會自動創建
DB db=mongo.getDB("myMongo");

//第三:操作具體的表
//在mongodb中沒有表的概念,而是指集合
//其中參數是資料庫中表,若不存在,會自動創建
DBCollection collection=db.getCollection("user");
BasicDBList condList = new BasicDBList();
BasicDBObject cond = null;

String ageStr = "function (){return parseFloat(this.id) > 2 && parseFloat(this.id) <= 4};";
cond = new BasicDBObject();
cond.put("$where",ageStr);

Pattern pattern = Pattern.compile("^.*明.*$", Pattern.CASE_INSENSITIVE);
BasicDBObject query =new BasicDBObject();
query.put("name", pattern);

condList.add(query);
condList.add(cond);
BasicDBObject searchCond = new BasicDBObject();
searchCond.put("$or", condList);

//查詢操作

DBCursor cursor=collection.find(searchCond);

System.out.println("mongodb中的user表結果如下:");
while(cursor.hasNext()){
System.out.println(cursor.next());
}
}

7. 關於mongoDB的$or 怎麼用java實現

舉例:
public static void selectAll() throws Exception{
//第一:實例化mongo對象,連接mongodb伺服器 包含所有的資料庫

//默認構造方法,默認是連接本機,埠號,默認是27017
//相當於Mongo mongo =new Mongo("localhost",27017)
Mongo mongo =new Mongo();

//第二:連接具體的資料庫
//其中參數是具體資料庫的名稱,若伺服器中不存在,會自動創建
DB db=mongo.getDB("myMongo");

//第三:操作具體的表
//在mongodb中沒有表的概念,而是指集合
//其中參數是資料庫中表,若不存在,會自動創建
DBCollection collection=db.getCollection("user");
BasicDBList condList = new BasicDBList();
BasicDBObject cond = null;

String ageStr = "function (){return parseFloat(this.id) > 2 && parseFloat(this.id) <= 4};";
cond = new BasicDBObject();
cond.put("$where",ageStr);

Pattern pattern = Pattern.compile("^.*明.*$", Pattern.CASE_INSENSITIVE);
BasicDBObject query =new BasicDBObject();
query.put("name", pattern);

condList.add(query);
condList.add(cond);
BasicDBObject searchCond = new BasicDBObject();
searchCond.put("$or", condList);

//查詢操作

DBCursor cursor=collection.find(searchCond);

System.out.println("mongodb中的user表結果如下:");
while(cursor.hasNext()){
System.out.println(cursor.next());
}
}

8. 怎麼將mongodb的語句直接轉成java里的mongodb語句

MongoClient mongoClient=new MongoClient("localhost",27017);//連接資料庫
MongoDatabase database=mongoClient.getDatabase("db");//獲取資料庫
MongoCollection<Document> collection=database.getCollection("集合");//獲取集合
System.out.println("請輸入需要查詢的欄位:");
Scanner scanner=new Scanner(System.in);
String j=scanner.next();
FindIterable<Document> docs= collection.find(Filters.eq("欄位",j);//查詢結果

9. 關於mongoDB的$or 怎麼用java實現

可以這樣寫:
"Type": {"$in" : ["1","2"]}
如果是不同欄位的話就這樣寫:
{"$or":[{"age":16},{"name":"xing"}]}