0

What I'm trying to do is take a user's query client side (via search function), send it to server via Meteor methods and if there is a search result, insert that query into another MongoDB instance? I need this in order to display search results from the user into client.

Something like this from server side:

UserSearch: function(query){

    var init = Table.find({userId: this.userId}, {data: {Name: query}});

    Search.insert({userId: this.userId, data: init});

  },

Obviously, that doesn't work. But I'm looking for a way to allow to insert a query from one database into a new database.

Mabeh Al-Zuq Yadeek
  • 46
  • 4
  • 16
  • 28
  • You cannot insert a cursor (which is what `find()` returns), but you can insert an array, which is what calling `fetch()` on the cursor will return. I would advise against it, though, as there is a hefty performance penalty for this approach. If you want to return the data to the user once, you can probably return it directly from the method. – MasterAM Feb 29 '16 at 14:41
  • Do you mean insert the result or just the query parameters? – Stephen Woods Feb 29 '16 at 14:51
  • Inserting just the result. – Mabeh Al-Zuq Yadeek Feb 29 '16 at 14:52
  • Why two MongoDB instances? What problem are you trying to solve? – Philipp Feb 29 '16 at 15:03
  • @Philipp I'm trying to display a user's search results on a new page. These results contain detailed information about what the user is looking for and the only way I can think of solving it is find it in one database, copy it into a temporary database and display that information via helpers on the new template. – Mabeh Al-Zuq Yadeek Feb 29 '16 at 15:43
  • 1
    @MabehAl-ZuqYadeek There are far better solutions for sharing information between page views, like storing that temporary information in the users session data on your webserver or having the browser pass it as POST parameters. It doesn't make any sense to put it into a database (a different one than your primary one, to boot), just to retrieve it a few ms later and then delete it. Databases are for *persistent* information. And why do you need to share information at all? Usually you would perform the search query as part of requesting the result page and not before. – Philipp Feb 29 '16 at 15:46
  • Actually, you are right. It's a much smarter method to store the query in a session than storing it in a temporary database. I'll look at doing that. Thanks. – Mabeh Al-Zuq Yadeek Feb 29 '16 at 15:58

1 Answers1

1

So I think there are two pieces two your question here:

  1. How do I connect to another MongoDB instance?
  2. How do I insert data from one query result into a collection?

For #1, referencing this SO question, you can do:

var database = new MongoInternals.RemoteCollectionDriver("<mongo url>");
Search = new Mongo.Collection("search", { _driver: database });

For #2, you're simply missing the .fetch() as MasterAM pointed out:

UserSearch: function(query){
  var init = Table.find({userId: this.userId, 'data.Name': query}).fetch();
  Search.insert({userId: this.userId, data: init});
},

I also modified your Table query a bit as I don't think it's working in the current form. You want each field property in a query object to be in the same object. Note that the fetch will give you an array of Table objects, so you'll need to have your data field in your Search collection reflect that.

Community
  • 1
  • 1
Stephen Woods
  • 4,049
  • 1
  • 16
  • 27
  • I'll give you the best answer because you answered my question. However, I found a better and faster way to share my data than transferring it from one DB to another. I found out that Session variables work really well transferring arrays of data from one place to another. – Mabeh Al-Zuq Yadeek Mar 01 '16 at 20:27