7

Is it possible to store the result of a mongodb statement in array using jquery

I have like this

Polls_Coll.find({},{question:1});

I want all the question filed records to store in array some thing like

var arr[]=Polls_Coll.find({},{question:1});

I know above thing is wrong. I need something like that.

I need it for autocompletion. Now i'm taking source from one collection like this

 source:_(Product_Mobiles.find().fetch()).pluck("title")

I want data from multiple sources and store it in array

Thanks

Sasikanth
  • 3,045
  • 1
  • 22
  • 45

2 Answers2

11

Using the mongo console you can do this with .toArray() like

var results = db.collection.find({}).toArray();

However, this might depend on the driver you are using... I guess the javascript driver has it as well.

If your problem is putting all the results from multiple sources into a single array: How to merge two arrays in Javascript and de-duplicate items

Community
  • 1
  • 1
joao
  • 4,067
  • 3
  • 33
  • 37
  • Get the results from each source and then concatenate the arrays into a single array. – joao Feb 05 '14 at 11:07
2

You could merge the two arrays if thats what you mean:

var results = collection.find({}).fetch();
var results2 = collection2.find({}).fetch();

results = results.concat(results2);

Then you can do pluck

_(results).pluck("title");

Also you can't use db. in Meteor you have to use the name of the collection varaible you defined with new Meteor.Collection

Tarang
  • 75,157
  • 39
  • 215
  • 276