In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
This article will explain in detail how to embed Javascript in MongoDBRuby. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.
How to embed Javascript in MongoDBRuby
Class Publication
Include Mongoid::Document
Field: name,: type = > String
Field: section,: type = > String
Field: body,: type = > String
Field: is_published,: type = > Boolean
End
Class LongerPublication
Field: extra_body,: type = > String
End
At this point, a Publication class and a LongerPublication class already exist in the system. Now we need to do some work on information collection, and we want to get statistics on the number of Publication class objects by their type and state. In addition, it is best to carry out targeted statistical analysis according to the current state.
One way is to use Mongo's built-in map-reduce. Mongoid extends this functionality, allowing programmers to use Ruby programs to implement the inline Javascript functions (mapper and reducer) they need. Readers may think this method is not good, but it seems to be the best way so far. There are many more complex functions that write these Javascript functions into separate files, which may be easier to test, but readers who test the input / output of this work themselves will find that their performance is not good.
How to embed Javascript in MongoDBRuby
KLASS = "this._type"
SECTION = "this.section"
Def self.count_by (type)
Map =
< function() { function truthy(value) { return (value ==true) ?1 :0; } emit(#{type}, {type: #{type}, count:1, published: truthy(this.is_published)}) } EOF reduce = < function(key, values) { var count =0; published =0; values.forEach(function(doc) { count += parseInt(doc.count); published += parseInt(doc.published); type = doc.type }; return {type: type, count: count, published: published} } EOF collection.mapreduce(map, reduce).find() end 在进行实验测试时会返回类似如下代码所示的数据;不过有些读者在实验时也有可能返回Mongo::Cursor,通过Mongo::Cursor可以获取到以下数据。 [{"_id"=>"Publication", "value" = > {"type" = > "Publication", "count" = > 42.0, "published" = > 29.0}}
{"_ id" = > "LongerPublication", "value" = > {"type" = > "LongerPublication", "count" = > 12.0, "published" = > 10.0}}]
Mongo and Mongoid make me like the mapreduce work of this kind of information collection very much, especially the mixing of Ruby and Javascript programming languages without affecting the readability of the code. Given a relational database, users are likely to write all kinds of strange SQL statements-- because the syntax differences between Javascript and SQL are much smaller than those between any other language and Ruby.
On how to embed Javascript in MongoDBRuby to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.