Mabeh Al-Zuq Yadeek
Mabeh Al-Zuq Yadeek

Reputation: 84

Is there a way to insert a DB query directly into a new MongoDB on server side?

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.

Upvotes: 0

Views: 374

Answers (1)

Stephen Woods
Stephen Woods

Reputation: 4049

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.

Upvotes: 1

Related Questions