如何在mongodb的变量中获取文档字段
此代码不起作用:
db.Conversation.find().forEach(function(item)
{
var _message = db.Message.find({ ConversationId : item._id.toString(), IsAgent : true }).sort({ SendDate: 1 }).limit(1);
if(_message._id != null)
{
item.FirstAgentMessageId = _message._id.toString();
item.FirstAgentMessageDate = _message.SendDate;
db.Conversation.save(item);
}
})
感谢名单
答案 0 :(得分:1)
成功的代码:
db.Conversation.find().forEach(function(item)
{
var _message = db.Message.find({ ConversationId : item._id.str, IsAgent : true }).sort({ SendDate: 1 }).limit(1).toArray()[0]
if(isObject(_message))
{
item.FirstAgentMessageId = _message._id.str
item.FirstAgentMessageDate = _message.SendDate;
db.Conversation.save(item);
}
})