简体   繁体   中英

Access native mongoDB collection in Meteor

I am using this ShareJS package in my Meteor application. ShareJS uses the collections docs and ops . How can I access these collections from the server? Do I have to define them as collections as well?

You can use MongoInternals . But make sure to wrap it in a Future for it to behave correctly. I like the following IIFE pattern the most:

var connection = MongoInternals.defaultRemoteCollectionDriver().mongo;

var searchResults = (function(collectionName, query) {

    var future = new Future(); 

    connection._getCollection(collectionName)
      .find(query)
      .toArray(function(error, results) {
        if(error) future.throw(error);
        else future.return(results || []);
      });      

    return future.wait();

})(options);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM