top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Write a meteor program, which gets a data from textbox and fetch the results from MongoDB?

+1 vote
249 views

Write a meteor program. Which gets a data from textbox and fetch the results from MongoDB. And Display the corresponding Search results.

posted Dec 2, 2016 by Jishnu Ramachandran

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote

Basically what you are looking is serach in mongodb using meteor...

It’s possible to do full text search in Meteor, as long as you’re running MongoDB 2.6 (which is included in Meteor 1.0.4 or higher). Earlier versions of MongoDB did not support text search well.

Text Index
Before you can do a search, you need to build a text index. This is a data structure that lets MongoDB perform text searches incredibly quickly. Whenever data is inserted or removed from the indexed collection, MongoDB updates the corresponding index.

Creating a text index:

if (Meteor.isServer) {
  Messages._ensureIndex({
    "value": "text"
  });
}

The fields specified will denote the fields that are considered when a search is performed. For example, if you use the above index, and search for something, it will ignore all other properties except for value.

You may create a text index that uses multiple properties like this:

if (Meteor.isServer) {
  Messages._ensureIndex({
    "title": "text",
    "value": "text"
  });
}

Publish Method
We’ll need to handle the text search on the server, in a publish method. As of this writing, Minimongo does not support the $text operator, so doing this on the client is not possible.

The search is done using the $text operator, using this form for the query:

  { $text: {$search: 'cookies'} }

By default, the results are not ordered, and so that query will return each document that has at least one “cookies” in it’s text index. Usually you want to show the documents that rank higher first. For instance a document containing “cookies cookies cookies” should be shown before one that contains only “cookies”.

The code to sort by search rank is a bit funky, but here’s a full example of a publish method:

// If `searchValue` is not provided, we publish all known messages. If it is
// provided, we publish only messages that match the given search value.
Meteor.publish("search", function(searchValue) {
  if (!searchValue) {
    return Messages.find({});
  }
  return Messages.find(
    { $text: {$search: searchValue} },
    {
      // `fields` is where we can add MongoDB projections. Here we're causing
      // each document published to include a property named `score`, which
      // contains the document's search rank, a numerical value, with more
      // relevant documents having a higher score.
      fields: {
        score: { $meta: "textScore" }
      },
      // This indicates that we wish the publication to be sorted by the
      // `score` property specified in the projection fields above.
      sort: {
        score: { $meta: "textScore" }
      }
    }
  );
});

In general, you can typically leave the fields and sort alone.

On the Client / Subscribe
The subscription code on the client is almost identical to any other subscription, and is different only if you want to sort by search rank.
When you specify the score property in the fields projection, each document that is sent to the client will have a score property that can be used for sorting.

Here’s how our client’s subscribe code might look:

Template.search.helpers({
  messages: function() {
    Meteor.subscribe("search", Session.get("searchValue"));
    if (Session.get("searchValue")) {
      return Messages.find({}, { sort: [["score", "desc"]] });
    } else {
      return Messages.find({});
    }
  }
});

If a search is being done, we sort the published results by their score property. Otherwise, we just show them in the order that the server published them.

Credit: https://www.okgrow.com/posts/guide-to-full-text-search-in-meteor

answer Dec 3, 2016 by Salil Agrawal
Similar Questions
+1 vote

I want to update Name, Age, and Mark fields in the collection school in MongoDB using Meteor.
Is it possible, then how?

+3 votes

Here I would like to explain my query a bit more.

I inserted multiple documents within a collection. Few documents have same set of keys and few of them have some extra key:value pairs . For example:
document 1 : key1 : "key1"
document 2: key1 : "key1", key2: "key2"
document 3: key1 : "key1", key2: "key2", key3 : "key3"
document 4: key1 : "key1", key3 : "key3"

Now I want to delete the documents which have key1 and key2.
What will be the command to do so ?

+1 vote

We have a situation where we have the raw data files, but mongodump is no longer finishing. We want to get the data from the data files in a readable format (e.g. JSON). We are unable to extract the data from these files due to the header information.

Does anyone have a strategy for extracting the records from these files?

...