mongodborjava
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"}]}