DarkMatter in Cyberspace
  • Home
  • Categories
  • Tags
  • Archives

The Algorithm of Related Fairs


For a given fair A, it's major category are a set of its cat.major subdocuments.

For example, for the fair:

{ cat: [ { major: "abc", minor: [ 1,23,3] }, { major: "xyz", minor: [ 3, 23, 583]} ] }

its major categories are set ["abc, "xyz"]. Here we say the major categories of fair X is majorCat(X).

If the intersection of set majorCat(X) and majorCat(Y) are not empty, we say fair X and Y are related.

So for a given fair X, how to find all its related fairs in a collection?

Here is the demonstration:

In mongodb, create a test collection with the following codes:

db.cats.insert( { cat: [ { major: "abc", minor: [ 1,23,3] }, { major: "xyz", minor: [ 3, 23, 583]} ] } );
db.cats.insert( { cat: [ { major: "abcd", minor: [ 1,23,3] }, { major: "xyzu", minor: [ 87, 987, 343]} ] } );
db.cats.insert( { cat: [ { major: "bcd", minor: [ 1,23,3] }, { major: "abc", minor: [ 3, 23, 876]} ] } );
db.cats.insert( { cat: [ { major: "xyz", minor: [ 8,83,5] }, { major: "axc", minor: [ 34, 3, 76]} ] } );

Create a new Meteor app with meteor create relatedFairs, and its files:

relatedFairs.html

<head>
  <title>relatedFairs</title>
</head>

<body>
  <h1>Find related fairs</h1>
  {{> hello}}
</body>

<template name="hello">
  <ul>
    {{#each allFairs}}
      <li> 
        <b>{{_id._str}}:</b>
        {{#each cat}}
          {{major}},
        {{/each}}
      </li>
    {{/each}}
  </ul>
  {{#with related}}
    <p>Related fairs of the first fair {{firstID}}</p>
    <ul>
      {{#each relatedFairs}}
        <li>
          <b>{{_id._str}}:</b>
          {{#each cat}}
            {{major}},
          {{/each}}
        </li>
      {{/each}}
    </ul>
  {{/with}}
</template>

relatedFairs.js:

Fairs = new Mongo.Collection("cats");

if (Meteor.isClient) {
  Template.hello.helpers({
    allFairs: function () {
      return Fairs.find();
    },
    related: function () {
      var theXrd = 0,
          theFair = Fairs.findOne( {}, {skip: theXrd } );
      if (! theFair) {
        return null;
      }
      var majors = _.map(theFair.cat, function(i) { return i.major; } );
      var rels = Fairs.find( { cat: { $elemMatch: { major: { $in: majors } } },
                               _id: { $ne: theFair._id } } );
      return {firstID: theFair._id._str, relatedFairs: rels };
    }
  });
}

Here we use Underscore.js, so install this package with meteor add underscore to run our demo app (with MONGO_URL="mongodb://localhost:27017/test" meteor in project root folder).

Modify the value of "theXrd" above, you can see the related fairs of each fair.

To make a demo in a production collection, in fair "A" we find a major category called "垃圾车与运输车", then choose a fair B, whose id is "54e71a5642fb549b1389ae6f".

Let's add the major cateory to fair B:

db.fairs.update( { _id: ObjectId("54e71a5642fb549b1389ae6f") }, { $push: {category: {major: "垃圾车与运输车"}} } );

Now fair A and B is related. Test them on your web site. If the test is past, remove the test data from fair B:

db.fairs.update( { _id: ObjectId("54e71a6642fb549b1389ae6f") }, { $pop: { category: 1 } } );


Published

Feb 25, 2015

Last Updated

Feb 25, 2015

Category

Tech

Tags

  • javascript 11
  • meteor 47
  • mongodb 24

Contact

  • Powered by Pelican. Theme: Elegant by Talha Mansoor