习惯了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
分享到:
相关推荐
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 ...
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 ...
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 ...
Note: You can also use the MongoDB servers like Mlab or MongoDB Cluster server如何在您的机器上设置Redis? 请按照提供的步骤在您的操作系统上安装Redis。如何在本地设置此项目? 移至: ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
// 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...
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...
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 ...
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 ...
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 ...
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 ...
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 ...
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...
// 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 ...