`

mongoDB中的“or” ,"like"操作

阅读更多

习惯了sql中的一些用法,当然希望mongoDB也能支持,而且mongoDB号称是最像DB的nosql方案了,那么相应的SQL-Mongo的映射如下,

http://www.mongodb.org/display/DOCS/SQL+to+Mongo+Mapping+Chart

 

这里的映射是shell中的例子,是javascript的语法了。

我这边需要java的语法,比较简单的很好实现,然后自己想利用or或者like怎么办呢,看了下QueryBuilder的各个方法,以及对照上面的映射表,总算试出来了。

 

首先介绍简单的like操作,利用正则表达式实现

 Pattern p = Pattern.compile("MongoDB");
 query.put("name",p);

 这里就是查询name中含有MongoDB字符的数据,正则表达式完全是java的语法,对于性能这个没测试过,待以后用到的时候再测试下,这里提示下,记得Pattern p 最好声明为static的,因为就只需要编译一次。

 

其次是or的操作,这个实现完全从映射表转换过来

// x 大于 1002 或者 x 小于 204
BasicDBObject or1 = new BasicDBObject();
or1.put("x", new BasicDBObject("$gt", 1002));

BasicDBObject or2 = new BasicDBObject();
or2.put("x", new BasicDBObject("$lt", 204));
	        
ArrayList<DBObject> list = new ArrayList<DBObject>();
list.add(or1);
list.add(or2);
query.put("$or",list);

//另外一个对y的查询过滤  y > 63
query.put("y", new BasicDBObject("$gt",63));

 以上的mongoDB语法对应到sql就是

where (x > 1002 or x < 204) and y > 63

 

关于group  sum等高级操作,见mongoDB的文档

 

http://www.mongodb.org/display/DOCS/Aggregation

分享到:
评论
1 楼 happy90 2011-04-20  
不错,对我有帮助 

相关推荐

    Practical.MongoDB.Architecting.Developing.and.Administering.MongoDB

    Skills in these areas are in demand, making this book essential reading for those who want to work more productively or break into big data work. It will prove equally useful for entrepreneurs and ...

    Practical MongoDB 无水印破地方 0分

    Skills in these areas are in demand, making this book essential reading for those who want to work more productively or break into big data work. It will prove equally useful for entrepreneurs and ...

    PHP and MongoDB Web Development.pdf

    You will learn unique MongoDB features and solve interesting problems like real-time analytics, location-aware web apps etc. You will be guided to use MongoDB alongside MySQL to build a diverse data ...

    social-platform-donut-backend:甜甜圈API

    Note: You can also use the MongoDB servers like Mlab or MongoDB Cluster server如何在您的机器上设置Redis? 请按照提供的步骤在您的操作系统上安装Redis。如何在本地设置此项目? 移至: ...

    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 ...

    Practical Data Analysis(PACKT,2ed,2016)

    Beyond buzzwords like Big Data or Data Science, there are a great opportunities to innovate in many businesses using data analysis to get data-driven products. Data analysis involves asking many ...

    ES6 for Humans

    of JavaScript platforms like Node.js, front-end frameworks like Angular & React, and complete webstacks like MEAN (MongoDB, ExpressJS, AngularJS and NodeJS). This book discusses the core concepts of ...

    Beginning KeystoneJS(Apress,2016)

    You’ll learn how KeystoneJS makes complicated things simple, without limiting the power or flexibility of node.js or the frameworks it is built on. Beginning KeystoneJS shows you how to build ...

    Practical Data Analysis – Second Edition 2016

    Beyond buzzwords like Big Data or Data Science, there are a great opportunities to innovate in many businesses using data analysis to get data-driven products. Data analysis involves asking many ...

    camel-gridfs:带有Mongodb GridFS的Apache Camel组件

    GridFS Endpoint support the following options, the depending on whether they are acting like a Producer or as a Consumer (options vary based on the consumer type too). 在Spring XML中配置数据库 The ...

    meteor-list-grouper:在您列出的内容中添加小组(标题?)

    // Pass a MongoDB cursor or just a native Array to the collection field collection : WorldCupPlayers . find ( { } , { limit : 100 } ) , // How would you like your data to be grouped? // The group...

    studio-3t-x64-mango.zip

    Whether you’re new to NoSQL or a MongoDB pro, be more productive with Studio 3T features like rich query autocompletion, the ability to write SQL to query MongoDB, automatic code generation, and more...

    Seven Databases in Seven Weeks

    Build clusters of servers using scalable services like Amazon’s Elastic Compute Cloud (EC2). Understand the tradeoffs between consistency and availability, and when you can use them to your ...

    OpenStack Trove(Apress,2015)

    use prepackaged or custom database implementations, and provision and operate a variety of databases - including MySQL, PostgreSQL, MongoDB, Cassandra, and Redis - in development and production ...

    Test-Driving JavaScript Applications: Rapid, Confident, Maintainable Code

    Whether you use Node.js, Express, MongoDB, jQuery, AngularJS, or directly manipulate the DOM, you can test-drive JavaScript. Learn the craft of writing meaningful, deterministic automated tests with ...

    OpenStack.Trove.1484212223

    use prepackaged or custom database implementations, and provision and operate a variety of databases—including MySQL, PostgreSQL, MongoDB, Cassandra, and Redis—in development and production ...

    Hands-On Full Stack Web Development with Aurelia pdf

    If you are a web or a full-stack JavaScript developer who have tried their hands on the traditional stacks like LAMP and wish to explore a new stack with modern web technologies then this book is for ...

    Meteor in Action(Manning,2015)

    Or, like most developers who have tried it, you might just call it awesome. Meteor is a JavaScript-based framework for both client and server web and mobile applications. Meteor applications react to...

    嵌入式文档数据库LinvoDB.zip

    // save is like an insert, except it allows saving existing document too Doc.save([ doc, { a: 55, test: ".save is handy" } ], function(err, docs) {   // docs[0] is doc ...

Global site tag (gtag.js) - Google Analytics