MongoMapper的复杂查询 参考文档

本文介绍了如何使用MongoMapper进行复杂的查询操作,特别是对于那些在官方文档中未详细说明的查询方式。通过参照MongoDB的高级查询文档,可以实现灵活且强大的查询功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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


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


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

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

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

[quote]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:[/quote]

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);

[quote]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.[/quote]

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值