MongoMapper的复杂查询 参考文档

在使用mongoMapper的时候,会用到一些比较复杂的查询,往往这个时候去看MongoMapper的文档发现并没有这些API的介绍。只是能发现很多人在用,比如
$where
conditions.merge!( :conditions => {'$where' => "/#{options[:searchterm].strip.gsub(/\//, '\/')}/i.test( this.name + ' ' + this.urls.join(' ') )" } )


当然,你不能企图找join什么的方法,原因我就不说了。


实际上,很多复杂的MongoMapper查询没有文档是因为,我们完全可以参考MongoDB的文档来解决。如下是MongoDB的高级查询的文档,相当完善

http://www.mongodb.org/display/DOCS/Advanced+Queries

如下是关于$where使用的介绍

引用
Javascript Expressions and $where
In addition to the structured query syntax shown so far, you may specify query expressions as Javascript. To do so, pass a string containing a Javascript expression to find(), or assign such a string to the query object member $where. The database will evaluate this expression for each object scanned. When the result is true, the object is returned in the query results.

For example, the following statements all do the same thing:


db.myCollection.find( { a : { $gt: 3 } } );
db.myCollection.find( { $where: "this.a > 3" } );
db.myCollection.find("this.a > 3");
f = function() { return this.a > 3; } db.myCollection.find(f);

引用
Javascript executes more slowly than the native operators listed on this page, but is very flexible. See the server-side processing page for more information.


可见,我们只要稍微的转换格式,就是mongomapper的查询了,所以MongoMapper比起ActiveRecord没有特别多API文档

你可能感兴趣的:(JavaScript,mongodb,Ruby,Rails,ActiveRecord)