【问题标题】:Setting a server variable to data held in a mongo collection Meteor.js将服务器变量设置为 mongo 集合 Meteor.js 中保存的数据
【发布时间】:2016-05-05 04:57:07
【问题描述】:

对于我的应用程序,我必须进行 HTTP.call 以获取会话 ID。我需要将此会话 ID 存储到一个 mongo 集合中,我正在这样做。

if (Meteor.isServer) {
  sessionDB = new Mongo.Collection("sessionID");
  HTTP.call( 'GET', 'http://mycalltosomeapi.svc/json/whatever', {
  }, function( error, response ) {
if ( error ) {
  console.log(error);
} else {
  sessionDB.insert({
sessionId: response.data.session_id,
date_inserted: new Date()
  });
}
}

如何获取 mongo 存储的“sessionId”的最新条目并将其放入变量中,以便进行其他调用?我试过这样做,但没有运气。

var sessionId = sessionDB.findOne({}, {sessionId:1}).sort({"date_inserted":-1}).limit(1);

现在已经研究了几个小时,找不到任何东西。提前致谢

【问题讨论】:

    标签: javascript mongodb http meteor server


    【解决方案1】:

    sortlimitfindfindOne 的选项。试试这个:

    var options = {
      fields: {sessionId: 1},
      sort: {date_inserted: -1}
    };
    
    var sessionId = sessionDB.findOne({}, options);
    

    【讨论】:

      猜你喜欢
      • 2021-05-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-06
      • 1970-01-01
      相关资源
      最近更新 更多