如何将mongodb查询分配给变量并将其插入另一个集合?我似乎无法弄明白。
Meteor.methods({
'insertStatusData':function(statusInput,categorySelected,dateDB,userEmail,currentEventID){
var eventNameCol = Events.find({_id:currentEventID});
var eventName = eventNameCol.eventName; //trying to assign query to a variable
Status.insert({
currentEventName: eventName, //Inserting the query to a collection
statusDesc : statusInput,
category : categorySelected,
date: dateDB,
userEmail: userEmail,
eventID : currentEventID,
});
},
});
答案 0 :(得分:1)
使用 findOne()
方法返回与选择器匹配的第一个文档,而不是 find()
方法,该方法将光标返回到文档(不是与选择器匹配的实际文档)并且不会立即访问数据库:
Meteor.methods({
'insertStatusData': function(statusInput,categorySelected, dateDB, userEmail, currentEventID){
var eventName = Events.findOne({_id: currentEventID}).eventName; //trying to assign query to a variable
Status.insert({
currentEventName: eventName, //Inserting the query to a collection
statusDesc : statusInput,
category : categorySelected,
date: dateDB,
userEmail: userEmail,
eventID : currentEventID,
});
},
});