如何从 MongoDB 文档中获取价值 [英] How to get value from a MongoDB document
本文介绍了如何从 MongoDB 文档中获取价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试从 mongodb 文档中获取一个值,但即使 request
是正确的,我也没有得到结果:
I am trying to get a value from a mongodb document, but I am not getting result even if the request
is correct:
db.open(function(err, db){
var request = {
'hash' : req.params['link']
}
db.collection('urlmaps', function(error, collection){
var result = collection.find(request, {'long_url' : 1 });
console.log(JSON.stringify(result));
res.send(JSON.stringify(result));
});
});
我期待字符串类型的 var 结果
,我不确定我的查询是否足以从数据库中提取字符串.有什么想法吗?
I am expecting a var result
of type string, I am not sure of my query is enough to pull out the string from the database. Any ideas?
推荐答案
我不得不使用 collection.findOne(request, function(...))
而不是 collection.find代码>.这解决了问题.
I had to use collection.findOne(request, function(...))
instead of just collection.find
. That fixed the problem.
这篇关于如何从 MongoDB 文档中获取价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文